simulation in R | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

simulation in R

hi, I run the following code in R but this error shows up for "while" part: Error in beta_1 * x: non-numeric argument to binary operator beta_0 <- 2 beta_0 beta_1 <- 1 beta_1 sig <- 4 n=100 x <- sample(x,100) x eps <- rnorm(n,mean=3,sd=sig) y <- beta_0 + beta_1*x +eps ols <- lm(y~x) ols beta_1 <- rep(0,m) beta_1 library(data.table) m <- 10000 while(i <= m){   eps_i <-  rnorm(n,mean=0, sd=sig)   y_i <- beta_0 + beta_1*x + eps   beta_1[i] <- lm(y~x)   i <- i+1 } hist(beta_1) https://code.sololearn.com/c1ZX4J6Qbu33/?ref=app

15th Oct 2022, 6:33 PM
Tree
Tree - avatar
5 Answers
+ 1
Where did you initialize i?
15th Oct 2022, 6:43 PM
Lisa
Lisa - avatar
+ 1
People are more likely to test your code if you LINK your code instead of pasting it into the task description.
15th Oct 2022, 7:52 PM
Lisa
Lisa - avatar
0
I added i=1 before "while" but I still get the error
15th Oct 2022, 6:46 PM
Tree
Tree - avatar
0
You are trying to use variables before you have defined them several times. Have a look at the comments that I made: https://code.sololearn.com/creFra0W09N7/?ref=app
15th Oct 2022, 8:33 PM
Lisa
Lisa - avatar
0
That simulation returns an error
17th Oct 2022, 4:29 PM
Byrone Omondi
Byrone Omondi - avatar