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 266 267 268 269 270 271 272 273 274 275 276 277 278
|
## $Id: estimable.R 1333 2009-05-09 05:00:47Z warnes $
estimable <- function (obj, cm, beta0, conf.int=NULL, show.beta0, ...)
{
UseMethod("estimable")
}
estimable.default <- function (obj, cm, beta0, conf.int=NULL,
show.beta0, joint.test=FALSE, ...)
{
if (is.matrix(cm) || is.data.frame(cm))
{
cm <- t(apply(cm, 1, .to.est, obj=obj))
}
else if(is.list(cm))
{
cm <- matrix(.to.est(obj, cm), nrow=1)
}
else if(is.vector(cm))
{
cm <- matrix(.to.est(obj, cm), nrow=1)
}
else
{
stop("`cm' argument must be of type vector, list, or matrix.")
}
if(missing(show.beta0))
{
if(!missing(beta0))
show.beta0=TRUE
else
show.beta0=FALSE
}
if (missing(beta0))
{
beta0 = rep(0, ifelse(is.null(nrow(cm)), 1, nrow(cm)))
}
if (joint.test==TRUE)
{
.wald(obj, cm, beta0)
}
else
{
if ("lme" %in% class(obj)) {
stat.name <- "t.stat"
cf <- summary(obj)$tTable
rho <- summary(obj)$cor
vcv <- rho * outer(cf[, 2], cf[, 2])
tmp <- cm
tmp[tmp==0] <- NA
df.all <- t(abs(t(tmp) * obj$fixDF$X))
df <- apply(df.all, 1, min, na.rm=TRUE)
problem <- apply(df.all !=df, 1, any, na.rm=TRUE)
if (any(problem))
warning(paste("Degrees of freedom vary among parameters used to ",
"construct linear contrast(s): ",
paste((1:nrow(tmp))[problem], collapse=", "),
". Using the smallest df among the set of parameters.",
sep=""))
}
else if ("lm" %in% class(obj))
{
stat.name <- "t.stat"
cf <- summary.lm(obj)$coefficients
vcv <- summary.lm(obj)$cov.unscaled * summary.lm(obj)$sigma^2
df <- obj$df.residual
if ("glm" %in% class(obj))
{
vcv <- summary(obj)$cov.scaled
if(family(obj)[1] %in% c("poisson", "binomial"))
{
stat.name <- "X2.stat"
df <- 1
}
else
{
stat.name <- "t.stat"
df <- obj$df.residual
}
}
}
else if ("geese" %in% class(obj))
{
stat.name <- "X2.stat"
cf <- summary(obj)$mean
vcv <- obj$vbeta
df <- 1
}
else if ("gee" %in% class(obj))
{
stat.name <- "X2.stat"
cf <- summary(obj)$coef
vcv <- obj$robust.variance
df <- 1
}
else
{
stop("obj must be of class 'lm', 'glm', 'aov', 'lme', 'gee', 'geese' or 'nlme'")
}
if (is.null(cm))
cm <- diag(dim(cf)[1])
if (!dim(cm)[2]==dim(cf)[1])
stop(paste("\n Dimension of ", deparse(substitute(cm)),
": ", paste(dim(cm), collapse="x"),
", not compatible with no of parameters in ",
deparse(substitute(obj)), ": ", dim(cf)[1], sep=""))
ct <- cm %*% cf[, 1]
ct.diff <- cm %*% cf[, 1] - beta0
vc <- sqrt(diag(cm %*% vcv %*% t(cm)))
if (is.null(rownames(cm)))
rn <- paste("(", apply(cm, 1, paste, collapse=" "),
")", sep="")
else rn <- rownames(cm)
switch(stat.name,
t.stat={
prob <- 2 * (1 - pt(abs(ct.diff/vc), df))
},
X2.stat={
prob <- 1 - pchisq((ct.diff/vc)^2, df=1)
})
if (stat.name=="X2.stat")
{
retval <- cbind(hyp=beta0, est=ct, stderr=vc,
"X^2 value"=(ct.diff/vc)^2,
df=df, prob=1 - pchisq((ct.diff/vc)^2, df=1))
dimnames(retval) <- list(rn, c("beta0", "Estimate", "Std. Error",
"X^2 value", "DF", "Pr(>|X^2|)"))
}
else if (stat.name=="t.stat")
{
retval <- cbind(hyp=beta0, est=ct, stderr=vc, "t value"=ct.diff/vc,
df=df, prob=2 * (1 - pt(abs(ct.diff/vc), df)))
dimnames(retval) <- list(rn, c("beta0", "Estimate", "Std. Error",
"t value", "DF", "Pr(>|t|)"))
}
if (!is.null(conf.int))
{
if (conf.int <=0 || conf.int >=1)
stop("conf.int should be between 0 and 1. Usual values are 0.95, 0.90")
alpha <- 1 - conf.int
switch(stat.name,
t.stat={
quant <- qt(1 - alpha/2, df)
},
X2.stat={
quant <- qt(1 - alpha/2, 100)
})
nm <- c(colnames(retval), "Lower.CI", "Upper.CI")
retval <- cbind(retval, lower=ct.diff - vc * quant, upper=ct.diff +
vc * quant)
colnames(retval) <- nm
}
rownames(retval) <- make.unique(rownames(retval))
retval <- as.data.frame(retval)
if(!show.beta0) retval$beta0 <- NULL
return(retval)
}
}
.wald <- function (obj, cm,
beta0=rep(0, ifelse(is.null(nrow(cm)), 1, nrow(cm))))
{
if (!is.matrix(cm) && !is.data.frame(cm))
cm <- matrix(cm, nrow=1)
df <- nrow(cm)
if ("geese" %in% class(obj))
{
cf <- obj$beta
vcv <- obj$vbeta
}
else if ("gee" %in% class(obj))
{
cf <- summary(obj)$coef
vcv <- obj$robust.variance
}
else if ("lm" %in% class(obj))
{
cf <- summary.lm(obj)$coefficients[, 1]
if ("glm" %in% class(obj))
vcv <- summary(obj)$cov.scaled
else
vcv <- summary.lm(obj)$cov.unscaled * summary.lm(obj)$sigma^2
}
else if ("lme" %in% class(obj))
{
s.o <- summary(obj)
cf <- s.o$tTable[,1]
se <- s.o$tTable[, 2]
rho <- s.o$cor
vcv <- rho * outer(se, se)
}
else
stop("obj must be of class 'lm', 'glm', 'aov', 'gee', 'geese', or 'lme'.")
u <- (cm %*% cf)-beta0
vcv.u <- cm %*% vcv %*% t(cm)
W <- t(u) %*% solve(vcv.u) %*% u
prob <- 1 - pchisq(W, df=df)
retval <- as.data.frame(cbind(W, df, prob))
names(retval) <- c("X2.stat", "DF", "Pr(>|X^2|)")
print(as.data.frame(retval))
}
estimable.mer <- function (obj, cm, beta0, conf.int=NULL, show.beta0,
sim.mer=TRUE, n.sim=1000, ...)
{
if (is.matrix(cm) || is.data.frame(cm))
{
cm <- t(apply(cm, 1, .to.est, obj=obj))
}
else if(is.list(cm))
{
cm <- matrix(.to.est(obj, cm), nrow=1)
}
else if(is.vector(cm))
{
cm <- matrix(.to.est(obj, cm), nrow=1)
}
else
{
stop("'cm' argument must be of type vector, list, or matrix.")
}
if(missing(show.beta0))
{
if(!missing(beta0))
show.beta0=TRUE
else
show.beta0=FALSE
}
if (missing(beta0))
{
beta0 = rep(0, ifelse(is.null(nrow(cm)), 1, nrow(cm)))
}
if ("mer" %in% class(obj)) {
if(sim.mer)
return(est.mer(obj=obj, cm=cm, beta0=beta0, conf.int=conf.int,
show.beta0=show.beta0, n.sim=n.sim))
stat.name <- "mer"
cf <- as.matrix(fixef(obj))
vcv <- as.matrix(vcov(obj))
df <- NA
}
else {
stop("obj is not of class mer")
}
if (is.null(rownames(cm)))
rn <- paste("(", apply(cm, 1, paste, collapse=" "),
")", sep="")
else rn <- rownames(cm)
ct <- cm %*% cf[, 1]
ct.diff <- cm %*% cf[, 1] - beta0
vc <- sqrt(diag(cm %*% vcv %*% t(cm)))
retval <- cbind(hyp=beta0, est=ct, stderr=vc, "t value"=ct.diff/vc)
dimnames(retval) <- list(rn, c("beta0", "Estimate", "Std. Error",
"t value"))
rownames(retval) <- make.unique(rownames(retval))
retval <- as.data.frame(retval)
if(!show.beta0) retval$beta0 <- NULL
return(retval)
}
|