-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmlsim.R
67 lines (51 loc) · 1.3 KB
/
dmlsim.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
rm(list=ls())
gc(TRUE)
source("JointSol_rep.R")
source("cv_dml.R")
# Matrix Power
"%^%" = function(x, n)
with(eigen(x), vectors %*% (values^n * t(vectors)))
# function to intialize y
mvsim <- function(x,prob,beta,covar){
n = dim(x)[1]
m = ncol(covar)
y = matrix(0,n,m)
for (i in 1:n){
y[i,] = rmvnorm(1,mean=x[i,]%*%beta,sigma=covar)
}
y
}
set.seed(13)
n <- 500 # nr of observation
p <- 80 # nbr of component of x
m <- 2 # nbr of component of y
k <- 1 # nbr of mixture model
x <- matrix(rnorm(n*p),n,p)
prob = 1
beta <- array(0, dim = c(p,m))
beta <- rbind(c(1,3),c(3,1),c(1,3),c(1,1),matrix(0, p-4, m))
covar <- array(0, dim = c(m,m))
covar <- matrix(c(2,1,1,2),2,2)
y <- mvsim(x=x,covar=covar,beta=beta,prob=prob)
# Initial for testing line by line
lambda =0.05
Y = y
X = x
#fit dml
fit <- JointSol(y,x,lambda=0.05)
P.est <- fit$P
Phi.est <- fit$Phi
fit
# fit dmlpath
la <- seq(0.5,1.5,length=21)
fdml <- dmlpath(y,x,lambda = la)
apply(fdml$coef, 3, function(x) x[1:10,1])
apply(fdml$coef, 3, function(x) x[1:10,2])
apply(fdml$Phi, 3, function(x) x[1:10,1])
apply(fdml$Phi, 3, function(x) x[1:10,2])
#apply(fdml$P, 3, function(x) x[1:10,1])
#apply(fdml$P, 3, function(x) x[1:10,2])
#apply(fdml$P, 3, eigen)
fdml$covar[,,1]
# perform cross validation
dml.cv <- cvdmlpath(y,x,lambda = la)