How to create doubled a loop?

主宰稳场 提交于 2019-12-12 04:24:14

问题


The following code works out quite well, BUT: I have to change the sample size n = 25, 50, ... and the variance estimator everytime before I run the code. I would like to solve this problem with a loop.

Hereafter, I briefly describe the code. Within the code, 1000 regression models for a given sample size n are created. Then, each regression model out of the 1000 is estimated by OLS. After that, I calculate t statistics based on the different beta values of x3 out of the 1000 samples. The nullhypothessis reads: H0: beta03 = beta3, that is the calculated beta value of x3 equals the 'real' value which I defined as 1. In the last step, I check how often the nullhypothesis is rejected (significance level = 0.05). My final goal is to create a code which spits out the procentual rejection rate of the nullhypothesis for each sample size and variance estimator. I would be pleased if anyone of you could help me with that. Here you can see my code:

#sample size n = 25, 50, 100, 250, 500, 1000
n <- 50
B <- 1000

#'real' beta values 
beta0 <- 1
beta1 <- 1
beta2 <- 1
beta3 <- 1

t.test.values <- rep(NA, B)

#simulation of size
for(rep in 1:B){

#data generation 
d1 <- runif(n, 0, 1)
d2 <- rnorm(n, 0, 1)
d3 <- rchisq(n, 1, ncp=0)
x1 <- (1 + d1)
x2 <- (3*d1 + 0.6*d2)
x3 <- (2*d1 + 0.6*d3)
exi <- rchisq(n, 4, ncp = 0)
y <- beta0 + beta1*x1 + beta2*x2 + beta3*x3 + exi
mydata <- data.frame(y, x1, x2, x3)

#ols estimation
lmobj <- lm(y ~ x1 + x2 + x3, mydata)

#extraction
betaestim <- coef(lmobj)[4]
betavar   <- vcov(lmobj)[4,4]

#robust variance estimators: hc0, hc1, hc2, hc3
betavar0 <- hccm(lmobj, type="hc0")[4,4]
betavar1 <- hccm(lmobj, type="hc1")[4,4]
betavar2 <- hccm(lmobj, type="hc2")[4,4]
betavar3 <- hccm(lmobj, type="hc3")[4,4]


#t statistic
t.test.values[rep] <- (betaestim - beta3h0)/sqrt(betavar)


}

alpha <- 0.05
test.decision <- abs(t.test.values) < qt(p=c(1-alpha/2), df=n-4)
length(test.decision[test.decision==FALSE])/B

回答1:


Write a function that runs a simulation

library(car)
sample_size = c("n=25"=25, "n=50"=50, "n=100"=100, "n=250"=250, "n=500"=500, "n=1000"=1000)
B <- 100
beta0 <- 1
beta1 <- 1
beta2 <- 1
beta3 <- 1
alpha <- 0.05

sim <- function(n, beta3h0){
  t.test.values <- rep(NA, B)
  #simulation of size
  for(rep in 1:B){
    #data generation 
    d1 <- runif(n, 0, 1)
    d2 <- rnorm(n, 0, 1)
    d3 <- rchisq(n, 1, ncp=0)
    x1 <- (1 + d1)
    x2 <- (3*d1 + 0.6*d2)
    x3 <- (2*d1 + 0.6*d3)
    exi <- rchisq(n, 4, ncp = 0)
    y <- beta0 + beta1*x1 + beta2*x2 + beta3*x3 + exi
    mydata <- data.frame(y, x1, x2, x3)
    #ols estimation
    lmobj <- lm(y ~ x1 + x2 + x3, mydata)
    #extraction
    betaestim <- coef(lmobj)[4]
    betavar   <- vcov(lmobj)[4,4]
    #robust variance estimators: hc0, hc1, hc2, hc3
    betavar0 <- hccm(lmobj, type="hc0")[4,4]
    betavar1 <- hccm(lmobj, type="hc1")[4,4]
    betavar2 <- hccm(lmobj, type="hc2")[4,4]
    betavar3 <- hccm(lmobj, type="hc3")[4,4]
    #t statistic
    t.test.values[rep] <- (betaestim - beta3h0)/sqrt(betavar)
  }
  mean(abs(t.test.values) < qt(p=c(1-alpha/2), df=n-4))
}

And use lapply

sapply(sample_size, sim, beta3h0 = 0.7)
#  n=25   n=50  n=100  n=250  n=500 n=1000 
#  0.92   0.88   0.92   0.79   0.44   0.24 


来源:https://stackoverflow.com/questions/40674367/how-to-create-doubled-a-loop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!