2
0
Fork 0
CVE/CVE_legacy/M2.R

61 lines
2.6 KiB
R
Executable File

#initialize model parameterss
m<-100 #number of replications in simulation
dim<-12 #dimension of random variable X
truedim<-2 #dimension of B=b
qs<-dim-truedim # dimension of orthogonal complement of B
b1=c(1,rep(0,dim-1))
b2=c(0,1,rep(0,dim-2))
b<-cbind(b1,b2) #B
P<-b%*%t(b)
sigma=0.5 #error standard deviation
N<-200 #sample size
K<-30 #number of arbitrary starting values for curvilinear optimization
MAXIT<-50 #maximal number of iterations in curvilinear search algorithm
M2_JASA<-mat.or.vec(m,8)
colnames(M2_JASA)<-c('CVE1','CVE2','CVE3','meanMAVE','csMAVE','phd','sir','save')
f_link<-function(x){return(x[1]*(x[2])^2)}
for (i in 1:m){
#generate dat according to M2
dat<-creat_sample(b,N,f_link,sigma)
#est sample covariance matrix
Sig_est<-est_varmat(dat[,-1])
#est trace of sample covariance matrix
tr<-var_tr(Sig_est)
#CVE
#calculates Vhat_k for CVE1,CVE2, CVE3 for k=qs
CVE1<-stiefl_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.8),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE2<-stiefl_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(2/3),tr=tr),maxit = MAXIT,sclack_para = 0)
CVE3<-stiefl_opt(dat,k=qs,k0=K,h=choose_h_2(dim,k=dim-truedim,N=N,nObs=(N)^(0.5),tr=tr),maxit = MAXIT,sclack_para = 0)
#calculate orthogonal complement of Vhat_k
#i.e. CVE1$est_base[,1:truedim] is estimator for B with dimension (dim times (dim-qs))
CVE1$est_base<-fill_base(CVE1$est_base)
CVE2$est_base<-fill_base(CVE2$est_base)
CVE3$est_base<-fill_base(CVE3$est_base)
# calculate distance between true B and estimated B
M2_JASA[i,1]<-subspace_dist(CVE1$est_base[,1:truedim],b)
M2_JASA[i,2]<-subspace_dist(CVE2$est_base[,1:truedim],b)
M2_JASA[i,3]<-subspace_dist(CVE3$est_base[,1:truedim],b)
#meanMAVE
mod_t2<-mave(Y~.,data=as.data.frame(dat),method = 'meanMAVE')
M2_JASA[i,4]<-subspace_dist(mod_t2$dir[[truedim]],b)
#csMAVE
mod_t<-mave(Y~.,data=as.data.frame(dat),method = 'csMAVE')
M2_JASA[i,5]<-subspace_dist(mod_t$dir[[truedim]],b)
#phd
test4<-summary(dr(Y~.,data=as.data.frame(dat),method='phdy',numdir=truedim+1))
M2_JASA[i,6]<-subspace_dist(orth(test4$evectors[,1:truedim]),b)
#sir
test5<-summary(dr(Y~.,data=as.data.frame(dat),method='sir',numdir=truedim+1))
M2_JASA[i,7]<-subspace_dist(orth(test5$evectors[,1:truedim]),b)
#save
test3<-summary(dr(Y~.,data=as.data.frame(dat),method='save',numdir=truedim+1))
M2_JASA[i,8]<-subspace_dist(orth(test3$evectors[,1:truedim]),b)
print(paste(i,paste('/',m)))
}
boxplot(M2_JASA[,]/sqrt(2*truedim),names=colnames(M2_JASA),ylab='err',main='M2')
summary(M2_JASA[,])