Hi, help needed.
I wish to simulate a simple linear regression using R but I am new user of R and keep struggling with it for some days now.
Below is my task.
Task.
Set up a simulation model according to the following recipe:
i) n=1000
ii) Xi~N(0,1)
iii) Yi = a+bxi+Ɛi i=1,2,3,…,n.
a=0.2
b=0.03
Ɛi~N(0, 0.32)
iv) Calculate the regression slope b and store it.
v) Calculate the t-value, p-value and standard error of regression slope b.
vi) Run this recipe 10000 times in a loop. How many times was P-value <0.05 of power.
My effort so far but am really lost:
Kindly help me please.Code:# (1)set parameters true.intercept<-0.2 true.slope<-0.03 true.err.var<-0.09 n.iter<-1000 s.sample<-100 estimates<-matrix(NA,nrow=n.iter,ncol=2) x<-10*runif(s.sample) for (i in 1:n.iter) { #generate data y<-true.intercept+true.slope*x+rnorm(s.sample,mean=0,sd=true.err.var) #estimate things lm(y~x)->mod #save estimates mod$coef->estimates[i,] } nms<-c("intercept","slope") #compute variance of estimates cbind(1,x)->x2 s2<-solve(t(x2) %*% x2)*true.err.var^2 estimates
Your assistance is highly appreciated in advance.
Many thanks.




Reply With Quote

