Problem is: Five observations on Y are to be taken when X = 4, 8, 12, 16, and 20, respectively. The true regression function is E(y} = 20 +4X, and the ei are independent N(O, 25).
a. Generate five normal random numbers, with mean 0 and variance 25. Consider these random numbers as the error terms for the five Y observations at X = 4,8, 12, 16, and 20 and calculate Y1, Y2 , Y3 , Y4 , and Y5. Obtain the least squares estimates bo and b1, when fitting a straight line to the five cases. Also calculate Yh when Xh = 10 and obtain a 95 percent confidence interval for E(Yh} when Xh = 10.
I did a), but I need help to repeat a) 200 times.
b. Repeat part(a) 200times,generating new random numbers each time.
c. Make a frequency distribution of the 200 estimates b1. Calculate the mean and standard deviation of the 200 estimates b1. Are the results consistent with theoretical expectations?
d. What proportion of the 200 confidence intervals for E(Yh) when Xh = 10 include E(Yh)? Is this result consistent with theoretical expectations?
Here's my code so far, I am stumped on how to repeat a) 200 times in order to do b-d:
X <- matrix(c(4, 8, 12, 16, 20), nrow = 5, ncol = 1)
e <- matrix(c(rnorm(5,0,sqrt(5))), nrow = 5, ncol = 1)
Y <- 20+4*X+e
mydata<-data.frame(cbind(Y=Y, X=X, e=e))
names(mydata) <- c("Y","X","e")
reg<-lm(Y~X, data=mydata)
predict(reg, newdata=data.frame(X=10), interval="confidence")
Aucun commentaire:
Enregistrer un commentaire