# Vectors defining the length of the data sets
no_indiv = c(50, 50, 100, 100)
no_time = c(10, 20, 10, 20)
# Generate matrices to store the values of the RMSE and the IMAE
Sim_res_RMSE_1 = Sim_res_RMSE_2 = Sim_res_RMSE_3 = Sim_res_RMSE_4 = matrix( 0, nrow = rep, ncol = 4 )
colnames(Sim_res_RMSE_1) = colnames(Sim_res_RMSE_2) = colnames(Sim_res_RMSE_3) = colnames(Sim_res_RMSE_4) <- c("LF/LMU", "LF", "LMU", "LL")
Sim_res_IMAE_1 = Sim_res_IMAE_2 = Sim_res_IMAE_3 = Sim_res_IMAE_4 = matrix( 0, nrow = rep, ncol = 4 )
colnames(Sim_res_IMAE_1) = colnames(Sim_res_IMAE_2) = colnames(Sim_res_IMAE_3) = colnames(Sim_res_IMAE_4) <- c("LF/LMU", "LF", "LMU", "LL")
dgp = function(x){ x^2 }
# Monte Carlo repetitions
rep = 400
for(d in 1:4){
for(i in 1:rep){
# Specify length of the data
N = no_indiv[d]; T = no_time[d] ; NT = N*T
# Draw the data
Data = dgpivfe(N=N, T=T)
# 1) The LF/LMU estimator (IV with two-way fixed effects)
if(i == 1){ # Bandwidths are fixed to those obtained of the first repetition
res.npivfe = npregivfe( y = Data$y,
x = Data$x,
z = Data$z,
N = N,
T = T,
c = 0.8,
tol = 1,
max.iter = 100,
bw_method = "optimal" )
bws_LF_LMU = phi_hat_LF_LMU[[3]]
fit.npivfe =res.npivfe[[1]][, ncol( res.npivfe[[1]] ) ]
}else{
res.npivfe = res.npivfe( y = Data$y,
x = Data$x,
z = Data$z,
N = N,
T = T,
c = 0.8,
tol=1,
max.iter=100,
bw_method = "plug_in",
bws = bws_LF_LMU )
}
# 2) The LF estimator (nonparametric IV only)
if(i == 1){
res.npiv2 = npregiv2( y = Data$y,
z = Data$x,
w = Data$z,
c = 0.8,
tol=1,
max.iter = 100,
bw_method = "optimal" )
bws_LF = res.npiv2[[3]]
fit.npivf2 = res.npivf2[[1]][, ncol( res.npivf2[[1]] ) ]
}else{
res.npiv2 = npivf2( y = Data$y,
z = Data$x,
w = Data$z,
c = 0.8,
tol=1,
max.iter = 100,
bw_method = "plug_in",
bws = bws_LF )
fit.npivf2 = res.npivf2[[1]][, ncol(res.npivf2[[1]])]
}
# The LUM estimator (nonparametric two-way fixed effects)
if(i==1){
bw_LMU = optimize( f = Lee.CVCMB,
interval = c(0,5*sd(Data$x)),
lower = 0,
upper = 5*sd(Data$x),
tol = 0.001,
x = Data$x,
y = Data$y,
N = N,
T = T,
effects = "indiv-time",
type = "ll",
jump = 1,
c1 = 1/T^2, c2 = 1/((N*T)^2))$minimum
fit.LMU = LMU.estimator( h = bw_LMU,
x = Data$x,
y = Data$y,
N = N,
T = T,
effects = "indiv-time",
type = "ll",
c1 = 1/T^2, c2 = 1/((N*T)^2))[,"mhat"]
}else{
fit.LMU = LMU.estimator( h = bw_LMU,
x = Data$x,
y = Data$y,
N = N,
T = T,
effects = "indiv-time",
type = "ll",
c1 = 1/T^2, c2 = 1/((N*T)^2))[,"mhat"]
}
# The LL estimator (local-linear kernel regression)
if(i==1){
bw_ll = npregbw( Data$y~Data$x,
ckertype="gaussian" ,
regtype = "ll",
bwmethod = "cv.ls")$bw
fit.LL = fitted( npreg( bws = bw_ll,
tydat = Data$y,
txdat = Data$x ) )
}else{
fit.LL = fitted( npreg( bws = bw_ll,
tydat = Data$y,
txdat = Data$x,
ckertype="gaussian"))
}
if(d == 1){
# RMSE
Sim_res_RMSE_1[i,"LF/LMU"] = sqrt( mean( ( fit.npivfe - dgp(Data$x ) )^2 ) )
Sim_res_RMSE_1[i,"LF"] = sqrt( mean( ( fit.npiv2 - dgp( Data$x ) )^2 ) )
Sim_res_RMSE_1[i,"LMU"] = sqrt( mean( ( fit.LMU - dgp( Data$x ) )^2) )
Sim_res_RMSE_1[i,"LL"] = sqrt( mean( ( fit.LL - dgp(Data$x) )^2) )
# IMEA
Sim_res_IMAE_1[i,"LF/LMU"] = mean( abs( fit.npivfe - dgp( Data$x ) ) )
Sim_res_IMAE_1[i,"LF"] = mean( abs( fit.npiv2 - dgp( Data$x ) ) )
Sim_res_IMAE_1[i,"LMU"] = mean( abs( fit.LMU - dgp( Data$x ) ) )
Sim_res_IMAE_1[i,"LL"] = mean( abs( fit.LL - dgp( Data$x ) ) )
}
if(d==2){
# RMSE
Sim_res_RMSE_2[i,"LF/LMU"] = sqrt( mean( ( fit.npivfe - dgp(Data$x ) )^2 ) )
Sim_res_RMSE_2[i,"LF"] = sqrt( mean( ( fit.npiv2 - dgp( Data$x ) )^2 ) )
Sim_res_RMSE_2[i,"LMU"] = sqrt( mean( ( fit.LMU - dgp( Data$x ) )^2) )
Sim_res_RMSE_2[i,"LL"] = sqrt( mean( ( fit.LL - dgp(Data$x) )^2) )
# IMEA
Sim_res_IMAE_2[i,"LF/LMU"] = mean( abs( fit.npivfe - dgp( Data$x ) ) )
Sim_res_IMAE_2[i,"LF"] = mean( abs( fit.npiv2 - dgp( Data$x ) ) )
Sim_res_IMAE_2[i,"LMU"] = mean( abs( fit.LMU - dgp( Data$x ) ) )
Sim_res_IMAE_2[i,"LL"] = mean( abs( fit.LL - dgp( Data$x ) ) )
}
if(d==3){
# RMSE
Sim_res_RMSE_3[i,"LF/LMU"] = sqrt( mean( ( fit.npivfe - dgp(Data$x ) )^2 ) )
Sim_res_RMSE_3[i,"LF"] = sqrt( mean( ( fit.npiv2 - dgp( Data$x ) )^2 ) )
Sim_res_RMSE_3[i,"LMU"] = sqrt( mean( ( fit.LMU - dgp( Data$x ) )^2) )
Sim_res_RMSE_3[i,"LL"] = sqrt( mean( ( fit.LL - dgp(Data$x) )^2) )
# IMEA
Sim_res_IMAE_3[i,"LF/LMU"] = mean( abs( fit.npivfe - dgp( Data$x ) ) )
Sim_res_IMAE_3[i,"LF"] = mean( abs( fit.npiv2 - dgp( Data$x ) ) )
Sim_res_IMAE_3[i,"LMU"] = mean( abs( fit.LMU - dgp( Data$x ) ) )
Sim_res_IMAE_3[i,"LL"] = mean( abs( fit.LL - dgp( Data$x ) ) )
}
if(d==4){
# RMSE
Sim_res_RMSE_4[i,"LF/LMU"] = sqrt( mean( ( fit.npivfe - dgp(Data$x ) )^2 ) )
Sim_res_RMSE_4[i,"LF"] = sqrt( mean( ( fit.npiv2 - dgp( Data$x ) )^2 ) )
Sim_res_RMSE_4[i,"LMU"] = sqrt( mean( ( fit.LMU - dgp( Data$x ) )^2) )
Sim_res_RMSE_4[i,"LL"] = sqrt( mean( ( fit.LL - dgp(Data$x) )^2) )
# IMEA
Sim_res_IMAE_4[i,"LF/LMU"] = mean( abs( fit.npivfe - dgp( Data$x ) ) )
Sim_res_IMAE_4[i,"LF"] = mean( abs( fit.npiv2 - dgp( Data$x ) ) )
Sim_res_IMAE_4[i,"LMU"] = mean( abs( fit.LMU - dgp( Data$x ) ) )
Sim_res_IMAE_4[i,"LL"] = mean( abs( fit.LL - dgp( Data$x ) ) )
}
}
}
# Output table of the MC simulation
tab.MC = cbind(no_indiv, no_time, rbind( round( colMeans( Sim_res_RMSE_1 ), 3 ),
round( colMeans( Sim_res_RMSE_2 ), 3 ),
round( colMeans( Sim_res_RMSE_3 ), 3 ),
round( colMeans( Sim_res_RMSE_4 ), 3 ) ),
rbind( round( colMeans( Sim_res_IMAE_1 ), 3 ),
round( colMeans( Sim_res_IMAE_2 ), 3 ),
round( colMeans( Sim_res_IMAE_3 ), 3 ),
round( colMeans( Sim_res_IMAE_4 ), 3 ) ) )
colnames(tab.MC)[1:2] <- c("N", "T")