set.seed(1111)
N=1000
nit=20
ndim_z=2
#some true values not following the echeleon structure
z=matrix(rnorm(N*ndim_z),N,ndim_z)
w=matrix(rnorm(nit*ndim_z),nit,ndim_z)
# simluate data using these true values
dat_obj=LSMsim(N,nit,ndim_z,z=z,w=w)
X=dat_obj$X
#fit model
results=LSMfit(X,2)
#plot the parameter recovery results using the *unrotated* true values
#spoiler: will look like nothing
oldpar=par(mfrow=c(2,2))
s_p=sign(cor(results$z,z)) # to correct for sign switches in the plots
s_i=sign(cor(results$w,w))
plot(s_p[1,1]*z[,1],results$z[,1]); abline(0,1)
plot(s_p[2,2]*z[,2],results$z[,2]); abline(0,1)
plot(s_i[1,1]*w[,1],results$w[,1]); abline(0,1)
plot(s_i[2,2]*w[,2],results$w[,2]); abline(0,1)
#plot the parameter recovery results using the *rotated* true values
#spoiler: will look better
zt=dat_obj$par$zt # rotated true z, see ?LSMsim and ?LSMrotate
wt=dat_obj$par$wt # rotated true w
s_p=sign(cor(results$z,zt)) # to correct for sign switches in the plots
s_i=sign(cor(results$w,wt))
plot(s_p[1,1]*zt[,1],results$z[,1]); abline(0,1)
plot(s_p[2,2]*zt[,2],results$z[,2]); abline(0,1)
plot(s_i[1,1]*wt[,1],results$w[,1]); abline(0,1)
plot(s_i[2,2]*wt[,2],results$w[,2]); abline(0,1)
par(oldpar)
Run the code above in your browser using DataLab