Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
试图在R中使用MonteCarlo包_R_Montecarlo - Fatal编程技术网

试图在R中使用MonteCarlo包

试图在R中使用MonteCarlo包,r,montecarlo,R,Montecarlo,我试图用蒙特卡罗方法模拟差异中的差异估计量,但我遇到了一个错误。以下是我正在运行的代码: # Set the random seed set.seed(1234567) library(MonteCarlo) #Set up problem, doing this before calling the function # set sample size n<- 400 # set true parameters: betas and sd of u b0 <- 1 # inte

我试图用蒙特卡罗方法模拟差异中的差异估计量,但我遇到了一个错误。以下是我正在运行的代码:

# Set the random seed
set.seed(1234567)

library(MonteCarlo)

#Set up problem, doing this before calling the function
# set sample size
n<- 400
# set true parameters: betas and sd of u
b0 <- 1 # intercept for control data (b0 in diffndiff)
b1 <- 1  # shift on both control and treated after treatment (b1 in 
#diffndiff)
b2 <- 2 # difference between intercept on control vs. treated (b2-this is 
#the level difference pre-treatment to compare to coef on treat)
b3 <- 3  # shift after treatment that is only for treated group (b3-this is 
#the coefficient of interest in diffndiff)
b4 <- 0  # parallel time trend (not measured in diffndiff) biases b0,b1 but 
#not b3 that we care about
b5 <- 0 # allows for treated group trend to shift after treatment (0 if 
#parallel trends holds)
su <- 4  # std. dev for errors

dnd <- function(n,b0,b1,b2,b3,b4,b5,su){
#initialize a time vector (set observations equal to n)
timelength = 10
t <- c(1:timelength)


num_obs_per_period = n/timelength #allows for multiple observations in one 
#time period (can simulate multiple states within one group or something)
t0 <- c(1:timelength)
for (p in 1:(num_obs_per_period-1)){
t <- c(t,t0)
}
T<- 5 #set treatment period
g <- t >T
post <- as.numeric(g)

# assign equal amounts of observations to each state to start with (would 
#like to allow selection into treatment at some point)
treat <- vector()
for (m in 1:(round(n/2))){
treat <- c(treat,0)
}
for (m in 1:(round(n/2))){
treat <- c(treat,1)
}
u <- rnorm(n,0,su) #This assumes the mean error is zero

#create my y vector now from the data
y<- b0  + b1*post + b2*treat + b3*treat*post + b4*t + b5*(t-T)*treat*post +u

interaction <- treat*post

#run regression
olsres <- lm(y ~ post + treat + interaction)
olsres$coefficients

# assign the coeeficients
bhat0<- olsres$coefficients[1]
bhat1 <- olsres$coefficients[2]
bhat2<- olsres$coefficients[3]
bhat3<- olsres$coefficients[4]

bhat3_stderr <- coef(summary(olsres))[3, "Std. Error"]

#Here I will use bhat3 to conduct a t-test and determine if this was a pass 
#or a fail
tval <- (bhat3-b3)/ bhat3_stderr

#decision at 5% confidence I believe (False indicates the t-stat was less 
#than 1.96, and we fail to reject the null)
decision <- abs(tval) > 1.96

decision <- unname(decision)
return(list(decision))
} 

#Define a parameter grid to simulate over
from <- -5
to <- 5
increment <- .25
gridparts<- c(from , to , increment)

b5_grid <- seq(from = gridparts[1], to = gridparts[2], by = gridparts[3])

parameter <- list("n" = n, "b0" = b0 , "b1" = b1 ,"b2" = b2 ,"b3" = b3 ,"b4" 
= 
b4 ,"b5" = b5_grid ,"su" = su)

#Now simulate this multiple times in a monte carlo setting
results <- MonteCarlo(func = dnd ,nrep = 100, param_list = parameter)
#设置随机种子
种子集(1234567)
图书馆(蒙地卡罗)
#设置问题,在调用函数之前执行此操作
#设定样本量

n传递给MonteCarlo的函数必须返回一个包含命名组件的列表。将第76行更改为

返回(列表(“决策”=决策))


应该可以工作了

非常感谢!实际上,我在代码中有这一行,但更改了它,因为我遇到了另一个错误“列表组件中的标量无法命名”,但显然这与那一行无关。根据你的建议,它工作得非常完美。模拟是在阵列的最后一个维度!因此,您可以使用
results$results$decision[,,,,,,,1:100]
@Dbrim]访问它们