forked from wjiang94/miSAEM_logReg
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_selection.Rmd
201 lines (158 loc) · 5.76 KB
/
model_selection.Rmd
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
---
title: "Model selection "
author: "Wei Jiang"
date: "5/10/2018"
output: html_notebook
---
After we estimate the parameters and their variance with SAEM, the next objective is to perform variable selection with missing values.
```{r}
library(misaem)
library(MASS)
library(mvtnorm)
```
Here we first assign the true values of parameters.
(By using different values, we can construct different setting, such as number of subjects $n$ or structure of correlation.)
```{r}
n <- 1000 # number of subjects
# n <- 10000 # or a larger number of subjects
p <- 5 # number of explanatory variables
mu.star <- 1:p # mean of the explanatory variables
sd <- 1:p # standard deviations
# with correlation
C <- matrix(c( # correlation matrix
1, 0.8, 0, 0, 0,
0.8, 1, 0, 0, 0,
0, 0, 1, 0.3, 0.6,
0, 0, 0.3, 1, 0.7,
0, 0, 0.6, 0.7, 1
), nrow=p)
## or without correlation
# C = diag(p)
Sigma.star <- diag(sd)%*%C%*%diag(sd) # variance-covariance matrix of the explanatory variables
beta.star <- c(0.5, 0, 1, 0, -0.6) # coefficients of logistic regression
beta0.star <- -0.2 # intercept
beta.true = c(beta0.star,beta.star)
#percentage of missingness
p.miss <- 0.10
```
We consider the criterion based on penalised likelihood, such as AIC and BIC, for selecting variables.
For each combination of variables, we estimate the parameters with SAEM, then calculate the observed log-likelihood. Finally we select the best model according to the minimum value of AIC or BIC.
We run 100 repetitions of simulations, and count the percentage when, the percentage of cases where each criterion selects the true model (C), overfits (O) – i.e., selects more variables than there were – or underfits (U) – i.e., selects less variables than there were.
```{r}
nb.simu = 100
subsets=combinations(p)
ll = AIC = BIC = matrix(0, nrow = nb.simu, ncol = nrow(subsets)-1)
AIC_min =BIC_min = matrix(1e+5,nrow = nb.simu,ncol = p)
j_AIC = j_BIC = matrix(0,nrow = nb.simu,ncol = p)
AIC_all_min =BIC_all_min = rep(1e+5,nb.simu)
j_all_AIC = j_all_BIC = rep(0,nb.simu)
for(nb in 1:nb.simu){
set.seed(nb)
cat('simu ',nb,'\n')
# complete data simulation
X.complete <- matrix(rnorm(n*p), nrow=n)%*%chol(Sigma.star) + matrix(rep(mu.star,n), nrow=n, byrow = TRUE)
p1 <- 1/(1+exp(-X.complete%*%beta.star-beta0.star))
y <- as.numeric(runif(n)<p1)
# generate missingness
X.obs <- X.complete
patterns = runif(n*p)<p.miss
X.obs[patterns] <- NA
# iterate among each combination
for (j in 1:(nrow(subsets)-1)){
nb.var = sum(subsets[j,])
variables = subsets[j,]
pos_var=which(variables==1)
nb.x = sum(variables)
nb.para = (nb.x + 1) + p + p*p
list.saem.subset=miss.saem(X.obs,y,pos_var,maxruns=1000,tol_em=1e-7,nmcmc=2,tau=1,k1=5,print_iter=FALSE,ll_obs_cal=TRUE)
ll[nb,j] = list.saem.subset$ll
AIC[nb,j] = -2*ll[nb,j]+ 2*nb.para
BIC[nb,j] = -2*ll[nb,j]+ nb.para * log(n)
if(AIC[nb,j]<=AIC_min[nb,nb.x]){
AIC_min[nb,nb.x]= AIC[nb,j]
j_AIC[nb,nb.x] = j
}
if(BIC[nb,j]<=BIC_min[nb,nb.x]){
BIC_min[nb,nb.x]= BIC[nb,j]
j_BIC[nb,nb.x] = j
}
if(AIC[nb,j]<=AIC_all_min[nb]){
AIC_all_min[nb]= AIC[nb,j]
j_all_AIC[nb] = j
}
if(BIC[nb,j]<=BIC_all_min[nb]){
BIC_all_min[nb]= BIC[nb,j]
j_all_BIC[nb] = j
}
}
}
```
Plot the BIC or AIC for several simulations.
```{r}
plot(AIC_min[1,])
#for (i in 1:10){lines(AIC_min[i+1,])}
abline(v = 4, col = "red", lty = 2)
plot(BIC_min[1,])
#for (i in 1:10){lines(BIC_min[i+1,])}
abline(v = 4, col = "red", lty = 2)
```
In the case without correlation,
```{r}
n <- 1000 # number of subjects
# n <- 10000 # or a larger number of subjects
p <- 5 # number of explanatory variables
mu.star <- 1:p # mean of the explanatory variables
sd <- 1:p # standard deviations
# without correlation
C = diag(p)
Sigma.star <- diag(sd)%*%C%*%diag(sd) # variance-covariance matrix of the explanatory variables
beta.star <- c(0.5, 0, 1, 0, -0.6) # coefficients of logistic regression
beta0.star <- -0.2 # intercept
beta.true = c(beta0.star,beta.star)
#percentage of missingness
p.miss <- 0.10
ll = AIC_nocor = BIC_nocor = matrix(0, nrow = nb.simu, ncol = nrow(subsets)-1)
AIC_nocor_min =BIC_nocor_min = matrix(1e+5,nrow = nb.simu,ncol = p)
j_AIC_nocor = j_BIC_nocor = matrix(0,nrow = nb.simu,ncol = p)
AIC_nocor_all_min =BIC_nocor_all_min = rep(1e+5,nb.simu)
j_all_AIC_nocor = j_all_BIC_nocor = rep(0,nb.simu)
for(nb in 1:nb.simu){
set.seed(nb)
# complete data simulation
X.complete <- matrix(rnorm(n*p), nrow=n)%*%chol(Sigma.star) + matrix(rep(mu.star,n), nrow=n, byrow = TRUE)
p1 <- 1/(1+exp(-X.complete%*%beta.star-beta0.star))
y <- as.numeric(runif(n)<p1)
# generate missingness
X.obs <- X.complete
patterns = runif(n*p)<p.miss
X.obs[patterns] <- NA
# iterate among each combination
for (j in 1:(nrow(subsets)-1)){
nb.var = sum(subsets[j,])
variables = subsets[j,]
pos_var=which(variables==1)
nb.x = sum(variables)
nb.para = (nb.x + 1) + p + p*p
list.saem.subset=miss.saem(X.obs,y,pos_var,maxruns=1000,tol_em=1e-7,nmcmc=2,tau=1,k1=5,print_iter=FALSE,ll_obs_cal=TRUE)
ll[nb,j] = list.saem.subset$ll
AIC_nocor[nb,j] = -2*ll[nb,j]+ 2*nb.para
BIC_nocor[nb,j] = -2*ll[nb,j]+ nb.para * log(n)
if(AIC_nocor[nb,j]<=AIC_nocor_min[nb,nb.x]){
AIC_nocor_min[nb,nb.x]= AIC_nocor[nb,j]
j_AIC_nocor[nb,nb.x] = j
}
if(BIC_nocor[nb,j]<=BIC_nocor_min[nb,nb.x]){
BIC_nocor_min[nb,nb.x]= BIC_nocor[nb,j]
j_BIC_nocor[nb,nb.x] = j
}
if(AIC_nocor[nb,j]<=AIC_nocor_all_min[nb]){
AIC_nocor_all_min[nb]= AIC_nocor[nb,j]
j_all_AIC_nocor[nb] = j
}
if(BIC_nocor[nb,j]<=BIC_nocor_all_min[nb]){
BIC_nocor_all_min[nb]= BIC_nocor[nb,j]
j_all_BIC_nocor[nb] = j
}
}
}
```