Karl
Karl

Reputation:

simulate a linear model 100 times

I need to simulate n=100 times a linear model, but get lost in the R commands.

I am still learning the basics of statistic and R, and I am a bit confused with this exercise:

I need to replicate a basic linear model 100 times using OLS and collect the N estimates in order to perform a test of consistency and efficiency. I have tried to solve the problem this way:

a <- 3
B <- 0.5
C <- -0.7

for (i in 1:100){
   x1[i] <- rnorm(200, mean=0, sd=1)
   x2[i] <- rnorm(200, mean=0, sd=1) 
   e[i] <- rnorm(200, mean=0, sd=1)
   y1[i] <- a+(B*x1[i])+(C*x2[i])+e[i] 

   y<- lm(y1[i]~x1[i]+x2[i]))
   results <-data.frame(coef(y))
}

but R keeps telling me there are errors. Could someone help me with this?

Upvotes: 2

Views: 6851

Answers (2)

user2016781
user2016781

Reputation: 1

    a <- 3
    B <- 0.5
    C <- -0.7
    sims <- 100

    #initialize a data frame to collect results
    df <- data.frame(matrix(ncol = 3, nrow = sims))
    colnames(df) <- c('a', 'B' , 'C')

    for(i in 1:sims){
    ##vectors each  200 long 
    x1 <- rnorm(200)
    x2 <- rnorm(200)
    e <- rnorm(200)

    y <- a + B*x1 + C*x2 +e
    #collect results for each itter
    df[i,] <- data.frame(t(lm(y ~x1 + x2)$coeff))
    }

    #results
    df

Upvotes: 0

Ben Bolker
Ben Bolker

Reputation: 226532

Something like:

a <- 3
B <- 0.5
C <- -0.7

results <- matrix(nrow=100,ncol=3)
for (i in 1:100){
   x1 <- rnorm(200, mean=0, sd=1)
   x2 <- rnorm(200, mean=0, sd=1) 
   e <- rnorm(200, mean=0, sd=1)
   y1 <- a+B*x1+C*x2+e 

   y<- lm(y1~x1+x2)
   results[i,] <- coef(y)
}

This assumes that you only need to save the coefficients from each run. A more elegant solution would be something like:

simfun <- function(a=3,B=0.5,C=-0.7,n=200,x1.sd=1,x2.sd=1,e.sd=1) {
   x1 <- rnorm(n, mean=0, sd=x1.sd)
   x2 <- rnorm(n, mean=0, sd=x2.sd) 
   e <-  rnorm(n, mean=0, sd=e.sd)
   y1 <- a+B*x1+C*x2+e 
   data.frame(x1,x2,y1)
}

statfun <- function(d) {
    coef(lm(y1~x1+x2,data=d))
}

library(plyr)
raply(100,statfun(simfun()))

Upvotes: 7

Related Questions