SCM

SCM Repository

[matrix] Annotation of /pkg/R/lmer.R
ViewVC logotype

Annotation of /pkg/R/lmer.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 710 - (view) (download)
Original Path: branches/trunk-lme4/R/lmer.R

1 : bates 689 ## Methods for lmer and for the objects that it produces
2 :    
3 :     ## Some utilities
4 :    
5 :     Lind <- function(i,j) {
6 :     if (i < j) stop(paste("Index i=", i,"must be >= index j=", j))
7 :     ((i - 1) * i)/2 + j
8 : bates 446 }
9 :    
10 : bates 689 Dhalf <- function(from) {
11 :     D <- from@D
12 :     nf <- length(D)
13 :     Gp <- from@Gp
14 :     res <- array(0, rep(Gp[nf+1],2))
15 :     for (i in 1:nf) {
16 :     DD <- D[[i]]
17 :     dd <- dim(DD)
18 :     for (k in 1:dd[3]) {
19 :     mm <- array(DD[ , , k], dd[1:2])
20 :     base <- Gp[i] + (k - 1)*dd[1]
21 :     res[cbind(c(base + row(mm)), c(base + col(mm)))] <- c(mm)
22 :     }
23 :     }
24 :     res
25 :     }
26 :    
27 : bates 435 lmerControl <- # Control parameters for lmer
28 :     function(maxIter = 50,
29 :     msMaxIter = 50,
30 :     tolerance = sqrt((.Machine$double.eps)),
31 :     niterEM = 20,
32 :     msTol = sqrt(.Machine$double.eps),
33 :     msVerbose = getOption("verbose"),
34 :     PQLmaxIt = 20,
35 :     EMverbose = getOption("verbose"),
36 :     analyticGradient = TRUE,
37 :     analyticHessian=FALSE)
38 :     {
39 :     list(maxIter = maxIter,
40 :     msMaxIter = msMaxIter,
41 :     tolerance = tolerance,
42 :     niterEM = niterEM,
43 :     msTol = msTol,
44 :     msVerbose = msVerbose,
45 :     PQLmaxIt = PQLmaxIt,
46 :     EMverbose=EMverbose,
47 :     analyticHessian=analyticHessian,
48 :     analyticGradient=analyticGradient)
49 :     }
50 :    
51 : bates 689 setMethod("lmer", signature(formula = "formula", family = "missing"),
52 :     function(formula, data, family,
53 :     method = c("REML", "ML", "PQL", "Laplace", "AGQ"),
54 : bates 435 control = list(),
55 :     subset, weights, na.action, offset,
56 :     model = TRUE, x = FALSE, y = FALSE, ...)
57 :     {
58 :     # match and check parameters
59 : bates 704 method <- match.arg(method)
60 :     if (method %in% c("PQL", "Laplace", "AGQ")) {
61 :     warning(paste('Argument method = "', method,
62 :     '" is not meaningful for a linear mixed model.\n',
63 :     'Using method = "REML".\n', sep = ''))
64 :     method <- "REML"
65 :     }
66 : bates 435 controlvals <- do.call("lmerControl", control)
67 : bates 704 controlvals$REML <- REML <- method == "REML"
68 :    
69 : bates 435 if (length(formula) < 3) stop("formula must be a two-sided formula")
70 : bates 449
71 :     mf <- match.call() # create the model frame as frm
72 : bates 435 m <- match(c("data", "subset", "weights", "na.action", "offset"),
73 :     names(mf), 0)
74 :     mf <- mf[c(1, m)]
75 :     mf[[1]] <- as.name("model.frame")
76 :     frame.form <- subbars(formula)
77 :     environment(frame.form) <- environment(formula)
78 :     mf$formula <- frame.form
79 :     mf$drop.unused.levels <- TRUE
80 :     frm <- eval(mf, parent.frame())
81 : bates 449
82 : bates 435 ## grouping factors and model matrices for random effects
83 :     bars <- findbars(formula[[3]])
84 :     random <-
85 :     lapply(bars,
86 :     function(x) list(model.matrix(eval(substitute(~term,
87 :     list(term=x[[2]]))),
88 :     frm),
89 : bates 452 eval(substitute(as.factor(fac)[,drop = TRUE],
90 : bates 435 list(fac = x[[3]])), frm)))
91 :     names(random) <- unlist(lapply(bars, function(x) deparse(x[[3]])))
92 : bates 449
93 : bates 435 ## order factor list by decreasing number of levels
94 : bates 449 nlev <- sapply(random, function(x) length(levels(x[[2]])))
95 : bates 452 if (any(diff(nlev) > 0)) {
96 : bates 449 random <- random[rev(order(nlev))]
97 : bates 435 }
98 : bates 452 fixed.form <- nobars(formula)
99 :     if (!inherits(fixed.form, "formula")) fixed.form <- ~ 1 # default formula
100 : bates 571 Xmat <- model.matrix(fixed.form, frm)
101 : bates 435 mmats <- c(lapply(random, "[[", 1),
102 : bates 571 .fixed = list(cbind(Xmat, .response = model.response(frm))))
103 : bates 689 obj <- .Call("lmer_create", lapply(random, "[[", 2),
104 :     mmats, PACKAGE = "Matrix")
105 : bates 691 slot(obj, "frame") <- frm
106 : bates 689 slot(obj, "terms") <- attr(model.frame(fixed.form, data), "terms")
107 :     slot(obj, "assign") <- attr(Xmat, "assign")
108 :     slot(obj, "call") <- match.call()
109 :     slot(obj, "REML") <- REML
110 : bates 571 rm(Xmat)
111 : bates 435 .Call("lmer_initial", obj, PACKAGE="Matrix")
112 :     .Call("lmer_ECMEsteps", obj,
113 :     controlvals$niterEM,
114 :     controlvals$REML,
115 :     controlvals$EMverbose,
116 :     PACKAGE = "Matrix")
117 :     LMEoptimize(obj) <- controlvals
118 : bates 689 slot(obj, "residuals") <-
119 :     unname(model.response(frm) -
120 :     (slot(obj, "fitted") <-
121 :     .Call("lmer_fitted", obj, mmats, TRUE, PACKAGE = "Matrix")))
122 : bates 435 obj
123 :     })
124 :    
125 : bates 413 setReplaceMethod("LMEoptimize", signature(x="lmer", value="list"),
126 : bates 316 function(x, value)
127 :     {
128 :     if (value$msMaxIter < 1) return(x)
129 :     st <- ccoef(x) # starting values
130 :     nc <- x@nc
131 :     nc <- nc[1:(length(nc) - 2)]
132 :     constr <- unlist(lapply(nc, function(k) 1:((k*(k+1))/2) <= k))
133 :     fn <- function(pars) {
134 :     ccoef(x) <- pars
135 :     deviance(x, REML = value$REML)
136 :     }
137 : bates 380 gr <- if (value$analyticGradient)
138 :     function(pars) {
139 : bates 468 if (!identical(TRUE,all.equal(pars, ccoef(x)))) ccoef(x) <- pars
140 : bates 457 grad <- gradient(x, REML = value$REML, unconst = TRUE)
141 : bates 380 grad[constr] <- -grad[constr]/pars[constr]
142 :     grad
143 :     } else NULL
144 : bates 316 optimRes <- optim(st, fn, gr,
145 :     method = "L-BFGS-B",
146 :     lower = ifelse(constr, 1e-10, -Inf),
147 : bates 362 control = list(maxit = value$msMaxIter,
148 : bates 411 trace = as.integer(value$msVerbose)))
149 : bates 316 if (optimRes$convergence != 0) {
150 :     warning(paste("optim returned message",optimRes$message,"\n"))
151 :     }
152 : bates 411 ccoef(x) <- optimRes$par
153 : bates 316 return(x)
154 :     })
155 :    
156 : bates 413 setMethod("ranef", signature(object = "lmer"),
157 : bates 689 function(object, accumulate = FALSE, ...) {
158 :     val <- new("lmer.ranef",
159 :     lapply(.Call("lmer_ranef", object, PACKAGE = "Matrix"),
160 :     data.frame, check.names = FALSE),
161 :     varFac = object@bVar,
162 :     stdErr = .Call("lmer_sigma", object,
163 :     object@REML, PACKAGE = "Matrix"))
164 :     if (!accumulate || length(val@varFac) == 1) return(val)
165 :     ## check for nested factors
166 :     L <- object@L
167 :     if (any(sapply(seq(a = val), function(i) length(L[[Lind(i,i)]]@i))))
168 :     error("Require nested grouping factors to accumulate random effects")
169 :     val
170 : bates 316 })
171 :    
172 : bates 413 setMethod("fixef", signature(object = "lmer"),
173 : bates 316 function(object, ...) {
174 : bates 550 val <- .Call("lmer_fixef", object, PACKAGE = "Matrix")
175 : bates 316 val[-length(val)]
176 :     })
177 :    
178 : bates 413 setMethod("VarCorr", signature(x = "lmer"),
179 : bates 316 function(x, REML = TRUE, useScale = TRUE, ...) {
180 : bates 550 val <- .Call("lmer_variances", x, PACKAGE = "Matrix")
181 : bates 316 for (i in seq(along = val)) {
182 :     dimnames(val[[i]]) = list(x@cnames[[i]], x@cnames[[i]])
183 :     val[[i]] = as(as(val[[i]], "pdmatrix"), "corrmatrix")
184 :     }
185 :     new("VarCorr",
186 : bates 449 scale = .Call("lmer_sigma", x, REML, PACKAGE = "Matrix"),
187 : bates 316 reSumry = val,
188 :     useScale = useScale)
189 :     })
190 :    
191 : bates 413 setMethod("gradient", signature(x = "lmer"),
192 : bates 316 function(x, REML, unconst, ...)
193 : bates 449 .Call("lmer_gradient", x, REML, unconst, PACKAGE = "Matrix"))
194 : bates 316
195 : bates 449 setMethod("summary", signature(object = "lmer"),
196 :     function(object, ...)
197 :     new("summary.lmer", object, useScale = TRUE, showCorrelation = TRUE))
198 : bates 316
199 : bates 449 setMethod("show", signature(object = "lmer"),
200 :     function(object)
201 : bates 550 show(new("summary.lmer", object, useScale = TRUE,
202 :     showCorrelation = FALSE))
203 : bates 449 )
204 :    
205 :     setMethod("show", "summary.lmer",
206 : bates 316 function(object) {
207 :     fcoef <- fixef(object)
208 : bates 449 useScale <- object@useScale
209 :     corF <- as(as(vcov(object, useScale = useScale), "pdmatrix"),
210 : bates 316 "corrmatrix")
211 :     DF <- getFixDF(object)
212 :     coefs <- cbind(fcoef, corF@stdDev, DF)
213 :     nc <- object@nc
214 :     dimnames(coefs) <-
215 :     list(names(fcoef), c("Estimate", "Std. Error", "DF"))
216 : bates 449 digits <- max(3, getOption("digits") - 2)
217 :     REML <- length(object@REML) > 0 && object@REML[1]
218 :     llik <- logLik(object)
219 :     dev <- object@deviance
220 :    
221 :     rdig <- 5
222 :     cat("Linear mixed-effects model fit by ")
223 :     cat(ifelse(object@REML, "REML\n", "maximum likelihood\n") )
224 :     if (!is.null(object@call$formula)) {
225 :     cat("Formula:", deparse(object@call$formula),"\n")
226 :     }
227 :     if (!is.null(object@call$data)) {
228 :     cat(" Data:", deparse(object@call$data), "\n")
229 :     }
230 :     if (!is.null(object@call$subset)) {
231 :     cat(" Subset:",
232 :     deparse(asOneSidedFormula(object@call$subset)[[2]]),"\n")
233 :     }
234 :     print(data.frame(AIC = AIC(llik), BIC = BIC(llik),
235 :     logLik = c(llik),
236 :     MLdeviance = dev["ML"],
237 :     REMLdeviance = dev["REML"],
238 :     row.names = ""))
239 :     cat("Random effects:\n")
240 :     show(VarCorr(object))
241 :     ngrps <- lapply(object@flist, function(x) length(levels(x)))
242 :     cat(sprintf("# of obs: %d, groups: ", object@nc[length(object@nc)]))
243 :     cat(paste(paste(names(ngrps), ngrps, sep = ", "), collapse = "; "))
244 :     cat("\n")
245 :     if (!useScale)
246 :     cat("\nEstimated scale (compare to 1) ",
247 :     .Call("lmer_sigma", object, object@REML, PACKAGE = "Matrix"),
248 :     "\n")
249 :     if (nrow(coefs) > 0) {
250 :     if (useScale) {
251 :     stat <- coefs[,1]/coefs[,2]
252 :     pval <- 2*pt(abs(stat), coefs[,3], lower = FALSE)
253 :     nms <- colnames(coefs)
254 :     coefs <- cbind(coefs, stat, pval)
255 :     colnames(coefs) <- c(nms, "t value", "Pr(>|t|)")
256 :     } else {
257 :     coefs <- coefs[, 1:2, drop = FALSE]
258 :     stat <- coefs[,1]/coefs[,2]
259 :     pval <- 2*pnorm(abs(stat), lower = FALSE)
260 :     nms <- colnames(coefs)
261 :     coefs <- cbind(coefs, stat, pval)
262 :     colnames(coefs) <- c(nms, "z value", "Pr(>|z|)")
263 :     }
264 :     cat("\nFixed effects:\n")
265 :     printCoefmat(coefs, tst.ind = 4, zap.ind = 3)
266 :     if (length(object@showCorrelation) > 0 && object@showCorrelation[1]) {
267 :     rn <- rownames(coefs)
268 :     dimnames(corF) <- list(
269 :     abbreviate(rn, minlen=11),
270 :     abbreviate(rn, minlen=6))
271 :     if (!is.null(corF)) {
272 :     p <- NCOL(corF)
273 :     if (p > 1) {
274 :     cat("\nCorrelation of Fixed Effects:\n")
275 :     corF <- format(round(corF, 3), nsmall = 3)
276 :     corF[!lower.tri(corF)] <- ""
277 :     print(corF[-1, -p, drop=FALSE], quote = FALSE)
278 :     }
279 :     }
280 :     }
281 :     }
282 :     invisible(object)
283 : bates 316 })
284 :    
285 : deepayan 707
286 : bates 689 setMethod("lmer", signature(formula = "formula"),
287 :     function(formula, family, data,
288 :     method = c("REML", "ML", "PQL", "Laplace", "AGQ"),
289 :     control = list(),
290 :     subset, weights, na.action, offset,
291 :     model = TRUE, x = FALSE, y = FALSE, ...)
292 :     {
293 : bates 704 if (missing(method)) {
294 :     method <- "PQL"
295 :     } else {
296 :     method <- match.arg(method)
297 :     if (method == "ML") method <- "PQL"
298 :     if (method == "REML")
299 :     warning(paste('Argument method = "REML" is not meaningful',
300 :     'for a generalized linear mixed model.',
301 :     '\nUsing method = "PQL".\n'))
302 :     }
303 : deepayan 707 ## if (method %in% c("Laplace", "AGQ"))
304 :     ## stop("Laplace and AGQ methods not yet implemented")
305 :     if (method %in% c("AGQ"))
306 :     stop("AGQ method not yet implemented")
307 : bates 689 gVerb <- getOption("verbose")
308 :     # match and check parameters
309 :     controlvals <- do.call("lmerControl", control)
310 :     controlvals$REML <- FALSE
311 :     if (length(formula) < 3) stop("formula must be a two-sided formula")
312 :     ## initial glm fit
313 :     mf <- match.call()
314 :     m <- match(c("family", "data", "subset", "weights",
315 :     "na.action", "offset"),
316 :     names(mf), 0)
317 :     mf <- mf[c(1, m)]
318 :     mf[[1]] <- as.name("glm")
319 :     fixed.form <- nobars(formula)
320 :     if (!inherits(fixed.form, "formula")) fixed.form <- ~ 1 # default formula
321 :     environment(fixed.form) <- environment(formula)
322 :     mf$formula <- fixed.form
323 :     mf$x <- mf$model <- mf$y <- TRUE
324 :     glm.fit <- eval(mf, parent.frame())
325 :     family <- glm.fit$family
326 :     ## Note: offset is on the linear scale
327 :     offset <- glm.fit$offset
328 :     if (is.null(offset)) offset <- 0
329 :     weights <- sqrt(abs(glm.fit$prior.weights))
330 :     ## initial 'fitted' values on linear scale
331 :     etaold <- eta <- glm.fit$linear.predictors
332 :    
333 :     ## evaluation of model frame
334 :     mf$x <- mf$model <- mf$y <- mf$family <- NULL
335 :     mf$drop.unused.levels <- TRUE
336 :     this.form <- subbars(formula)
337 :     environment(this.form) <- environment(formula)
338 :     mf$formula <- this.form
339 :     mf[[1]] <- as.name("model.frame")
340 :     frm <- eval(mf, parent.frame())
341 :    
342 :     ## grouping factors and model matrices for random effects
343 :     bars <- findbars(formula[[3]])
344 :     random <-
345 :     lapply(bars,
346 :     function(x) list(model.matrix(eval(substitute(~term,
347 :     list(term=x[[2]]))),
348 :     frm),
349 :     eval(substitute(as.factor(fac)[,drop = TRUE],
350 :     list(fac = x[[3]])), frm)))
351 :     names(random) <- unlist(lapply(bars, function(x) deparse(x[[3]])))
352 :    
353 :     ## order factor list by decreasing number of levels
354 :     nlev <- sapply(random, function(x) length(levels(x[[2]])))
355 :     if (any(diff(nlev) > 0)) {
356 :     random <- random[rev(order(nlev))]
357 :     }
358 :     mmats <- c(lapply(random, "[[", 1),
359 :     .fixed = list(cbind(glm.fit$x, .response = glm.fit$y)))
360 :     ## FIXME: Use Xfrm and Xmat to get the terms and assign
361 :     ## slots, pass these to lmer_create, then destroy Xfrm, Xmat, etc.
362 : bates 691 obj <- .Call("lmer_create", lapply(random, "[[", 2),
363 :     mmats, PACKAGE = "Matrix")
364 :     slot(obj, "frame") <- frm
365 :     slot(obj, "terms") <- attr(glm.fit$model, "terms")
366 :     slot(obj, "assign") <- attr(glm.fit$x, "assign")
367 :     slot(obj, "call") <- match.call()
368 :     slot(obj, "REML") <- FALSE
369 : bates 689 rm(glm.fit)
370 :     .Call("lmer_initial", obj, PACKAGE="Matrix")
371 :     mmats.unadjusted <- mmats
372 :     mmats[[1]][1,1] <- mmats[[1]][1,1]
373 :     conv <- FALSE
374 :     firstIter <- TRUE
375 :     msMaxIter.orig <- controlvals$msMaxIter
376 :     responseIndex <- ncol(mmats$.fixed)
377 :    
378 :     for (iter in seq(length = controlvals$PQLmaxIt))
379 :     {
380 :     mu <- family$linkinv(eta)
381 :     dmu.deta <- family$mu.eta(eta)
382 :     ## weights (note: weights is already square-rooted)
383 :     w <- weights * dmu.deta / sqrt(family$variance(mu))
384 :     ## adjusted response (should be comparable to X \beta, not including offset
385 :     z <- eta - offset + (mmats.unadjusted$.fixed[, responseIndex] - mu) / dmu.deta
386 :     .Call("nlme_weight_matrix_list",
387 :     mmats.unadjusted, w, z, mmats, PACKAGE="Matrix")
388 :     .Call("lmer_update_mm", obj, mmats, PACKAGE="Matrix")
389 :     if (firstIter) {
390 :     .Call("lmer_initial", obj, PACKAGE="Matrix")
391 :     if (gVerb) cat(" PQL iterations convergence criterion\n")
392 :     }
393 :     .Call("lmer_ECMEsteps", obj,
394 :     controlvals$niterEM,
395 :     FALSE,
396 :     controlvals$EMverbose,
397 :     PACKAGE = "Matrix")
398 :     LMEoptimize(obj) <- controlvals
399 :     eta[] <- offset + ## FIXME: should the offset be here ?
400 :     .Call("lmer_fitted", obj,
401 :     mmats.unadjusted, TRUE, PACKAGE = "Matrix")
402 :     crit <- max(abs(eta - etaold)) / (0.1 + max(abs(eta)))
403 :     if (gVerb) cat(sprintf("%03d: %#11g\n", as.integer(iter), crit))
404 :     ## use this to determine convergence
405 :     if (crit < controlvals$tolerance) {
406 :     conv <- TRUE
407 :     break
408 :     }
409 :     etaold[] <- eta
410 :    
411 :     ## Changing number of iterations on second and
412 :     ## subsequent iterations.
413 :     if (firstIter)
414 :     {
415 :     controlvals$niterEM <- 2
416 :     controlvals$msMaxIter <- 10
417 :     firstIter <- FALSE
418 :     }
419 :     }
420 :     if (!conv) warning("IRLS iterations for glmm did not converge")
421 : deepayan 706 controlvals$msMaxIter <- msMaxIter.orig
422 :    
423 :    
424 : deepayan 707 ### if (TRUE) ## Laplace
425 :     ### {
426 :     ## Need to optimize L(theta, beta) using Laplace approximation
427 : deepayan 706
428 : deepayan 707 ## Things needed for that:
429 :     ##
430 :     ## 1. reduced ssclme object, offset, weighted model matrices
431 :     ## 2. facs, reduced model matrices
432 : deepayan 706
433 : deepayan 707 ## Of these, those in 2 will be fixed given theta and beta,
434 :     ## and can be thought of arguments to the L(theta, beta)
435 :     ## function. However, the ones in 1 will have the same
436 :     ## structure. So the plan is to pre-allocate them and pass
437 :     ## them in too so they can be used without creating/copying
438 :     ## them more than once
439 : deepayan 706
440 :    
441 : deepayan 707 ## reduced ssclme
442 : deepayan 706
443 : deepayan 707 reducedObj <- .Call("lmer_collapse", obj, PACKAGE = "Matrix")
444 :     reducedMmats.unadjusted <- mmats.unadjusted
445 :     reducedMmats.unadjusted$.fixed <-
446 :     reducedMmats.unadjusted$.fixed[, responseIndex, drop = FALSE]
447 :     reducedMmats <- mmats
448 :     reducedMmats$.fixed <-
449 :     reducedMmats$.fixed[, responseIndex, drop = FALSE]
450 : deepayan 706
451 : deepayan 707 ## define function that calculates bhats given theta and beta
452 : deepayan 706
453 : deepayan 707 bhat <-
454 :     function(pars = NULL) # 1:(responseIndex-1) - beta, rest - theta
455 :     {
456 :     if (is.null(pars))
457 : deepayan 706 {
458 : deepayan 707 off <- drop(mmats.unadjusted$.fixed %*%
459 :     c(fixef(obj), 0)) + offset
460 :     }
461 :     else
462 :     {
463 :     .Call("lmer_coefGets",
464 :     reducedObj,
465 :     as.double(pars[responseIndex:length(pars)]),
466 :     TRUE,
467 :     PACKAGE = "Matrix")
468 :     off <- drop(mmats.unadjusted$.fixed %*%
469 :     c(pars[1:(responseIndex-1)], 0)) + offset
470 :     }
471 :     niter <- 20
472 :     conv <- FALSE
473 : deepayan 706
474 : deepayan 707 eta <- offset +
475 :     .Call("lmer_fitted",
476 :     obj, mmats.unadjusted, TRUE,
477 :     PACKAGE = "Matrix")
478 :     etaold <- eta
479 :    
480 :     for (iter in seq(length = niter))
481 :     {
482 :     mu <- family$linkinv(eta)
483 :     dmu.deta <- family$mu.eta(eta)
484 :     w <- weights * dmu.deta / sqrt(family$variance(mu))
485 :     z <- eta - off + (reducedMmats.unadjusted$.fixed[, 1]
486 :     - mu) / dmu.deta
487 :     .Call("nlme_weight_matrix_list",
488 :     reducedMmats.unadjusted, w, z, reducedMmats,
489 :     PACKAGE="Matrix")
490 :     .Call("lmer_update_mm",
491 :     reducedObj, reducedMmats,
492 :     PACKAGE="Matrix")
493 :     eta[] <- off +
494 :     .Call("lmer_fitted", reducedObj,
495 :     reducedMmats.unadjusted, TRUE,
496 : deepayan 706 PACKAGE = "Matrix")
497 : deepayan 707 ##cat(paste("bhat Criterion:", max(abs(eta - etaold)) /
498 :     ## (0.1 + max(abs(eta))), "\n"))
499 :     ## use this to determine convergence
500 :     if (max(abs(eta - etaold)) <
501 :     (0.1 + max(abs(eta))) * controlvals$tolerance)
502 : deepayan 706 {
503 : deepayan 707 conv <- TRUE
504 :     break
505 : deepayan 706 }
506 : deepayan 707 etaold[] <- eta
507 :    
508 :     }
509 :     if (!conv) warning("iterations for bhat did not converge")
510 : deepayan 706
511 : deepayan 707 ## bhat doesn't really need to return anything, we
512 :     ## just want the side-effect of modifying reducedObj
513 :     ## In particular, we are interested in
514 :     ## ranef(reducedObj) and reducedObj@bVar (?). But
515 :     ## the mu-scale response will be useful for log-lik
516 :     ## calculations later, so return them anyway
517 : deepayan 706
518 : deepayan 707 invisible(family$linkinv(eta))
519 :     }
520 : deepayan 706
521 : deepayan 707 ## function that calculates log likelihood (the thing that
522 :     ## needs to get evaluated at each Gauss-Hermite location)
523 :    
524 :     ## log scale ? worry about details later, get the pieces in place
525 :    
526 :     ## this is for the Laplace approximation only. GH is more
527 :     ## complicated
528 :    
529 :     devLaplace <- function(pars = NULL)
530 :     {
531 :     ## FIXME: This actually returns half the deviance.
532 : deepayan 706
533 : deepayan 707 ## gets correct values of bhat and bvars. As a side
534 :     ## effect, mu now has fitted values
535 :     mu <- bhat(pars = pars)
536 :    
537 :     ## GLM family log likelihood (upto constant ?)(log scale)
538 :     ## FIXME: need to adjust for sigma^2 for appropriate models (not trivial!)
539 :    
540 :     ## Keep everything on (log) likelihood scale
541 : deepayan 706
542 : deepayan 707 ## log lik from observations given fixed and random effects
543 :     ## get deviance, then multiply by -1/2 (since deviance = -2 log lik)
544 :     ans <- -sum(family$dev.resids(y = mmats.unadjusted$.fixed[, responseIndex],
545 :     mu = mu,
546 :     wt = weights^2))/2
547 :    
548 : deepayan 710
549 :     if (is.null(getOption("laplaceinC")))
550 : deepayan 706 {
551 : deepayan 710 ranefs <- .Call("lmer_ranef", reducedObj, PACKAGE = "Matrix")
552 :     ## ans <- ans + reducedObj@devComp[2]/2 # log-determinant of Omega
553 : deepayan 706
554 : deepayan 710 Omega <- reducedObj@Omega
555 :     for (i in seq(along = ranefs))
556 :     {
557 :     ## contribution for random effects (get it working,
558 :     ## optimize later)
559 :     ## symmetrize RE variance
560 :     Omega[[i]] <- Omega[[i]] + t(Omega[[i]])
561 :     diag(Omega[[i]]) <- diag(Omega[[i]]) / 2
562 : deepayan 706
563 : deepayan 710 ## want log of `const det(Omega) exp(-1/2 b'
564 :     ## Omega b )` i.e., const + log det(Omega) - .5
565 :     ## * (b' Omega b)
566 : deepayan 706
567 : deepayan 710 ## FIXME: need to adjust for sigma^2 for appropriate
568 :     ## models (easy). These are all the b'Omega b,
569 :     ## summed as they eventually need to be. Think of
570 :     ## this as sum(rowSums((ranefs[[i]] %*% Omega[[i]])
571 :     ## * ranefs[[i]]))
572 : deepayan 706
573 : deepayan 710 ranef.loglik.det <- nrow(ranefs[[i]]) *
574 :     determinant(Omega[[i]], logarithm = TRUE)$modulus/2
575 :     ranef.loglik.re <-
576 :     -sum((ranefs[[i]] %*% Omega[[i]]) * ranefs[[i]])/2
577 :     ranef.loglik <- ranef.loglik.det + ranef.loglik.re
578 : deepayan 706
579 : deepayan 710 ## Jacobian adjustment
580 :     log.jacobian <-
581 :     sum(log(abs(apply(reducedObj@bVar[[i]],
582 :     3,
583 : deepayan 706
584 : deepayan 710 ## next line depends on
585 :     ## whether bVars are variances
586 :     ## or Cholesly factors
587 : deepayan 706
588 : deepayan 710 ## function(x) sum(diag(x)))
589 :     function(x) sum(diag( La.chol( x ) )))
590 :     )))
591 : deepayan 706
592 : deepayan 710 ## the constant terms from the r.e. and the final
593 :     ## Laplacian integral cancel out both being:
594 :     ## ranef.loglik.constant <- 0.5 * length(ranefs[[i]]) * log(2 * base::pi)
595 :    
596 :     ans <- ans + ranef.loglik + log.jacobian
597 :     }
598 : deepayan 706 }
599 : deepayan 710 else
600 :     {
601 :     ans <- ans +
602 :     .Call("lmer_laplace_devComp", reducedObj,
603 :     PACKAGE = "Matrix")
604 :     }
605 : deepayan 707 ## ans is (up to some constant) log of the Laplacian
606 :     ## approximation of the likelihood. Return it's negative
607 :     ## to be minimized
608 : deepayan 706
609 : deepayan 707 ## cat("Parameters: ")
610 :     ## print(pars)
611 :    
612 :     ## cat("Value: ")
613 :     ## print(as.double(-ans))
614 :    
615 :     -ans
616 :     }
617 :    
618 :     if (method == "Laplace")
619 :     {
620 : deepayan 708 ### Rprof() # trying to figure out if C-ifying bhat is worthwhile
621 : deepayan 707 ## no analytic gradients or hessians
622 :     optimRes <-
623 :     optim(fn = devLaplace,
624 :     par =
625 :     c(fixef(obj),
626 :     .Call("lmer_coef",
627 :     obj,
628 :     TRUE,
629 :     PACKAGE = "Matrix")),
630 :     ## WAS: coef(obj, unconst = TRUE)),
631 :     method = "BFGS", hessian = TRUE,
632 :     control = list(trace = getOption("verbose"),
633 :     reltol = controlvals$msTol,
634 :     maxit = controlvals$msMaxIter))
635 :     if (optimRes$convergence != 0)
636 :     warning("optim failed to converge")
637 :     optpars <- optimRes$par
638 :     Hessian <- optimRes$hessian
639 :    
640 :     ##fixef(obj) <- optimRes$par[seq(length = responseIndex - 1)]
641 :     if (getOption("verbose")) {
642 :     cat(paste("optim convergence code",
643 :     optimRes$convergence, "\n"))
644 :     cat("Fixed effects:\n")
645 :     print(fixef(obj))
646 :     print(optimRes$par[seq(length = responseIndex - 1)])
647 :     cat("(Unconstrained) variance coefficients:\n")
648 : deepayan 706
649 : deepayan 707 print(
650 : deepayan 706 .Call("lmer_coef",
651 :     obj,
652 :     TRUE,
653 :     PACKAGE = "Matrix"))
654 : deepayan 707
655 :     ##coef(obj, unconst = TRUE) <-
656 :     ## optimRes$par[responseIndex:length(optimRes$par)]
657 :     ##print(coef(obj, unconst = TRUE))
658 :     print( optimRes$par[responseIndex:length(optimRes$par)] )
659 : deepayan 706 }
660 : deepayan 707
661 :     ## need to calculate likelihood. also need to store
662 :     ## new estimates of fixed effects somewhere
663 :     ## (probably cannot update standard errors)
664 : deepayan 708 ### Rprof(NULL)
665 : deepayan 707 }
666 :     else
667 :     {
668 :     optpars <-
669 :     c(fixef(obj),
670 :     .Call("lmer_coef",
671 :     obj,
672 :     TRUE,
673 :     PACKAGE = "Matrix"))
674 :     Hessian <- new("matrix")
675 :     }
676 : deepayan 706
677 :    
678 : deepayan 707 ## Before finishing, we need to call devLaplace with the
679 :     ## optimum pars to get the final log likelihood (still need
680 :     ## to make sure it's the actual likelihood and not a
681 :     ## multiple). This would automatically call bhat() and hence
682 :     ## have the 'correct' random effects in reducedObj.
683 : deepayan 706
684 : deepayan 707 loglik <- devLaplace(optpars)
685 :     ##print(loglik)
686 :     ff <- optpars[1:(responseIndex-1)]
687 :     names(ff) <- names(fixef(obj))
688 : deepayan 706
689 : deepayan 707 if (!x) mmats <- list()
690 : deepayan 706
691 : deepayan 707 ### }
692 : deepayan 706
693 : bates 689 obj
694 :     })
695 :    
696 : deepayan 707
697 : bates 316 ## calculates degrees of freedom for fixed effects Wald tests
698 :     ## This is a placeholder. The answers are generally wrong. It will
699 :     ## be very tricky to decide what a 'right' answer should be with
700 :     ## crossed random effects.
701 :    
702 : bates 413 setMethod("getFixDF", signature(object="lmer"),
703 : bates 316 function(object, ...)
704 :     {
705 :     nc <- object@nc[-seq(along = object@Omega)]
706 :     p <- nc[1] - 1
707 :     n <- nc[2]
708 :     rep(n-p, p)
709 :     })
710 :    
711 : bates 446 setMethod("logLik", signature(object="lmer"),
712 :     function(object, REML = object@REML, ...) {
713 :     val <- -deviance(object, REML = REML)/2
714 :     nc <- object@nc[-seq(a = object@Omega)]
715 :     attr(val, "nall") <- attr(val, "nobs") <- nc[2]
716 : bates 689 attr(val, "df") <- nc[1] + length(ccoef(object))
717 : bates 446 attr(val, "REML") <- REML
718 :     class(val) <- "logLik"
719 :     val
720 :     })
721 :    
722 :     setMethod("anova", signature(object = "lmer"),
723 :     function(object, ...)
724 :     {
725 :     mCall <- match.call(expand.dots = TRUE)
726 :     dots <- list(...)
727 :     modp <- logical(0)
728 :     if (length(dots))
729 :     modp <- sapply(dots, inherits, "lmer") | sapply(dots, inherits, "lm")
730 :     if (any(modp)) { # multiple models - form table
731 :     opts <- dots[!modp]
732 :     mods <- c(list(object), dots[modp])
733 :     names(mods) <- sapply(as.list(mCall)[c(FALSE, TRUE, modp)], as.character)
734 :     mods <- mods[order(sapply(lapply(mods, logLik, REML = FALSE), attr, "df"))]
735 :     calls <- lapply(mods, slot, "call")
736 :     data <- lapply(calls, "[[", "data")
737 :     if (any(data != data[[1]])) stop("all models must be fit to the same data object")
738 :     header <- paste("Data:", data[[1]])
739 :     subset <- lapply(calls, "[[", "subset")
740 :     if (any(subset != subset[[1]])) stop("all models must use the same subset")
741 :     if (!is.null(subset[[1]]))
742 :     header <-
743 :     c(header, paste("Subset", deparse(subset[[1]]), sep = ": "))
744 :     llks <- lapply(mods, logLik, REML = FALSE)
745 :     Df <- sapply(llks, attr, "df")
746 :     llk <- unlist(llks)
747 :     chisq <- 2 * pmax(0, c(NA, diff(llk)))
748 :     dfChisq <- c(NA, diff(Df))
749 :     val <- data.frame(Df = Df,
750 :     AIC = sapply(llks, AIC),
751 :     BIC = sapply(llks, BIC),
752 :     logLik = llk,
753 :     "Chisq" = chisq,
754 :     "Chi Df" = dfChisq,
755 :     "Pr(>Chisq)" = pchisq(chisq, dfChisq, lower = FALSE),
756 :     check.names = FALSE)
757 :     class(val) <- c("anova", class(val))
758 :     attr(val, "heading") <-
759 : bates 690 c(header, "Models:",
760 : bates 446 paste(names(mods),
761 :     unlist(lapply(lapply(calls, "[[", "formula"), deparse)),
762 : bates 690 sep = ": "))
763 : bates 446 return(val)
764 :     } else {
765 : bates 571 foo <- object
766 :     foo@status["factored"] <- FALSE
767 :     .Call("lmer_factor", foo, PACKAGE="Matrix")
768 :     dfr <- getFixDF(foo)
769 :     rcol <- ncol(foo@RXX)
770 :     ss <- foo@RXX[ , rcol]^2
771 :     ssr <- ss[[rcol]]
772 :     ss <- ss[seq(along = dfr)]
773 :     names(ss) <- object@cnames[[".fixed"]][seq(along = dfr)]
774 :     asgn <- foo@assign
775 :     terms <- foo@terms
776 :     nmeffects <- attr(terms, "term.labels")
777 :     if ("(Intercept)" %in% names(ss))
778 :     nmeffects <- c("(Intercept)", nmeffects)
779 :     ss <- unlist(lapply(split(ss, asgn), sum))
780 :     df <- unlist(lapply(split(asgn, asgn), length))
781 :     dfr <- unlist(lapply(split(dfr, asgn), function(x) x[1]))
782 :     ms <- ss/df
783 :     f <- ms/(ssr/dfr)
784 :     P <- pf(f, df, dfr, lower.tail = FALSE)
785 :     table <- data.frame(df, ss, ms, dfr, f, P)
786 :     dimnames(table) <-
787 :     list(nmeffects,
788 :     c("Df", "Sum Sq", "Mean Sq", "Denom", "F value", "Pr(>F)"))
789 :     if ("(Intercept)" %in% nmeffects) table <- table[-1,]
790 :     attr(table, "heading") <- "Analysis of Variance Table"
791 :     class(table) <- c("anova", "data.frame")
792 :     table
793 : bates 446 }
794 : bates 316 })
795 : bates 446
796 :     setMethod("update", signature(object = "lmer"),
797 :     function(object, formula., ..., evaluate = TRUE)
798 :     {
799 :     call <- object@call
800 :     if (is.null(call))
801 :     stop("need an object with call component")
802 :     extras <- match.call(expand.dots = FALSE)$...
803 :     if (!missing(formula.))
804 :     call$formula <- update.formula(formula(object), formula.)
805 :     if (length(extras) > 0) {
806 :     existing <- !is.na(match(names(extras), names(call)))
807 :     for (a in names(extras)[existing]) call[[a]] <- extras[[a]]
808 :     if (any(!existing)) {
809 :     call <- c(as.list(call), extras[!existing])
810 :     call <- as.call(call)
811 :     }
812 :     }
813 :     if (evaluate)
814 :     eval(call, parent.frame())
815 :     else call
816 :     })
817 :    
818 :    
819 :     setMethod("confint", signature(object = "lmer"),
820 :     function (object, parm, level = 0.95, ...)
821 :     {
822 :     cf <- fixef(object)
823 :     pnames <- names(cf)
824 :     if (missing(parm))
825 :     parm <- seq(along = pnames)
826 :     else if (is.character(parm))
827 :     parm <- match(parm, pnames, nomatch = 0)
828 :     a <- (1 - level)/2
829 :     a <- c(a, 1 - a)
830 :     pct <- paste(round(100 * a, 1), "%")
831 :     ci <- array(NA, dim = c(length(parm), 2),
832 :     dimnames = list(pnames[parm], pct))
833 :     ses <- sqrt(diag(vcov(object)))[parm]
834 : bates 449 ci[] <- cf[parm] + ses * t(outer(a, getFixDF(object)[parm], qt))
835 : bates 446 ci
836 :     })
837 :    
838 : bates 689 setMethod("param", signature(object = "lmer"),
839 : bates 449 function(object, unconst = FALSE, ...) {
840 :     .Call("lmer_coef", object, unconst, PACKAGE = "Matrix")
841 :     })
842 : bates 446
843 : bates 449 setMethod("deviance", "lmer",
844 :     function(object, REML = NULL, ...) {
845 :     .Call("lmer_factor", object, PACKAGE = "Matrix")
846 :     if (is.null(REML))
847 :     REML <- if (length(oR <- object@REML)) oR else FALSE
848 :     object@deviance[[ifelse(REML, "REML", "ML")]]
849 :     })
850 : bates 446
851 : bates 449 setMethod("chol", signature(x = "lmer"),
852 :     function(x, pivot = FALSE, LINPACK = pivot) {
853 :     x@status["factored"] <- FALSE # force a decomposition
854 :     .Call("lmer_factor", x, PACKAGE = "Matrix")
855 :     })
856 :    
857 :     setMethod("solve", signature(a = "lmer", b = "missing"),
858 :     function(a, b, ...)
859 : bates 562 .Call("lmer_invert", a, PACKAGE = "Matrix")
860 : bates 449 )
861 :    
862 :     setMethod("formula", "lmer", function(x, ...) x@call$formula)
863 :    
864 :     setMethod("vcov", signature(object = "lmer"),
865 :     function(object, REML = object@REML, useScale = TRUE,...) {
866 :     sc <- .Call("lmer_sigma", object, REML, PACKAGE = "Matrix")
867 :     rr <- object@RXX
868 :     nms <- object@cnames[[".fixed"]]
869 :     dimnames(rr) <- list(nms, nms)
870 :     nr <- nrow(rr)
871 :     rr <- rr[-nr, -nr, drop = FALSE]
872 :     rr <- rr %*% t(rr)
873 :     if (useScale) {
874 :     rr = sc^2 * rr
875 :     }
876 :     rr
877 :     })
878 :    
879 : bates 550 ## Extract the L matrix
880 :     setAs("lmer", "dtTMatrix",
881 :     function(from)
882 :     {
883 :     ## force a refactorization if the factors have been inverted
884 :     if (from@status["inverted"]) from@status["factored"] <- FALSE
885 :     .Call("lmer_factor", from, PACKAGE = "Matrix")
886 :     L <- lapply(from@L, as, "dgTMatrix")
887 :     nf <- length(from@D)
888 :     Gp <- from@Gp
889 :     nL <- Gp[nf + 1]
890 : bates 562 Li <- integer(0)
891 :     Lj <- integer(0)
892 :     Lx <- double(0)
893 : bates 550 for (i in 1:nf) {
894 :     for (j in 1:i) {
895 :     Lij <- L[[Lind(i, j)]]
896 : bates 562 Li <- c(Li, Lij@i + Gp[i])
897 :     Lj <- c(Lj, Lij@j + Gp[j])
898 :     Lx <- c(Lx, Lij@x)
899 : bates 550 }
900 :     }
901 : bates 562 new("dtTMatrix", Dim = as.integer(c(nL, nL)), i = Li, j = Lj, x = Lx,
902 : bates 550 uplo = "L", diag = "U")
903 :     })
904 : bates 562
905 :     ## Extract the ZZX matrix
906 :     setAs("lmer", "dsTMatrix",
907 :     function(from)
908 :     {
909 :     .Call("lmer_inflate", from, PACKAGE = "Matrix")
910 :     ZZpO <- lapply(from@ZZpO, as, "dgTMatrix")
911 :     ZZ <- lapply(from@ZtZ, as, "dgTMatrix")
912 :     nf <- length(ZZpO)
913 :     Gp <- from@Gp
914 :     nZ <- Gp[nf + 1]
915 :     Zi <- integer(0)
916 :     Zj <- integer(0)
917 :     Zx <- double(0)
918 :     for (i in 1:nf) {
919 :     ZZpOi <- ZZpO[[i]]
920 :     Zi <- c(Zi, ZZpOi@i + Gp[i])
921 :     Zj <- c(Zj, ZZpOi@j + Gp[i])
922 :     Zx <- c(Zx, ZZpOi@x)
923 :     if (i > 1) {
924 :     for (j in 1:(i-1)) {
925 :     ZZij <- ZZ[[Lind(i, j)]]
926 :     ## off-diagonal blocks are transposed
927 :     Zi <- c(Zi, ZZij@j + Gp[j])
928 :     Zj <- c(Zj, ZZij@i + Gp[i])
929 :     Zx <- c(Zx, ZZij@x)
930 :     }
931 :     }
932 :     }
933 :     new("dsTMatrix", Dim = as.integer(c(nZ, nZ)), i = Zi, j = Zj, x = Zx,
934 :     uplo = "U")
935 :     })
936 : bates 689
937 :     setMethod("fitted", signature(object = "lmer"),
938 : bates 691 function(object, ...)
939 :     napredict(attr(object@frame, "na.action"), object@fitted))
940 : bates 689
941 :     setMethod("residuals", signature(object = "lmer"),
942 : bates 691 function(object, ...)
943 :     naresid(attr(object@frame, "na.action"), object@residuals))
944 : bates 689
945 :     setMethod("resid", signature(object = "lmer"),
946 :     function(object, ...) do.call("residuals", c(list(object), list(...))))
947 :    
948 :     setMethod("coef", signature(object = "lmer"),
949 :     function(object, ...)
950 :     {
951 :     fef <- data.frame(rbind(fixef(object)), check.names = FALSE)
952 :     ref <- as(ranef(object), "list")
953 :     names(ref) <- names(object@flist)
954 :     val <- lapply(ref, function(x) fef[rep(1, nrow(x)),])
955 :     for (i in seq(a = val)) {
956 :     refi <- ref[[i]]
957 :     row.names(val[[i]]) <- row.names(refi)
958 :     if (!all(names(refi) %in% names(fef)))
959 :     stop("unable to align random and fixed effects")
960 :     val[[i]][ , names(refi)] <- val[[i]][ , names(refi)] + refi
961 :     }
962 :     new("lmer.coef", val)
963 :     })
964 :    
965 :     setMethod("plot", signature(x = "lmer.coef"),
966 :     function(x, y, ...)
967 :     {
968 :     varying <- unique(do.call("c",
969 :     lapply(x, function(el)
970 :     names(el)[sapply(el,
971 :     function(col)
972 :     any(col != col[1]))])))
973 :     gf <- do.call("rbind", lapply(x, "[", j = varying))
974 :     gf$.grp <- factor(rep(names(x), sapply(x, nrow)))
975 :     switch(min(length(varying), 3),
976 :     qqmath(eval(substitute(~ x | .grp,
977 :     list(x = as.name(varying[1])))), gf, ...),
978 :     xyplot(eval(substitute(y ~ x | .grp,
979 :     list(y = as.name(varying[1]),
980 :     x = as.name(varying[2])))), gf, ...),
981 :     splom(~ gf | .grp, ...))
982 :     })
983 :    
984 :     setMethod("plot", signature(x = "lmer.ranef"),
985 :     function(x, y, ...)
986 :     {
987 :     lapply(x, function(x) {
988 :     cn <- lapply(colnames(x), as.name)
989 :     switch(min(ncol(x), 3),
990 :     qqmath(eval(substitute(~ x, list(x = cn[[1]]))), x, ...),
991 :     xyplot(eval(substitute(y ~ x, list(y = cn[[1]], x = cn[[2]]))),
992 :     x, ...),
993 :     splom(~ x, ...))
994 :     })
995 :     })
996 :    
997 :     setMethod("with", signature(data = "lmer"),
998 : bates 690 function(data, expr, ...) {
999 : bates 691 dat <- eval(data@call$data)
1000 :     if (!is.null(na.act <- attr(data@frame, "na.action")))
1001 :     dat <- dat[-na.act, ]
1002 :     lst <- c(list(. = data), data@flist, data@frame, dat)
1003 :     eval(substitute(expr), lst[unique(names(lst))])
1004 :     })
1005 : bates 690
1006 : bates 691 setMethod("terms", signature(x = "lmer"),
1007 :     function(x, ...) x@terms)

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge