Skip to content

Commit 1c0fb26

Browse files
committed
improve documentation (S3-methods)
version bump v0.1.14 cleanup package dependencies
1 parent 8c8e020 commit 1c0fb26

17 files changed

+725
-117
lines changed

.Rbuildignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
11
^.*\.Rproj$
22
^\.Rproj\.user$
3+
^LICENSE\.md$
4+
5+
^inst/lit$
6+
^inst/internal$
7+
^inst/pres$

DESCRIPTION

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
Package: lme4cens
22
Type: Package
33
Title: Simple Linear Mixed Effect Models for Censored Data
4-
Version: 0.1.13
4+
Version: 0.1.14
55
Author: Matthias Kuhn
66
Maintainer: Matthias Kuhn <matthias.kuhn@tu-dresden.de>
77
Description: Simple Linear Mixed Effect Models that cope with censored data.
88
It uses the formula interface of lme4. As a response variable you can use survival's Surv-objects.
9-
License: GPL-3
9+
License: GPL (>= 3)
1010
Encoding: UTF-8
1111
LazyData: true
1212
Imports:
13-
dplyr,
1413
optimx,
15-
parallel,
14+
stats,
1615
survival
1716
Depends: R (>= 3.6.0), lme4 (>= 1.1.12)
1817
Suggests:
18+
dplyr,
1919
knitr,
20+
parallel,
2021
rmarkdown,
2122
testthat
2223
VignetteBuilder: knitr
2324
Roxygen: list(markdown = TRUE)
24-
RoxygenNote: 7.1.1
25+
RoxygenNote: 7.1.2

LICENSE.md

Lines changed: 595 additions & 0 deletions
Large diffs are not rendered by default.

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
# lme4cens 0.1.14
3+
* bug fix: `ranef` failed for models with intercept as only fixed effect (#5, @moosterwegel).
4+
* clean up package dependencies
5+
* improve documentation (in particular S3 methods)
6+

R/lmod_cens.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ lmcens.objFun <- function(x, yTime1, yTime2, yStat, w, offset){
243243

244244

245245
#' @export
246-
summary.lmcens <- function(object){
246+
summary.lmcens <- function(object, ...){
247247
z <- object
248248

249249
lSigmaCoef <- z$coefficients[length(z$coefficients)]
@@ -290,12 +290,12 @@ print.summary.lmcens <- function(x, digits = max(3L, getOption("digits") - 3L),
290290
}
291291

292292
#' @export
293-
logLik.lmcens <- function(obj) obj$logLik
293+
logLik.lmcens <- function(object, ...) object$logLik
294294

295295

296296
#' Variance covariance function for [lmcens]-models.
297297
#'
298298
#' Includes the residual standard error as own parameter
299299
#' @return asymptotic variance-covariance matrix for the parameters, including residual std error as last parameter
300300
#' @export
301-
vcov.lmcens <- function(obj) solve(obj$hess)
301+
vcov.lmcens <- function(object, ...) solve(object$hess)

R/zzz.R

Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -24,98 +24,99 @@ survival::Surv
2424
# add some S3-convenience functions for q&d lmercens
2525

2626
#' @export
27-
print.lmercens <- function(obj){
27+
print.lmercens <- function(x, ...){
2828
cat("Linear Mixed Model with Censored Observations\n")
2929

30-
cat("\nCall:\n", paste(deparse(obj$call), sep = "\n", collapse = "\n"),
30+
cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"),
3131
"\n\n", sep = "")
32-
cat("\nCoefficients:\nFixed coefs: ", fixef.lmercens(obj))
33-
cat("\nRandom effect coefs: log(S_betw) = ", log(sigma(obj, which = "between")),
34-
"\t log(S_within) = ", log(sigma(obj, which = "residual")), "\n")
32+
cat("\nCoefficients:\nFixed coefs: ", fixef.lmercens(x))
33+
cat("\nRandom effect coefs: log(S_betw) = ", log(sigma(x, which = "between")),
34+
"\t log(S_within) = ", log(sigma(x, which = "residual")), "\n")
3535
}
3636

3737
#' Extract variance estimates on standard-deviation scale
3838
#' @param which which type of variance estimate
3939
#' @export
40-
sigma.lmercens <- function(obj, which = c("residual", "between"), ...){
40+
sigma.lmercens <- function(object, which = c("residual", "between"), ...){
4141
which <- match.arg(which)
4242

4343
switch(which,
44-
between = exp(obj$par[[1L]]),
44+
between = exp(object$par[[1L]]),
4545
residual = ,
46-
exp(obj$par[[2L]])
46+
exp(object$par[[2L]])
4747
)
4848
}
4949

5050

51+
#' Fixed effect coefficient estimates.
5152
#' @export
52-
fixef.lmercens <- function(obj){
53-
setNames(obj$fixef, nm = colnames(obj$ingredients$X))
53+
fixef.lmercens <- function(object, ...){
54+
stats::setNames(object$fixef, nm = colnames(object$ingredients$X))
5455
}
5556

5657
#' Random effect predictions for the random intercept.
5758
#' @export
58-
ranef.lmercens <- function(obj){
59-
stopifnot( inherits(obj, what = "lmercens") )
59+
ranef.lmercens <- function(object, ...){
60+
stopifnot( inherits(object, what = "lmercens") )
6061

6162
# use only observed cases
62-
respMatrix <- as.matrix(obj$ingredients$fr[[1L]])
63+
respMatrix <- as.matrix(object$ingredients$fr[[1L]])
6364
stopifnot( "status" %in% colnames(respMatrix) )
6465
obsInd <- respMatrix[,"status", drop=TRUE] == 1L ## eventually could use also interval-censored (=> take mean of interval)
65-
fixef_resids <- respMatrix[obsInd, 1L, drop=FALSE] - obj$ingredients$X[obsInd,, drop=FALSE] %*% fixef(obj)
66+
fixef_resids <- respMatrix[obsInd, 1L, drop=FALSE] - object$ingredients$X[obsInd,, drop=FALSE] %*% fixef.lmercens(object)
6667

6768
# cf. Demidenko, section 3.7
68-
Zt <- obj$ingredients$reTrms$Zt
69-
D <- diag(exp(2 * (obj$par[[1L]] - obj$par[[2L]])), nrow = NROW(Zt))
69+
Zt <- object$ingredients$reTrms$Zt
70+
D <- diag(exp(2 * (object$par[[1L]] - object$par[[2L]])), nrow = NROW(Zt))
7071

7172
# with random intercept only, it is enough to give out a vector (not a named list)
7273
setNames(as.numeric(D %*% Matrix::solve(a = diag(1, nrow = NROW(Zt)) + Matrix::tcrossprod(Zt) %*% D,
7374
b = Zt[, obsInd] %*% (fixef_resids))),
74-
nm = levels(obj$ingredients$reTrms$flist[[1]]))
75+
nm = levels(object$ingredients$reTrms$flist[[1]]))
7576
}
7677

7778
#' Predict method for `lmercens` objects.
7879
#'
79-
#' @param obj `lmercens` model object
80+
#' @param object `lmercens` model object
8081
#' @param newdata dataframe with covariate values for prediction. Default is `NULL` which is to fall back to the training data.
8182
#' @param re.form right-side formula for random effects to condition on in case of prediction on training data. If `NULL`, include all random effects; if `~0` or `NA`, include no random effects.
8283
#' @export
83-
predict.lmercens <- function(obj, newdata = NULL, re.form = NULL){
84-
stopifnot( inherits(obj, what = "lmercens") )
84+
predict.lmercens <- function(object, newdata = NULL, re.form = NULL, ...){
85+
stopifnot( inherits(object, what = "lmercens") )
8586

8687
ret <- NULL
8788

8889
if ( is.null(newdata) ){ # use training data itself for predictions
8990

90-
ret <- obj$ingredients$X %*% fixef(obj)
91+
ret <- object$ingredients$X %*% fixef.lmercens(object)
9192

9293
if ( is.null(re.form) || re.form == ~1 ){ ##!isTRUE(is.na(re.form)) && !re.form == ~0 ){
9394
# expect simple random effects model (only a single random intercept)
94-
stopifnot( length(obj$ingredients$reTrms$cnms) == 1L && obj$ingredients$reTrms$cnms[[1L]] == '(Intercept)' )
95+
stopifnot( length(object$ingredients$reTrms$cnms) == 1L && object$ingredients$reTrms$cnms[[1L]] == '(Intercept)' )
9596

96-
ret <- ret + Matrix::crossprod(obj$ingredients$reTrms$Zt, y = ranef(obj))
97+
ret <- ret + Matrix::crossprod(object$ingredients$reTrms$Zt, y = ranef(object))
9798
}
9899

99100
} else { # new data used without random effect BLUPs
100-
X <- model.matrix(lme4:::getFixedFormula(obj$call$formula[-2]), data = newdata[,-1])
101-
ret <- X %*% fixef(obj)
101+
X <- model.matrix(lme4:::getFixedFormula(object$call$formula[-2]), data = newdata[,-1])
102+
ret <- X %*% fixef.lmercens(object)
102103
}
103104

104105
as.vector(ret)
105106
}
106107

107108

108109
#' @export
109-
fitted.lmercens <- function(obj){
110-
predict.lmercens(obj)
110+
fitted.lmercens <- function(object, ...){
111+
predict.lmercens(object, ...)
111112
}
112113

113114
#' @export
114-
residuals.lmercens <- function(obj){
115-
stopifnot( inherits(obj, what = "lmercens") )
115+
residuals.lmercens <- function(object, ...){
116+
stopifnot( inherits(object, what = "lmercens") )
116117

117-
y_pred <- predict(obj)
118-
y_obs <- as.matrix(obj$ingredients$fr[[1L]])
118+
y_pred <- predict(object)
119+
y_obs <- as.matrix(object$ingredients$fr[[1L]])
119120
y_obs_status <- y_obs[, "status"]
120121

121122
# NA as default (for all censored outcomes)
@@ -127,14 +128,14 @@ residuals.lmercens <- function(obj){
127128
}
128129

129130
#' @export
130-
summary.lmercens <- function(obj){
131-
if (!inherits(obj, "lmercens")) warning("calling summary.lmercens(<fake-lmercens-object>) ...")
132-
ans <- obj[c("call", "ingredients", "fval")]
133-
ans$sigmas <- c(between = as.numeric(sigma(obj, which = "between")),
134-
residual = as.numeric(sigma(obj, which = "residual")))
135-
136-
est <- fixef(obj)
137-
stdError <- sqrt(diag(vcov(obj)))
131+
summary.lmercens <- function(object, ...){
132+
if (!inherits(object, "lmercens")) warning("calling summary.lmercens(<fake-lmercens-object>) ...")
133+
ans <- object[c("call", "ingredients", "fval")]
134+
ans$sigmas <- c(between = as.numeric(sigma(object, which = "between")),
135+
residual = as.numeric(sigma(object, which = "residual")))
136+
137+
est <- fixef(object)
138+
stdError <- sqrt(diag(vcov(object)))
138139
tval <- est / stdError
139140
ans$coefs <- cbind(Estimate = est, `Std. Error` = stdError,
140141
`z value` = tval,
@@ -146,46 +147,46 @@ summary.lmercens <- function(obj){
146147
}
147148

148149
#' @export
149-
print.summary.lmercens <- function(obj, ...) {
150+
print.summary.lmercens <- function(x, ...) {
150151
cat("Linear Mixed Model with Censored Observations\n")
151152

152-
cat("\nCall:\n", paste(deparse(obj$call), sep = "\n", collapse = "\n"),
153+
cat("\nCall:\n", paste(deparse(x$call), sep = "\n", collapse = "\n"),
153154
"\n\n", sep = "")
154155
cat("\nCoefficients:\nFixed coefs:\n ")
155-
coefs <- obj$coefs
156+
coefs <- x$coefs
156157
stats::printCoefmat(coefs, ...)
157158
cat("\nRandom effects:\n")
158-
cat("S_betw = ", obj$sigmas["between"], "\t S_within = ", obj$sigmas["residual"], "\n")
159-
cat("Log-likelihood: ", round(-obj$fval, 2), "\n")
159+
cat("S_betw = ", x$sigmas["between"], "\t S_within = ", x$sigmas["residual"], "\n")
160+
cat("Log-likelihood: ", round(-x$fval, 2), "\n")
160161
}
161162

162163
#' Variance-covariance matrix for fixed effect coefficients.
163164
#' It uses the observed Fisher information matrix at the ML parameter estimate.
164165
#' Therefore, it is only useful asymptotically.
165-
#' @param obj a fitted `lmercens`-object
166+
#' @param object a fitted `lmercens`-object
166167
#' @return estimated variance-covariance matrix for fixed effect coefficients
167168
#' @export
168-
vcov.lmercens <- function(obj, ...){
169-
stopifnot( inherits(obj, what = "lmercens") )
169+
vcov.lmercens <- function(object, ...){
170+
stopifnot( inherits(object, what = "lmercens") )
170171

171172
retMat <- NULL
172173

173-
if (!is.null(obj$hess) && is.matrix(obj$hess) && isSymmetric(obj$hess)){
174-
retMat <- solve(obj$hess)
175-
dimnames(retMat) <- list(colnames(obj$ingredients$X), colnames(obj$ingredients$X))
174+
if (!is.null(object$hess) && is.matrix(object$hess) && isSymmetric(object$hess)){
175+
retMat <- solve(object$hess)
176+
dimnames(retMat) <- list(colnames(object$ingredients$X), colnames(object$ingredients$X))
176177
}
177178

178179
retMat
179180
}
180181

181182

182183
#' @export
183-
confint.lmercens <- function(obj, parm, level = 0.95, ...){
184-
stopifnot( inherits(obj, "lmercens"), is.numeric(level), level > 0L, level < 1L )
184+
confint.lmercens <- function(object, parm, level = 0.95, ...){
185+
stopifnot( inherits(object, "lmercens"), is.numeric(level), level > 0L, level < 1L )
185186

186-
cf <- fixef(obj)
187+
cf <- fixef(object)
187188
pnames <- names(cf)
188-
ses <- sqrt(diag(vcov(obj)))
189+
ses <- sqrt(diag(vcov(object)))
189190

190191
if (missing(parm)) parm <- pnames else if (is.numeric(parm)) parm <- pnames[parm]
191192

man/fixef.lmercens.Rd

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/predict.lmercens.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ranef.lmercens.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/sigma.lmercens.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)