| 1 |
## ---------------------------------------------------------------------------- |
| 2 |
## Author: Jason W. Morgan |
| 3 |
## URL: http://leftcensored.skepsi.net |
| 4 |
## Date: 2011.06.21 |
| 5 |
## |
| 6 |
## Description: This file contains functions for extracting model summaries |
| 7 |
## according to the requirements of mtable() and toLatex() available in the |
| 8 |
## memisc packages. |
| 9 |
## |
| 10 |
## Note: This code is based heavily on the examples and functions provided by |
| 11 |
## Martin Elff in his memisc package. If you find it useful, you should cite |
| 12 |
## *his* work. You can find his website here: http://www.martin-elff.net |
| 13 |
## |
| 14 |
## Copyright 2011 Jason W. Morgan |
| 15 |
## |
| 16 |
## This program is free software: you can redistribute it and/or modify it |
| 17 |
## under the terms of the GNU General Public License as published by the Free |
| 18 |
## Software Foundation, either version 3 of the License, or (at your option) |
| 19 |
## any later version. |
| 20 |
## |
| 21 |
## This program is distributed in the hope that it will be useful, but WITHOUT |
| 22 |
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 23 |
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 24 |
## more details. |
| 25 |
## |
| 26 |
## You should have received a copy of the GNU General Public License along with |
| 27 |
## this program. If not, see <http://www.gnu.org/licenses/>. |
| 28 |
## ---------------------------------------------------------------------------- |
| 29 |
|
| 30 |
## ---------------------------------------------------------------------------- |
| 31 |
## Supported model types |
| 32 |
## ---------------------------------------------------------------------------- |
| 33 |
## |
| 34 |
## coxph, survreg - Cox proportional hazards models and parametric survival |
| 35 |
## models from the survival library. |
| 36 |
## |
| 37 |
## aftreg, phreg, weibreg - parametric AFT, proportional hazards, and Weibull |
| 38 |
## models available from the eha library. |
| 39 |
## |
| 40 |
## mer - multilevel mixed effects models as provided by the lme4 package. The |
| 41 |
## model objects produced in the beta lme4a package are not supported. |
| 42 |
## |
| 43 |
## ---------------------------------------------------------------------------- |
| 44 |
|
| 45 |
## ---------------------------------------------------------------------------- |
| 46 |
## Summary functions for survival objects. |
| 47 |
## ---------------------------------------------------------------------------- |
| 48 |
|
| 49 |
setSummaryTemplate(coxph = c("Log-likelihood" = "($logLik:f#)", |
| 50 |
"AIC" = "($AIC:f#)", |
| 51 |
"BIC" = "($BIC:f#)", |
| 52 |
"N" = "($N:d)")) |
| 53 |
|
| 54 |
setSummaryTemplate(survreg = c("Log-likelihood" = "($logLik:f#)", |
| 55 |
"AIC" = "($AIC:f#)", |
| 56 |
"BIC" = "($BIC:f#)", |
| 57 |
"N" = "($N:d)")) |
| 58 |
|
| 59 |
setSummaryTemplate(phreg = c("Log-likelihood" = "($logLik:f#)", |
| 60 |
"AIC" = "($AIC:f#)", |
| 61 |
"BIC" = "($BIC:f#)", |
| 62 |
"N" = "($N:d)")) |
| 63 |
|
| 64 |
getSummary.coxph <- function(obj, alpha = 0.05, ...) { |
| 65 |
N <- obj$n |
| 66 |
|
| 67 |
## Take the info right out of the object to guarantee that I get the right |
| 68 |
## standard errors. |
| 69 |
b <- coef(obj) |
| 70 |
se <- sqrt(diag(vcov(obj))) |
| 71 |
coef <- cbind(b, se, b / se, 1-pnorm(abs(b/se))) |
| 72 |
|
| 73 |
lower <- qnorm(p = alpha/2, mean = coef[, 1], sd = coef[,2]) |
| 74 |
upper <- qnorm(p = 1 - alpha/2, mean = coef[, 1], sd = coef[,2]) |
| 75 |
|
| 76 |
coef <- cbind(coef, lower, upper) |
| 77 |
|
| 78 |
# Patched by ME 2017-09-02 |
| 79 |
dn <- list( |
| 80 |
rownames(coef), |
| 81 |
c("est","se","stat","p","lwr","upr"), |
| 82 |
rownames(attr(obj$terms,"factors"))[1] |
| 83 |
) |
| 84 |
dim(coef) <- c(dim(coef)[1],dim(coef)[2],1) |
| 85 |
dimnames(coef) <- dn |
| 86 |
|
| 87 |
ll <- obj$loglik[2] |
| 88 |
K <- length(b) |
| 89 |
|
| 90 |
AIC <- -2*ll + 2*K |
| 91 |
BIC <- -2*ll + K*log(N) |
| 92 |
|
| 93 |
sumstat <- c(logLik = ll, AIC = AIC, BIC = BIC, N = N) |
| 94 |
|
| 95 |
list(coef = coef, |
| 96 |
sumstat = sumstat, |
| 97 |
contrasts = obj$contrasts, |
| 98 |
xlevels = obj$xlevels, |
| 99 |
call = obj$call) |
| 100 |
} |
| 101 |
|
| 102 |
getSummary.survreg <- function(obj, alpha = 0.05, ...) { |
| 103 |
|
| 104 |
coef <- cbind(c(coef(obj), obj$scale), c(sqrt(diag(obj$var)),NA)) |
| 105 |
K <- length(coef) - 1 |
| 106 |
N <- obj$df.residual + K |
| 107 |
stat <- coef[, 1] / coef[, 2] |
| 108 |
pval <- 1-pnorm(abs(stat)) |
| 109 |
|
| 110 |
lower <- qnorm(p = alpha/2, mean = coef[, 1], sd = coef[,2]) |
| 111 |
upper <- qnorm(p = 1 - alpha/2, mean = coef[, 1], sd = coef[,2]) |
| 112 |
coef <- cbind(coef, stat, pval, lower, upper) |
| 113 |
|
| 114 |
# Patched by ME 2017-09-02 |
| 115 |
dn <- list( |
| 116 |
rownames(coef), |
| 117 |
c("est","se","stat","p","lwr","upr"), |
| 118 |
rownames(attr(obj$terms,"factors"))[1] |
| 119 |
) |
| 120 |
dim(coef) <- c(dim(coef)[1],dim(coef)[2],1) |
| 121 |
dimnames(coef) <- dn |
| 122 |
rownames(coef)[nrow(coef)] <- "scale" |
| 123 |
|
| 124 |
pv <- c("(Intercept)",colnames(attr(obj$terms,"factors"))) |
| 125 |
pv <- match(pv,rownames(coef),nomatch=0) |
| 126 |
distparms <- coef[-pv,,,drop=FALSE] |
| 127 |
coef <- coef[pv,,,drop=FALSE] |
| 128 |
|
| 129 |
ll <- obj$loglik[2] |
| 130 |
AIC <- -2*ll + 2*K |
| 131 |
BIC <- -2*ll + K*log(N) |
| 132 |
sumstat <- c(logLik = ll, AIC = AIC, BIC = BIC, N = N) |
| 133 |
|
| 134 |
list(coef = coef, |
| 135 |
distparms = distparms, |
| 136 |
sumstat = sumstat, |
| 137 |
contrasts = obj$contrasts, |
| 138 |
xlevels = obj$xlevels, |
| 139 |
call = obj$call) |
| 140 |
} |
| 141 |
|
| 142 |
## aftreg(), phreg(), weibreg() are part of the eha package. |
| 143 |
getSummary.aftreg <- function(obj, alpha = 0.05, ...) { |
| 144 |
|
| 145 |
N <- obj$n |
| 146 |
|
| 147 |
coef <- cbind(coef(obj), sqrt(diag(obj$var))) |
| 148 |
stat <- coef[, 1] / coef[, 2] |
| 149 |
pval <- 1-pnorm(abs(stat)) |
| 150 |
lower <- qnorm(p = alpha/2, mean = coef[, 1], sd = coef[, 2]) |
| 151 |
upper <- qnorm(p = 1 - alpha/2, mean = coef[, 1], sd = coef[, 2]) |
| 152 |
|
| 153 |
coef <- cbind(coef, stat, pval, lower, upper) |
| 154 |
|
| 155 |
# Patched by ME 2017-09-02 |
| 156 |
dn <- list( |
| 157 |
rownames(coef), |
| 158 |
c("est","se","stat","p","lwr","upr"), |
| 159 |
rownames(attr(obj$terms,"factors"))[1] |
| 160 |
) |
| 161 |
dim(coef) <- c(dim(coef)[1],dim(coef)[2],1) |
| 162 |
dimnames(coef) <- dn |
| 163 |
|
| 164 |
pv <- colnames(attr(obj$terms,"factors")) |
| 165 |
pv <- match(pv,rownames(coef)) |
| 166 |
distparms <- coef[-pv,,,drop=FALSE] |
| 167 |
coef <- coef[pv,,,drop=FALSE] |
| 168 |
|
| 169 |
ll <- obj$loglik[2] |
| 170 |
K <- length(coef[,1,]) |
| 171 |
AIC <- -2*ll + 2*K |
| 172 |
BIC <- -2*ll + K*log(N) |
| 173 |
sumstat <- c(logLik = ll, AIC = AIC, BIC = BIC, N = N) |
| 174 |
|
| 175 |
list(coef = coef, |
| 176 |
distparms = distparms, |
| 177 |
sumstat = sumstat, |
| 178 |
contrasts = obj$contrasts, |
| 179 |
xlevels = obj$xlevels, |
| 180 |
call = obj$call) |
| 181 |
} |
| 182 |
|
| 183 |
getSummary.phreg <- function(obj, alpha = 0.05, ...) { |
| 184 |
getSummary.aftreg(obj, alpha = alpha, ...) |
| 185 |
} |
| 186 |
|
| 187 |
getSummary.weibreg <- function(obj, alpha = 0.05, ...) { |
| 188 |
getSummary.aftreg(obj, alpha = alpha, ...) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|