r - How to create a double loop? -


i know how create double loop. in code, multiple regression on 1000 samples (each sample size: 25) then, create t test values each sample out of 1000 nullhypothesis: beta3 value sample = 'real' beta3 value. know 'real' beta3 value monte carlo simulation (beta 3 = value of third coefficient of regression). however, code works far. now, want same procedure sample sizes of 50, 100, 250, 500, , 1000 (each sample size 1000 times). how can realise goal loop. pleased if me! here can see code:

n <- 25 b <- 1000 beta3 <- 1.01901 #'real' beta3 value   t.test.values <- rep(na, b) for(rep in 1:b){  ##data generation   d1  <- runif(25, 0, 1)   d2  <- rnorm(25, 0, 1)   d3  <- rchisq(25, 1, ncp=0)   x1  <- (1 + d1)   x2  <- (3 * d1 + 0.6 * d2)   x3  <- (2 * d1 + 0.6 * d3)   exi <- rchisq(25, 5, ncp = 0)   y   <- beta0 + beta1*x1 + beta2*x2 + beta3*x3 + exi  ## estimation   lmobj      <- lm(y ~ x1 + x2 + x3)             ## extraction   betaestim <- coefficients(lmobj)[2:4]   betavar   <- vcov(lmobj)[2:4, 2:4]  ## t-test   t.test.values[rep] <- (betaestim[3] - beta3)/sqrt((betavar)[9])    } 

we can use data.frame store results. note didn't include values beta0, beta1, or beta2, used placeholder values.

n <- c(50,100,250,500,1000) #how big our sample sizes? b <- 1000 beta3 <- 1.01901 #'real' beta3 value  #other beta values (note these not included in question) beta1 <- 2 beta2 <- 4 beta0 <- 6  iter <- 1  #initialize our results data.frame result_df <- data.frame(sample_size = numeric(length(n) * b),                         t.test.values = numeric(length(n) * b)                         )  for(size in n){  for(rep in 1:b){    ##data generation   d1  <- runif(size, 0, 1)   d2  <- rnorm(size, 0, 1)         d3  <- rchisq(size, 1, ncp=0)         x1  <- (1 + d1)         x2  <- (3 * d1 + 0.6 * d2)         x3  <- (2 * d1 + 0.6 * d3)         exi <- rchisq(size, 5, ncp = 0)         y   <- beta0 + beta1*x1 + beta2*x2 + beta3*x3 + exi    ## estimation   lmobj      <- lm(y ~ x1 + x2 + x3)               ## extraction   betaestim <- coefficients(lmobj)[2:4]   betavar   <- vcov(lmobj)[2:4, 2:4]    ## store our values   result_df[iter, 1] <- size    result_df[iter, 2] <- (betaestim[3] - beta3)/sqrt((betavar)[9])    iter = iter + 1 #iterate    } } 

as long use track iterations (i've used iter here), double for loop isn't bad. make sure initialize data.frame correct size. might helpful @ replicate function , *apply class of functions if you're planning more simulations.


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -