-
Notifications
You must be signed in to change notification settings - Fork 0
/
lmestContMISS.R
178 lines (150 loc) · 4.97 KB
/
lmestContMISS.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
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
lmestContMISS <- function(responsesFormula = NULL, latentFormula = NULL,
data, index, k = 1:4, start = 0,
modSel = c("BIC", "AIC"), modBasic = 0, covariance = "EEE",
paramLatent = c("multilogit", "difflogit"),
weights = NULL, tol = 10^-10,
maxit = 5000, out_se = FALSE, output = FALSE,
parInit = list(piv = NULL, Pi = NULL,
Mu = NULL, Si = NULL,
Be = NULL, Ga = NULL),indb=NULL,
fort = TRUE, seed = NULL)
{
data <- as.data.frame(data)
if(!is.data.frame(data))
{
stop("A data.frame must be provided")
}
if(start == 2)
{
if(is.null(parInit))
{
stop("With start = 2, initial parameters must be provided")
}else if(!is.null(parInit$Mu))
{
k <- dim(parInit$Mu)[2]
}
}
if(!is.null(seed))
{
set.seed(seed)
}
k <- sort(unique(k))
nkv <- length(k)
if(length(index) !=2)
{
stop("id and time must be provided")
}
id.which <- which(names(data) == index[1])
tv.which <- which(names(data) == index[2])
if(length(id.which) == 0)
{
stop("the id column does not exist")
}
if(length(tv.which) == 0)
{
stop("the time column does not exist")
}
modSel <- match.arg(modSel, choices = eval(formals(lmestCont)$modSel))
paramLatent <- match.arg(paramLatent, choices = eval(formals(lmestCont)$paramLatent))
id <- data[,id.which]
tv <- data[,tv.which]
if(is.character(id) | is.factor(id))
{
warning("id column must be numeric. Coerced in numeric.")
id <- as.numeric(id)
}
if(is.character(tv) | is.factor(tv))
{
warning("time column must be numeric. Coerced in numeric.")
tv <- as.numeric(tv)
}
data.new <- data[,-c(id.which,tv.which), drop = FALSE]
if(is.null(responsesFormula))
{
Y <- data.new
Xmanifest <- NULL
Xinitial <- NULL
Xtrans <- NULL
}else{
temp <- getResponses(data = data.new,formula = responsesFormula)
Y <- temp$Y
Xmanifest <- temp$X
Xinitial <- NULL
Xtrans <- NULL
}
if(!is.null(latentFormula))
{
temp <- getLatent(data = data.new,latent = latentFormula, responses = responsesFormula)
Xinitial <- temp$Xinitial
Xtrans <- temp$Xtrans
}
tmp <- long2matrices.internal(Y = Y, id = id, time = tv,
Xinitial = Xinitial, Xmanifest = Xmanifest, Xtrans = Xtrans, cont = TRUE)
model <- tmp$model
Xinitial <- tmp$Xinitial
Xtrans <- tmp$Xtrans
Y <- tmp$Y
if(is.null(weights))
{
freq = tmp$freq
}else{
freq = weights
if(nrow(Y)!=length(weights)) stop("dimensions mismatch between data and weights")
}
out = vector("list",nkv)
if(!is.null(Xinitial))
{
if(any(is.na(Xinitial)))
{
stop("missing data in the covariates affecting the initial probabilities are not allowed")
}
}
if(!is.null(Xtrans))
{
if(any(is.na(Xtrans)))
{
stop("missing data in the covariates affecting the transition probabilities are not allowed")
}
}
miss = any(is.na(Y))
aicv = rep(NA,nkv)
bicv = rep(NA,nkv)
model <- paste(model, covariance, sep = "")
for(kv in 1:nkv){
out[[kv]] <- switch(model,
"LMbasiccontEEE" = (lmbasic.cont.MISS(Y = Y,k = k[kv],start = start,modBasic = modBasic,tol = tol,maxit = maxit,out_se = out_se,
piv = parInit$piv,Pi = parInit$Pi,Mu = parInit$Mu,Si = parInit$Si, fort = fort,indb=indb)),
# "LMbasiccontVVV" = (lmbasic.cont.MISS.VVV(Y = Y,k = k[kv],start = start,modBasic = modBasic,tol = tol,maxit = maxit,out_se = out_se,
# piv = parInit$piv,Pi = parInit$Pi,Mu = parInit$Mu,Si = parInit$Si, miss = miss)),
"LMlatentcont" = (lmcovlatent.cont(Y = Y,X1 = Xinitial,X2 = Xtrans,yv = freq,k = k[kv],
start = start,tol = tol,maxit = maxit,paramLatent = paramLatent,
Mu = parInit$Mu, Si = parInit$Si,Be = parInit$Be,Ga = parInit$Ga,output = output, out_se = out_se, miss = miss)))
aicv[kv] = out[[kv]]$aic
bicv[kv] = out[[kv]]$bic
}
if(modSel == "BIC")
{
best <- out[[which.min(bicv)]]
}else if(modSel == "AIC"){
best <- out[[which.min(aicv)]]
}
Bic <- bicv
Aic <- aicv
names(Bic) <- paste("k",k,sep = "=")
names(Aic) <- paste("k",k,sep = "=")
# if(nkv > 1)
# {
# Bic = bicv
# Aic = aicv
# }
out <- do.call(c, list(best,
list( Bic = Bic, Aic = Aic, call = match.call(),data = data)))
attributes(out)$responsesFormula = responsesFormula
attributes(out)$latentFormula = latentFormula
attributes(out)$whichid = id.which
attributes(out)$whichtv = tv.which
attributes(out)$id = id
attributes(out)$time = tv
class(out) <- class(best)
return(out)
}