library(MASS)
library(lqmm)
my.make.positive.definite <- function(n.v){
sigma <- matrix(rnorm(n.v^2),n.v,n.v)
sigma <- sigma + t(sigma)
sigma <- make.positive.definite(sigma)
return(sigma)
}
my.mvrnorm <- function(n.sample,n.v,m=NULL,sigma=NULL){
if(is.null(m)){
m <- rnorm(n.v)
}
if(is.null(sigma)){
sigma <- my.make.positive.definite(n.v)
}
return(mvrnorm(n.sample,m,sigma))
}
n.s <- 1000
n.v <- 5
M <- rnorm(n.v)
S <- my.make.positive.definite(n.v)
X <- my.mvrnorm(n.s,n.v,M,S)
plot(as.data.frame(X))
library(rgl)
tmp.X <- X[,1:3]
tmp.X <- rbind(tmp.X,rep(max(tmp.X),3),rep(min(tmp.X),3))
plot3d(tmp.X)
n.ss <- c(10,25,50,75,100,250,500,750,1000,5000,10000)
diff.M <- rep(0,length(n.ss))
for(i in 1:length(n.ss)){
n.s <- n.ss[i]
X <- my.mvrnorm(n.s,n.v,M,S)
M.hat <- apply(X,2,mean)
M.hat
M
pca.out <- prcomp(X)
K <- diag(pca.out$sdev)
R <- pca.out$rotation
P <- R %*% K
Q <- P %*% t(P)
S
diff.M[i] <- sum((Q-S)^2)
}
plot(log(n.ss),diff.M,type="l")