+ Reply to Thread
Results 1 to 7 of 7

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

  1. #1
    Points: 723, Level: 14
    Level completed: 23%, Points required for next Level: 77

    Location
    UNITED KINGDOM
    Posts
    19
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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.
    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:
    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
    Kindly help me please.
    Your assistance is highly appreciated in advance.
    Many thanks.

  2. #2
    Points: 1,980, Level: 26
    Level completed: 80%, Points required for next Level: 20
    derksheng's Avatar
    Posts
    247
    Thanks
    49
    Thanked 34 Times in 28 Posts

    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 })
    Last edited by derksheng; 06-21-2012 at 08:59 PM.

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

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

  4. #3
    Points: 723, Level: 14
    Level completed: 23%, Points required for next Level: 77

    Location
    UNITED KINGDOM
    Posts
    19
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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. #4
    Cookie Scientist
    Points: 5,945, Level: 49
    Level completed: 98%, Points required for next Level: 5
    Jake's Avatar
    Location
    Boulder, CO
    Posts
    797
    Thanks
    18
    Thanked 315 Times in 241 Posts

    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.
    “In God we trust. All others must bring data.”
    ~W. Edwards Deming

  6. #5
    Points: 723, Level: 14
    Level completed: 23%, Points required for next Level: 77

    Location
    UNITED KINGDOM
    Posts
    19
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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. #6
    Points: 1,980, Level: 26
    Level completed: 80%, Points required for next Level: 20
    derksheng's Avatar
    Posts
    247
    Thanks
    49
    Thanked 34 Times in 28 Posts

    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. #7
    Points: 723, Level: 14
    Level completed: 23%, Points required for next Level: 77

    Location
    UNITED KINGDOM
    Posts
    19
    Thanks
    9
    Thanked 0 Times in 0 Posts

    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.

+ Reply to Thread

Posting Permissions

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








Advertise on Talk Stats