-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsampler2.R
265 lines (233 loc) · 8.66 KB
/
sampler2.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
sampler2 <- function(data, m, where, imp, blocks, method, visitSequence,
predictorMatrix, formulas, blots, post,
fromto, printFlag, ...)
# The sampler controls the actual Gibbs sampling iteration scheme.
# This function is called by mice and mice.mids
{
is.passive <- function(string)
{
return("~" == substring(string, 1, 1))
}
handles.format <- function(fn) {
# determine whether function fn handles the `format` argument
f <- get(fn)
handles.arg(f, "format")
}
handles.arg <- function(f, a = "data") {
# determine whether function f handles argument a
if (!is.function(f)) return(FALSE)
a %in% names(formals(f))
}
from <- fromto[1]
to <- fromto[2]
maxit <- to - from + 1
r <- !is.na(data)
print(r)
# set up array for convergence checking
source("initialize.chain.R")
chainMean <- chainVar <- initialize.chain(blocks, maxit, m)
X.imp <- matrix(0, sum(is.na(data)), maxit, byrow = FALSE)
z <- 0
#print(chainMean)
## THE MAIN LOOP: GIBBS SAMPLER ##
if (maxit < 1) iteration <- 0
if (maxit >= 1) {
if (printFlag)
cat("\n iter imp variable")
for (k in from:to) {
# begin k loop : main iteration loop
iteration <- k
for (i in seq_len(m)) {
# begin i loop: repeated imputation loop
if (printFlag)
cat("\n ", iteration, " ", i)
# prepare the i'th imputation
# do not overwrite any observed data
for (h in visitSequence) {
for (j in blocks[[h]]) {
y <- data[, j]
ry <- r[, j]
wy <- where[, j]
data[(!ry) & wy, j] <- imp[[j]][(!ry)[wy], i]
}
}
# impute block-by-block
for (h in visitSequence) {
ct <- attr(blocks, "calltype")
calltype <- ifelse(length(ct) == 1, ct[1], ct[h])
b <- blocks[[h]]
if (calltype == "formula") ff <- formulas[[h]] else ff <- NULL
if (calltype == "type") type <- predictorMatrix[h, ] else type <- NULL
user <- blots[[h]]
# univariate/multivariate logic
theMethod <- method[h]
empt <- theMethod == ""
univ <- !empt && !is.passive(theMethod) &&
!handles.format(paste0("mice.impute.", theMethod))
mult <- !empt && !is.passive(theMethod) &&
handles.format(paste0("mice.impute.", theMethod))
pass <- !empt && is.passive(theMethod) && length(blocks[[h]]) == 1
if (printFlag & !empt) cat(" ", b)
## store current state
oldstate <- get("state", pos = parent.frame())
newstate <- list(it = k, im = i,
dep = h,
meth = theMethod,
log = oldstate$log)
assign("state", newstate, pos = parent.frame(), inherits = TRUE)
# (repeated) univariate imputation - type method
if (univ) {
for (j in b) {
z <- z + 1
print(z)
imp[[j]][, i] <-
sampler.univ(data = data, r = r, where = where,
type = type, formula = ff,
method = theMethod,
yname = j, k = k,
calltype = calltype,
user = user, ...)
#print(imp[[j]][, i])
X.imp[, z] <- imp[[j]][, i]
#print(x.imp)
data[(!r[, j]) & where[, j], j] <-
imp[[j]][(!r[, j])[where[, j]], i]
# optional post-processing
cmd <- post[j]
if (cmd != "") {
eval(parse(text = cmd))
data[where[, j], j] <- imp[[j]][, i]
}
}
}
# multivariate imputation - type and formula
if (mult) {
mis <- !r
mis[, setdiff(b, colnames(data))] <- FALSE
data[mis] <- NA
fm <- paste("mice.impute", theMethod, sep = ".")
if (calltype == "formula")
imputes <- do.call(fm, args = list(data = data,
formula = ff, ...))
else if (calltype == "type")
imputes <- do.call(fm, args = list(data = data,
type = type, ...))
else stop("Cannot call function of type ", calltype,
call. = FALSE)
if (is.null(imputes)) stop("No imputations from ", theMethod,
h, call. = FALSE)
for (j in names(imputes)) {
imp[[j]][, i] <- imputes[[j]]
data[!r[, j], j] <- imp[[j]][, i]
}
}
# passive imputation
if (pass) {
for (j in b) {
wy <- where[, j]
ry <- r[, j]
imp[[j]][, i] <- model.frame(as.formula(theMethod), data[wy, ],
na.action = na.pass)
data[(!ry) & wy, j] <- imp[[j]][(!ry)[wy], i]
}
}
} # end h loop (blocks)
} # end i loop (imputation number)
# store means and sd of m imputes
k2 <- k - from + 1L
if (length(visitSequence) > 0L) {
for (h in visitSequence) {
for (j in blocks[[h]]) {
if (!is.factor(data[, j])) {
chainVar[j, k2, ] <- apply(imp[[j]], 2L, var, na.rm = TRUE)
chainMean[j, k2, ] <- colMeans(as.matrix(imp[[j]]), na.rm = TRUE)
}
if (is.factor(data[, j])) {
for (mm in seq_len(m)) {
nc <- as.integer(factor(imp[[j]][, mm], levels = levels(data[, j])))
chainVar[j, k2, mm] <- var(nc, na.rm = TRUE)
chainMean[j, k2, mm] <- mean(nc, na.rm = TRUE)
}
}
}
}
}
} # end main iteration
if (printFlag) {
r <- get("loggedEvents", parent.frame(1))
ridge.used <- any(grepl("A ridge penalty", r$out))
if (ridge.used) {
cat("\n * Please inspect the loggedEvents \n")
} else {
cat("\n")
}
}
}
#return(list(iteration = maxit, imp = imp, chainMean = chainMean, chainVar = chainVar))
return(X.imp)
}
sampler.univ <- function(data, r, where, type, formula, method, yname, k,
calltype = "type", user, ...) {
j <- yname[1L]
if (calltype == "type") {
vars <- colnames(data)[type != 0]
pred <- setdiff(vars, j)
if (length(pred) > 0L) {
formula <- reformulate(pred, response = j)
formula <- update(formula, ". ~ . ")
} else
formula <- as.formula(paste0(j, " ~ 1"))
}
if (calltype == "formula") {
# move terms other than j from lhs to rhs
ymove <- setdiff(lhs(formula), j)
formula <- update(formula, paste(j, " ~ . "))
if (length(ymove) > 0L)
formula <- update(formula, paste("~ . + ", paste(ymove, collapse = "+")))
}
# get the model matrix
obtain.design <- function(data, formula = ~ .) {
mf <- model.frame(formula, data = data, na.action = na.pass)
model.matrix(formula, data = mf)
}
x <- obtain.design(data, formula)
# expand type vector to model matrix, remove intercept
if (calltype == "type") {
type <- type[labels(terms(formula))][attr(x, "assign")]
x <- x[, -1L, drop = FALSE]
names(type) <- colnames(x)
}
if (calltype == "formula") {
x <- x[, -1L, drop = FALSE]
type <- rep(1L, length = ncol(x))
names(type) <- colnames(x)
}
# define y, ry and wy
y <- data[, j]
ry <- complete.cases(x, y) & r[, j]
wy <- complete.cases(x) & where[, j]
# nothing to impute
if (all(!wy)) return(numeric(0))
check.df <- function(x, y, ry) {
# if needed, writes the df warning message to the log
df <- sum(ry) - ncol(x) - 1
mess <- paste("df set to 1. # observed cases:", sum(ry), " # predictors:", ncol(x) + 1)
if (df < 1 && sum(ry) > 0)
updateLog(out = mess, frame = 4)
}
cc <- wy[where[, j]]
if (k == 1L) check.df(x, y, ry)
# remove linear dependencies
#keep <- remove.lindep(x, y, ry, ...)
#x <- x[, keep, drop = FALSE]
#type <- type[keep]
#if (ncol(x) != length(type))
# stop("Internal error: length(type) != number of predictors")
# here we go
f <- paste("mice.impute", method, sep = ".")
imputes <- data[wy, j]
imputes[!cc] <- NA
args <- c(list(y = y, ry = ry, x = x, wy = wy, type = type), user, list(...))
imputes[cc] <- do.call(f, args = args)
imputes
}