Thread: Help needed. Simple linear regression simulation in r.

1. Help needed. Simple linear regression simulation in r.

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.

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:
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``````
Many thanks.

2. Re: Help needed. Simple linear regression simulation in r.

You should work with lists.

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)

list<-list()
s.sample<-100
x<-10*runif(s.sample)

for(i in 1:n.iter) {
y<-true.intercept+true.slope*x+rnorm(s.sample,mean=0, sd=true.err.var)
list[[i]]<-lm(y~x)
}

#Store p-value of coefficient.
sapply(1:length(list), function(X) { summary(list[[X]])\$coef[2,4] })

#Store p-value of intercept.
sapply(1:length(list), function(X) { summary(list[[X]])\$coef[1,4] })

#Store R squares
sapply(1:length(list), function(X) { summary(list[[X]])\$r.squared })``````

3. The Following 2 Users Say Thank You to derksheng For This Useful Post:

GretaGarbo (06-22-2012), JUSTICE (06-21-2012)

4. Re: Help needed. Simple linear regression simulation in r.

Hi Derksheng,
Thank you very much for the help.
I am very grateful.
Will get in touch should I need further help on that.
Best regards.

5. Re: Help needed. Simple linear regression simulation in r.

Hi derksheng,

Very helpful response. In the future, it is encouraged when posting code to wrap the code in "code tags," which is done like so:

[code]
awesome
code
goes
here
[/code]

This is only for formatting reasons, but it does make it quite a bit easier to read since it preserves the spacing.

6. Re: Help needed. Simple linear regression simulation in r.

Dear Derksheng,
Many thanks for the helpful and quick response yesterday.
I have tried to use your codes to simulate similar problems and it works perfectly.
Could you kindly help me to compute the standard error of the regression slope (b) and the t-value for the same problem.
Best regards.

7. Re: Help needed. Simple linear regression simulation in r.

Well I'm not 100% sure that this is what the question is after (I don't have time to completely do it), but to get the t-values and the standard errors;

Code:
``````#Retrieve t-values of intercept#
sapply(1:length(list), function(X) { summary(list[[X]])\$coef[1,3] })

#Retrieve sample standard-errors of intercept#
sapply(1:length(list), function(X) { summary(list[[X]])\$coef[1,2] })

#Retrieve t-values of slope coeff#
sapply(1:length(list), function(X) { summary(list[[X]])\$coef[2,3] })

#Retrieve estimated SEs of slope coeff#
sapply(1:length(list), function(X) { summary(list[[X]])\$coef[2,2] })``````
Be careful in your language. This isn't the population standard error. We can't observe the population standard error of the estimator. Also, it's the sample standard error of the ESTIMATOR, not of the ESTIMATE. The estimate is a constant, the estimator is a random variable.

If you want to learn of the meaning of the numbers inside my \$coef[number1,number2], just go summary(list[[1]])\$coef and observe the dimensions of the matrix and the column names.

8. The Following 2 Users Say Thank You to derksheng For This Useful Post:

GretaGarbo (06-22-2012), JUSTICE (06-22-2012)

9. Re: Help needed. Simple linear regression simulation in r.

Hi Derksheng,
I am very grateful for the response and the notes. They were very helpful.
Best regards.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts