library(MASS)
library(intervals)
n = 500 # {500, 1000, 2000}
VIO.str = 0.4 # {0.2, 0.4}
setting = "S2" # {"S1", "S2", "S3", "S4", "S5"}
sim.round = 1
IV.str = 0.5
# the number of simulations
nsim=500
####################
pi.value = IV.str*VIO.str
beta = 1
case = "hetero"
px = 10
if(setting=="S1"){
L = 10;
s1 = s2 = 2; s = s1+s2
alpha = c(rep(0,L-s1-s2),rep(pi.value,s1),-seq(1,s2)/2)
}
if(setting=="S2"){
L = 10;
s1 = 2; s2 = 4; s=s1+s2
alpha = c(rep(0,L-s),rep(pi.value,s1),-seq(1,s2)/3)
}
if(setting=="S3"){
L = 10;
s1 = 2; s2 = 4; s=s1+s2
alpha = c(rep(0,L-s),rep(pi.value,s1),-seq(1,s2)/6)
}
if(setting=="S4"){
L = 6;
s1 = s2 = s3 = s4 = 1; s=s1+s2+s3+s4
alpha = c(rep(0,L-s),rep(pi.value,s1),-seq(0.8,s2),-seq(0.4,s3),seq(0.6,s4))
}
if(setting=="S5"){
L = 6;
s1 = s2 = s3= s4 = 1; s=s1+s2+s3+s4
alpha = c(rep(0,L-s),rep(pi.value,s1),-seq(0.8,s2),-seq(0.4,s3),seq(pi.value+0.1,s4));
}
gamma=rep(IV.str,L)
p=L+px # p stands for the number of total exogeneous variables
phi<-rep(0,px)
psi<-rep(0,px)
phi[1:px]<-(1/px)*seq(1,px)+0.5
psi[1:px]<-(1/px)*seq(1,px)+1
rho=0.5
A1gen <- function(rho, p){
A1 = matrix(0, nrow=p, ncol=p)
for(i in 1:p) for(j in 1:p) A1[i, j] = rho^(abs(i-j))
return(A1)
}
Cov<-(A1gen(rho,p))
CI.sear = matrix(NA, nrow=nsim, ncol=2)
CI.samp = matrix(NA, nrow=nsim, ncol=2)
for(i.sim in 1:nsim){
set.seed(i.sim+(sim.round-1)*nsim)
print(i.sim)
W = mvrnorm(n, rep(0, p), Cov)
Z = W[, 1:L]
X = W[, (L+1):p]
if(case=="hetero"){
epsilon1 = rnorm(n)
tao1 = rep(NA, n); for(i.n in 1:n) tao1[i.n] = rnorm(n=1, mean=0, sd=0.25+0.5*(Z[i.n, 1])^2)
tao2 = rnorm(n)
epsilon2 = 0.3*epsilon1 + sqrt((1-0.3^2)/(0.86^4+1.38072^2))*(1.38072*tao1+0.86^2*tao2)
}else if(case=="homo"){
epsilonSigma = matrix(c(1, 0.8, 0.8, 1), 2, 2)
epsilon = mvrnorm(n, rep(0, 2), epsilonSigma)
epsilon1 = epsilon[,1]
epsilon2 = epsilon[,2]
}
D = 0.5 + Z %*% gamma+ X%*% psi + epsilon1
Y = -0.5 + Z %*% alpha + D * beta + X%*%phi+ epsilon2
out1 = SearchingSampling(Y, D, Z, X, intercept=TRUE, method="OLS", robust=TRUE, Sampling=FALSE)
out2 = SearchingSampling(Y, D, Z, X, intercept=TRUE, method="OLS", robust=TRUE, Sampling=TRUE)
CI.sear[i.sim, ] = out1$ci
CI.samp[i.sim, ] = out2$ci
}
rm(Y, D, Z, X)
filename = paste("Hetero-Setting", setting, "-Strength", IV.str, "-Violation", VIO.str, "-n", n,"-SimRound",sim.round, ".RData", sep="")
save.image(filename)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.