| 1 |
str_has <- function(text,has,not=NULL,how=c("all","any")){ |
| 2 |
how <- match.fun(match.arg(how)) |
| 3 |
|
| 4 |
hasit <- sapply(has,function(pat)regexpr(pat,text,fixed=TRUE) > 0) |
| 5 |
if(is.matrix(hasit)) |
| 6 |
hasit <- apply(hasit,1,how) |
| 7 |
else |
| 8 |
hasit <- all(hasit) |
| 9 |
|
| 10 |
|
| 11 |
if(!length(not)) return(hasit) |
| 12 |
# else |
| 13 |
hasnot <- sapply(not,function(pat)regexpr(pat,text,fixed=TRUE) > 0) |
| 14 |
if(is.matrix(hasnot)) |
| 15 |
hasnot <- apply(hasnot,1,how) |
| 16 |
else |
| 17 |
hasnot <- all(hasnot) |
| 18 |
|
| 19 |
hasit & !hasnot |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
setCoefTemplate <- function(...){ |
| 25 |
args <- list(...) |
| 26 |
argnames <- names(args) |
| 27 |
CoefTemplates <- get("CoefTemplates", envir=.memiscEnv) |
| 28 |
OldCoefTemplates <- CoefTemplates |
| 29 |
for(coef.style in argnames){ |
| 30 |
CoefTemplates[[coef.style]] <- args[[coef.style]] |
| 31 |
} |
| 32 |
assign("CoefTemplates",CoefTemplates, envir=.memiscEnv) |
| 33 |
return(invisible(OldCoefTemplates)) |
| 34 |
} |
| 35 |
|
| 36 |
getFirstMatch <- function(x,n){ |
| 37 |
for(n. in n){ |
| 38 |
if(n. %in% names(x)) return(x[[n.]]) |
| 39 |
} |
| 40 |
return(x[["default"]]) |
| 41 |
} |
| 42 |
|
| 43 |
getCoefTemplate <- function(style){ |
| 44 |
CoefTemplates <- get("CoefTemplates", envir=.memiscEnv) |
| 45 |
if(missing(style)) return(CoefTemplates) |
| 46 |
else return(CoefTemplates[[style]]) |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
getSummary <- function(obj,alpha=.05,...) UseMethod("getSummary") |
| 51 |
# setGeneric("getSummary") |
| 52 |
|
| 53 |
summaryTemplate <- function(x) |
| 54 |
UseMethod("summaryTemplate") |
| 55 |
|
| 56 |
getFirstS3method <- function(mname,cls,optional){ |
| 57 |
for(cls1 in cls){ |
| 58 |
mfun <- getS3method(mname,cls1,optional) |
| 59 |
if(length(mfun)) return(mfun) |
| 60 |
} |
| 61 |
return(NULL) |
| 62 |
} |
| 63 |
|
| 64 |
getSummaryTemplate <- function(x){ |
| 65 |
SummaryTemplates <- get("SummaryTemplates", envir=.memiscEnv) |
| 66 |
if(missing(x)) return(SummaryTemplates) |
| 67 |
if(is.character(x)) cls <- x |
| 68 |
else cls <- class(x) |
| 69 |
stf <- getFirstS3method("summaryTemplate",cls,optional=TRUE) |
| 70 |
if(length(stf)) |
| 71 |
res <- stf(x) |
| 72 |
else |
| 73 |
res <- getFirstMatch(SummaryTemplates,cls) |
| 74 |
return(res) |
| 75 |
} |
| 76 |
|
| 77 |
setSummaryTemplate <- function(...){ |
| 78 |
args <- list(...) |
| 79 |
argnames <- names(args) |
| 80 |
OldSummaryTemplates <- SummaryTemplates <- get("SummaryTemplates", envir=.memiscEnv) |
| 81 |
for(cls in argnames){ |
| 82 |
SummaryTemplates[[cls]] <- args[[cls]] |
| 83 |
} |
| 84 |
assign("SummaryTemplates",SummaryTemplates,envir=.memiscEnv) |
| 85 |
return(invisible(OldSummaryTemplates)) |
| 86 |
} |
| 87 |
|
| 88 |
selectSummaryStats <- function(x,n) { |
| 89 |
if(is.character(n)){ |
| 90 |
n |
| 91 |
} |
| 92 |
else if(isTRUE(n)){ |
| 93 |
cls <- class(x) |
| 94 |
sumstats.name <- paste0("summary.stats.",cls) |
| 95 |
sumstats <- lapply(sumstats.name,getOption) |
| 96 |
if(any(!vapply(sumstats, is.null, TRUE))){ |
| 97 |
sumstats <- unlist(sumstats) |
| 98 |
sumstats[1] |
| 99 |
} |
| 100 |
else |
| 101 |
sumstats <- getOption("summary.stats.default") |
| 102 |
sumstats |
| 103 |
} |
| 104 |
else FALSE |
| 105 |
} |
| 106 |
|
| 107 |
prettyNames <- function(coefnames, |
| 108 |
contrasts, |
| 109 |
xlevels, |
| 110 |
factor.style, |
| 111 |
show.baselevel, |
| 112 |
baselevel.sep |
| 113 |
){ |
| 114 |
termorders <- sapply(strsplit(coefnames,":",fixed=TRUE),length) |
| 115 |
ordergroups <- split(coefnames,termorders) |
| 116 |
ordergroups <- lapply(ordergroups,prettyNames1, |
| 117 |
contrasts=contrasts, |
| 118 |
xlevels=xlevels, |
| 119 |
factor.style=factor.style, |
| 120 |
show.baselevel=show.baselevel, |
| 121 |
baselevel.sep=baselevel.sep |
| 122 |
) |
| 123 |
unsplit(ordergroups,termorders) |
| 124 |
} |
| 125 |
|
| 126 |
prettyNames1 <- function(str, |
| 127 |
contrasts, |
| 128 |
xlevels, |
| 129 |
factor.style, |
| 130 |
show.baselevel, |
| 131 |
baselevel.sep |
| 132 |
){ |
| 133 |
str <- gsub(":"," x ",str,fixed=TRUE) |
| 134 |
for(f in names(contrasts)){ |
| 135 |
contrast.f <- contrasts[[f]] |
| 136 |
levels <- xlevels[[f]] |
| 137 |
#if(!length(levels)) levels <- c("FALSE","TRUE") |
| 138 |
if(!length(levels)) { |
| 139 |
str <- gsub(paste(f,"TRUE",sep=""),f,str,fixed=TRUE) |
| 140 |
next |
| 141 |
} |
| 142 |
if(is.character(contrast.f)) |
| 143 |
contrast.matrix <- do.call(contrast.f,list(n=levels)) |
| 144 |
else if(is.matrix(contrast.f)) |
| 145 |
contrast.matrix <- contrast.f |
| 146 |
levels.present <- sapply(levels,function(level) |
| 147 |
any(str_has(str,c(f,level))) |
| 148 |
) |
| 149 |
if(all(levels.present)) |
| 150 |
oldlabels <- newlabels <- levels |
| 151 |
else if(!length(colnames(contrast.matrix))){ |
| 152 |
oldlabels <- newlabels <- as.character(1:ncol(contrast.matrix)) |
| 153 |
} |
| 154 |
else if(is.character(contrast.f) && |
| 155 |
contrast.f %in% c( |
| 156 |
"contr.treatment", |
| 157 |
"contr.SAS" |
| 158 |
)){ |
| 159 |
baselevel <- setdiff(rownames(contrast.matrix),colnames(contrast.matrix)) |
| 160 |
if(show.baselevel) |
| 161 |
newlabels <- paste(colnames(contrast.matrix),baselevel,sep=baselevel.sep) |
| 162 |
else |
| 163 |
newlabels <- colnames(contrast.matrix) |
| 164 |
oldlabels <- colnames(contrast.matrix) |
| 165 |
} |
| 166 |
else if(is.character(contrast.f) && |
| 167 |
contrast.f %in% c( |
| 168 |
"contr.sum", |
| 169 |
"contr.helmert" |
| 170 |
)){ |
| 171 |
newlabels <- apply(contrast.matrix,2, |
| 172 |
function(x)rownames(contrast.matrix)[x>=1]) |
| 173 |
oldlabels <- colnames(contrast.matrix) |
| 174 |
} |
| 175 |
else if( |
| 176 |
all(colnames(contrast.matrix) %in% rownames(contrast.matrix)) |
| 177 |
){ |
| 178 |
baselevel <- setdiff(rownames(contrast.matrix),colnames(contrast.matrix)) |
| 179 |
if(show.baselevel) |
| 180 |
newlabels <- paste(colnames(contrast.matrix),baselevel,sep=baselevel.sep) |
| 181 |
else |
| 182 |
newlabels <- colnames(contrast.matrix) |
| 183 |
oldlabels <- colnames(contrast.matrix) |
| 184 |
} |
| 185 |
else { |
| 186 |
oldlabels <- newlabels <- colnames(contrast.matrix) |
| 187 |
} |
| 188 |
from <- paste(f,oldlabels,sep="") |
| 189 |
to <- sapply(newlabels, |
| 190 |
function(l)applyTemplate(c(f=f,l=l),template=factor.style)) |
| 191 |
for(i in 1:length(from)) |
| 192 |
str <- gsub(from[i],to[i],str,fixed=TRUE) |
| 193 |
} |
| 194 |
str |
| 195 |
} |
| 196 |
|
| 197 |
bind_arrays <- function(args,along=1){ |
| 198 |
along.dn <- unlist(lapply(args,function(x)dimnames(x)[[along]])) |
| 199 |
groups <- sapply(args,function(x)dim(x)[along]) |
| 200 |
dn <- dimnames(args[[1]]) |
| 201 |
keep.dn <- dn[-along] |
| 202 |
dim1 <- dim(args[[1]]) |
| 203 |
keep.dim <- dim1[-along] |
| 204 |
ldim <- length(dim1) |
| 205 |
dimseq <- seq_len(ldim) |
| 206 |
perm.to <- dimseq |
| 207 |
perm.to[ldim] <- along |
| 208 |
perm.to[along] <- ldim |
| 209 |
res <- lapply(args,function(x){ |
| 210 |
x <- aperm(x,perm.to) |
| 211 |
dim(x) <- c(prod(dim(x)[-ldim]),dim(x)[ldim]) |
| 212 |
x |
| 213 |
}) |
| 214 |
res <- do.call(cbind,res) |
| 215 |
dim(res) <- c(keep.dim,ncol(res)) |
| 216 |
dimnames(res) <- c(keep.dn,list(along.dn)) |
| 217 |
structure(aperm(res,perm.to),groups=groups) |
| 218 |
} |
| 219 |
|
| 220 |
names.or.rownames <- function(x){ |
| 221 |
if(is.array(x)) rownames(x) |
| 222 |
else names(x) |
| 223 |
} |
| 224 |
|
| 225 |
mtable <- function(..., |
| 226 |
coef.style=getOption("coef.style"), |
| 227 |
summary.stats=TRUE, |
| 228 |
signif.symbols=getOption("signif.symbols"), |
| 229 |
factor.style=getOption("factor.style"), |
| 230 |
show.baselevel=getOption("show.baselevel"), |
| 231 |
baselevel.sep=getOption("baselevel.sep"), |
| 232 |
getSummary=eval.parent(quote(getSummary)), |
| 233 |
float.style=getOption("float.style"), |
| 234 |
digits=min(3,getOption("digits")), |
| 235 |
sdigits=digits, |
| 236 |
show.eqnames=getOption("mtable.show.eqnames",NA), |
| 237 |
gs.options=NULL, |
| 238 |
controls=NULL, |
| 239 |
collapse.controls=FALSE, |
| 240 |
control.var.indicator=getOption("control.var.indicator",c("Yes","No")) |
| 241 |
){ |
| 242 |
args <- list(...) |
| 243 |
if(length(args)==1 && inherits(args[[1]],"by")) |
| 244 |
args <- args[[1]] |
| 245 |
argnames <- names(args) |
| 246 |
if(!length(argnames)) { |
| 247 |
m <- match.call(expand.dots=FALSE) |
| 248 |
argnames <- sapply(m$...,paste) |
| 249 |
} |
| 250 |
n.args <- length(args) |
| 251 |
|
| 252 |
arg.classes <- lapply(args,class) |
| 253 |
if(any(sapply(arg.classes,length))==0) stop("don\'t know how to handle these arguments") |
| 254 |
|
| 255 |
if(length(gs.options)){ |
| 256 |
summaries.call <- as.call( |
| 257 |
c(list(as.name("lapply"), |
| 258 |
as.name("args"), |
| 259 |
FUN=as.name("getSummary")), |
| 260 |
gs.options |
| 261 |
)) |
| 262 |
summaries <- eval(summaries.call) |
| 263 |
} |
| 264 |
else |
| 265 |
summaries <- lapply(args,getSummary) |
| 266 |
|
| 267 |
# parameter.types <- unique(unlist(lapply(summaries,names))) |
| 268 |
# parameter.types <- parameter.types[parameter.types %nin% c("sumstat","contrasts","call","xlevels")] |
| 269 |
parameter.types <- unique(unlist(lapply(summaries,`[[`,"parameter.types"))) |
| 270 |
parameter.types <- c("coef",parameter.types) |
| 271 |
parmnames <- list() |
| 272 |
for(pt in parameter.types){ |
| 273 |
|
| 274 |
tmp.pn <- lapply(summaries,`[[`,pt) |
| 275 |
tmp.pn <- lapply(tmp.pn,names.or.rownames) |
| 276 |
parmnames[[pt]] <- unique(unlist(tmp.pn)) |
| 277 |
} |
| 278 |
parameter.names <- unique(unlist(parmnames)) |
| 279 |
|
| 280 |
stemplates <- lapply(args,getSummaryTemplate) |
| 281 |
if(isTRUE(summary.stats)) |
| 282 |
summary.stats <- lapply(args,selectSummaryStats,TRUE) |
| 283 |
else if(is.character(summary.stats)) |
| 284 |
summary.stats <- lapply(args,selectSummaryStats,summary.stats) |
| 285 |
else if(is.list(summary.stats)){ |
| 286 |
tmp.summary.stats <- summary.stats |
| 287 |
summary.stats <- vector(mode="list",length=length(args)) |
| 288 |
summary.stats[] <- tmp.summary.stats |
| 289 |
} else { |
| 290 |
summary.stats <- vector(mode="list",length=length(args)) |
| 291 |
summary.stats[] <- list(FALSE) |
| 292 |
} |
| 293 |
|
| 294 |
if(length(controls)){ |
| 295 |
if(is.character(controls)) |
| 296 |
controls <- asOneSidedFormula(controls) |
| 297 |
if(inherits(controls,"formula")){ |
| 298 |
control.coefs <- lapply(args,formula2coefs,fo=controls) |
| 299 |
control.terms <- lapply(args,formula2termlabs,fo=controls) |
| 300 |
} |
| 301 |
else |
| 302 |
stop("'controls=' must be a formula or a character vector.") |
| 303 |
controls <- list(coefs=control.coefs,terms=control.terms) |
| 304 |
} |
| 305 |
|
| 306 |
structure(summaries, |
| 307 |
names=argnames, |
| 308 |
class="memisc_mtable", |
| 309 |
parameter.names=parameter.names, |
| 310 |
coef.style=coef.style, |
| 311 |
summary.stats=summary.stats, |
| 312 |
signif.symbols=signif.symbols, |
| 313 |
factor.style=factor.style, |
| 314 |
show.baselevel=show.baselevel, |
| 315 |
baselevel.sep=baselevel.sep, |
| 316 |
float.style=float.style, |
| 317 |
digits=digits, |
| 318 |
stemplates=stemplates, |
| 319 |
sdigits=sdigits, |
| 320 |
show.eqnames=show.eqnames, |
| 321 |
controls=controls, |
| 322 |
collapse.controls=collapse.controls, |
| 323 |
control.var.indicator=control.var.indicator |
| 324 |
) |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
prefmt1 <- function(parm,template,float.style,digits,signif.symbols,controls){ |
| 329 |
|
| 330 |
rn <- rownames(parm) |
| 331 |
if(length(intersect(rn,controls))){ |
| 332 |
controls <- intersect(rn,controls) |
| 333 |
rn <- setdiff(rn,controls) |
| 334 |
if(length(dim(parm))==2) |
| 335 |
parm <- parm[rn,,drop=FALSE] |
| 336 |
else |
| 337 |
parm <- parm[rn,,,drop=FALSE] |
| 338 |
} |
| 339 |
else controls <- NULL |
| 340 |
adims <- if(length(dim(parm))==2) 1 else c(1,3) |
| 341 |
if(length(parm)){ |
| 342 |
|
| 343 |
if(is.array(parm)){ |
| 344 |
ans <- apply(parm,adims,applyTemplate, |
| 345 |
template=template, |
| 346 |
float.style=float.style, |
| 347 |
digits=digits, |
| 348 |
signif.symbols=signif.symbols) |
| 349 |
} |
| 350 |
else { |
| 351 |
ans <- array(formatC(parm, |
| 352 |
digits=digits, |
| 353 |
ifelse(is.integer(parm), |
| 354 |
"d","f"), |
| 355 |
width=1), |
| 356 |
dim=c(1,1,length(parm),1), |
| 357 |
dimnames=list(NULL,NULL,names(parm),NULL)) |
| 358 |
return(ans) |
| 359 |
} |
| 360 |
} |
| 361 |
else { |
| 362 |
ans <- array(character(0), |
| 363 |
dim=c(0,dim(parm)[adims]), |
| 364 |
dimnames=c(list(NULL),dimnames(parm)[adims])) |
| 365 |
} |
| 366 |
|
| 367 |
if(length(dim(template))){ |
| 368 |
newdims <- c(dim(template),dim(ans)[-1]) |
| 369 |
newdimnames <- c(dimnames(template),dimnames(ans)[-1]) |
| 370 |
|
| 371 |
# for(i in 1:length(newdims)){ |
| 372 |
# if(!length(newdimnames[[i]])){ |
| 373 |
# if(newdims[i]==0) |
| 374 |
# newdimnames[[i]] <- character(0) |
| 375 |
# else |
| 376 |
# newdimnames[[i]] <- as.character(1:newdims[i]) |
| 377 |
# } |
| 378 |
# } |
| 379 |
|
| 380 |
dim(ans) <- newdims |
| 381 |
dimnames(ans) <- newdimnames |
| 382 |
} else rownames(ans) <- names(template) |
| 383 |
|
| 384 |
ans[ans=="()"] <- "" |
| 385 |
attr(ans,"controls") <- controls |
| 386 |
return(ans) |
| 387 |
} |
| 388 |
|
| 389 |
prefmt2 <- function(parm){ |
| 390 |
|
| 391 |
if(length(dim(parm))<4) |
| 392 |
dim(parm)[4] <- 1 |
| 393 |
|
| 394 |
parm <- aperm(parm,c(1,3,2,4)) |
| 395 |
dim(parm) <- c(prod(dim(parm)[1:2]),prod(dim(parm)[3:4])) |
| 396 |
|
| 397 |
parm |
| 398 |
} |
| 399 |
|
| 400 |
colexpand <- function(x,nc){ |
| 401 |
x.nr <- nrow(x) |
| 402 |
x.nc <- ncol(x) |
| 403 |
y <- matrix("",nrow=x.nr,ncol=max(nc,1)) |
| 404 |
if(length(x)) |
| 405 |
y[,1:x.nc] <- x |
| 406 |
y |
| 407 |
} |
| 408 |
|
| 409 |
rowexpand <- function(x,nr){ |
| 410 |
x.nr <- nrow(x) |
| 411 |
x.nc <- ncol(x) |
| 412 |
y <- matrix("",nrow=nr,ncol=x.nc) |
| 413 |
if(length(x)) |
| 414 |
y[1:x.nr,] <- x |
| 415 |
y |
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
dimnames3 <- function(x)dimnames(x)[[3]] |
| 420 |
|
| 421 |
getRows <- function(x,r){ |
| 422 |
if(is.character(r)) |
| 423 |
r <- intersect(r,rownames(x)) |
| 424 |
x[r,,drop=FALSE] |
| 425 |
} |
| 426 |
get_rows <- function(x,i)try(x[i,,drop=FALSE]) |
| 427 |
|
| 428 |
relabel.memisc_mtable <- function(x,...,gsub=FALSE,fixed=!gsub,warn=FALSE){ |
| 429 |
|
| 430 |
relab.req <- list(..., |
| 431 |
gsub=gsub,fixed=fixed,warn=warn) |
| 432 |
|
| 433 |
relab.attr <- attr(x,"relabel") |
| 434 |
if(!length(relab.attr)) |
| 435 |
relab.attr <-list(relab.req) |
| 436 |
else |
| 437 |
relab.attr <-c(relab.attr, |
| 438 |
list(relab.req)) |
| 439 |
|
| 440 |
attr(x,"relabel") <- relab.attr |
| 441 |
|
| 442 |
x |
| 443 |
} |
| 444 |
|
| 445 |
pt_getrow <- function(x,i){ |
| 446 |
y <- x[i,] |
| 447 |
isn <- sapply(y,is.null) |
| 448 |
if(any(isn)) return(y[!isn]) |
| 449 |
else return(y) |
| 450 |
} |
| 451 |
|
| 452 |
do_subs <- function(x,r){ |
| 453 |
for(rr in r) |
| 454 |
x <- do_1sub(x,rr) |
| 455 |
return(x) |
| 456 |
} |
| 457 |
|
| 458 |
do_1sub <- function(x,r){ |
| 459 |
|
| 460 |
r.gsub <- r$gsub |
| 461 |
r.fixed <- r$fixed |
| 462 |
|
| 463 |
r <- r[names(r)%nin%c("gsub","fixed","warn")] |
| 464 |
|
| 465 |
y <- x |
| 466 |
for(i in seq_along(r)){ |
| 467 |
from <- names(r)[i] |
| 468 |
to <- r[[i]] |
| 469 |
if(r.gsub) |
| 470 |
y <- gsub(from,to,y,fixed=r.fixed) |
| 471 |
else { |
| 472 |
y[y==from] <- to |
| 473 |
} |
| 474 |
} |
| 475 |
return(y) |
| 476 |
} |
| 477 |
|
| 478 |
do_prettyfy <- function(pn, |
| 479 |
contrasts, |
| 480 |
xlevels, |
| 481 |
factor.style, |
| 482 |
show.baselevel, |
| 483 |
baselevel.sep){ |
| 484 |
|
| 485 |
if(!length(contrasts)) return(pn) |
| 486 |
|
| 487 |
res <- pn |
| 488 |
|
| 489 |
done <- res != pn |
| 490 |
|
| 491 |
for(m in names(contrasts)){ |
| 492 |
contrasts.m <- contrasts[[m]] |
| 493 |
xlevels.m <- xlevels[[m]] |
| 494 |
if(all(done)) break |
| 495 |
pn.tmp <- pn[!done] |
| 496 |
pn.tmp <- prettyNames(pn.tmp, |
| 497 |
contrasts=contrasts.m, |
| 498 |
xlevels=xlevels.m, |
| 499 |
factor.style=factor.style, |
| 500 |
show.baselevel=show.baselevel, |
| 501 |
baselevel.sep=baselevel.sep) |
| 502 |
res[!done] <- pn.tmp |
| 503 |
done <- res != pn |
| 504 |
} |
| 505 |
|
| 506 |
return(res) |
| 507 |
} |
| 508 |
|
| 509 |
nzchar_row <- function(x){ |
| 510 |
nzch <- array(nzchar(x),dim=dim(x)) |
| 511 |
apply(nzch,1,any) |
| 512 |
} |
| 513 |
|
| 514 |
dropnull <- function(x) { |
| 515 |
ii <- sapply(x,is.null) |
| 516 |
x[!ii] |
| 517 |
} |
| 518 |
ni <- function(tab,x) x%in%tab |
| 519 |
drop_zchrow <- function(x)x[nzchar_row(x),,drop=FALSE] |
| 520 |
|
| 521 |
preformat_mtable <- function(x){ |
| 522 |
|
| 523 |
x <- unclass(x) |
| 524 |
|
| 525 |
coef.style <- attr(x,"coef.style") |
| 526 |
summary.stats <- attr(x,"summary.stats") |
| 527 |
other.stats <- attr(x,"other.stats") |
| 528 |
signif.symbols <- attr(x,"signif.symbols") |
| 529 |
factor.style <- attr(x,"factor.style") |
| 530 |
show.baselevel <- attr(x,"show.baselevel") |
| 531 |
baselevel.sep <- attr(x,"baselevel.sep") |
| 532 |
float.style <- attr(x,"float.style") |
| 533 |
digits <- attr(x,"digits") |
| 534 |
stemplates <- attr(x,"stemplates") |
| 535 |
sdigits <- attr(x,"sdigits") |
| 536 |
|
| 537 |
# allcompo <- unique(unlist(lapply(x,names))) |
| 538 |
# nonparnames <- c("sumstat","contrasts","xlevels","call") |
| 539 |
# partypes <- setdiff(allcompo,nonparnames) |
| 540 |
|
| 541 |
sumstats <- lapply(x,`[[`,"sumstat") |
| 542 |
contrasts <- lapply(x,`[[`,"contrasts") |
| 543 |
xlevels <- lapply(x,`[[`,"xlevels") |
| 544 |
calls <- lapply(x,`[[`,"call") |
| 545 |
|
| 546 |
partypes <- unique(c("coef",unlist(lapply(x,`[[`,"parameter.types")))) |
| 547 |
parms <- lapply(x,`[`,partypes) |
| 548 |
parms <- lapply(parms,dropnull) |
| 549 |
|
| 550 |
descriptives <- lapply(x,`[[`,"descriptives") |
| 551 |
|
| 552 |
ctemplate <- getCoefTemplate(coef.style) |
| 553 |
if(!length(ctemplate)) stop("invalid coef.style argument") |
| 554 |
ctemplate <- as.matrix(ctemplate) |
| 555 |
ctdims <- dim(ctemplate) |
| 556 |
lctdims <- length(ctdims) |
| 557 |
if(lctdims>2) stop("can\'t handle templates with dim>2") |
| 558 |
|
| 559 |
relab.attr <- attr(x,"relabel") |
| 560 |
|
| 561 |
modelnames <- names(x) |
| 562 |
modelgroups <- attr(x,"model.groups") |
| 563 |
|
| 564 |
force.header <- isTRUE(attr(x,"force.header")) # Document that later ... |
| 565 |
show.eqnames <- attr(x,"show.eqnames") |
| 566 |
|
| 567 |
all.control.terms <- NULL |
| 568 |
control.terms <- NULL |
| 569 |
control.coefs <- NULL |
| 570 |
controls <- attr(x,"controls") |
| 571 |
collapse.controls <- attr(x,"collapse.controls") |
| 572 |
if(length(controls)){ |
| 573 |
control.terms <- controls$terms |
| 574 |
control.coefs <- controls$coefs |
| 575 |
control.coefs <- unique(unlist(control.coefs)) |
| 576 |
|
| 577 |
all.control.terms <- unique(unlist(control.terms)) |
| 578 |
} |
| 579 |
|
| 580 |
parmtab <- NULL |
| 581 |
|
| 582 |
ct.indicator <- attr(x,"control.var.indicator") |
| 583 |
if(!length(ct.indicator)) ct.indicator <- c("X","") |
| 584 |
|
| 585 |
if(length(partypes)){ |
| 586 |
for(n in 1:length(parms)){ |
| 587 |
|
| 588 |
parms.n <- parms[[n]] |
| 589 |
parms.n<- lapply(parms.n, |
| 590 |
prefmt1, |
| 591 |
template=ctemplate, |
| 592 |
float.style=float.style, |
| 593 |
digits=digits, |
| 594 |
signif.symbols=signif.symbols, |
| 595 |
controls=control.coefs) |
| 596 |
if(length(control.terms)){ |
| 597 |
ct <- control.terms[[n]] |
| 598 |
ct <- all.control.terms %in% ct |
| 599 |
if(collapse.controls) { |
| 600 |
if(all(ct)) |
| 601 |
ct <- ct.indicator[1] |
| 602 |
else if(!any(ct)) |
| 603 |
ct <- ct.indicator[2] |
| 604 |
else |
| 605 |
ct <- as.character(NA) |
| 606 |
dim(ct) <- c(1,1,1,1) |
| 607 |
dimnames(ct) <- list(1,2,"Controls",3) |
| 608 |
} |
| 609 |
else { |
| 610 |
ct <- ifelse(ct,ct.indicator[1],ct.indicator[2]) |
| 611 |
dim(ct) <- c(1,1,length(ct),1) |
| 612 |
dimnames(ct) <- list(1,2,all.control.terms,3) |
| 613 |
} |
| 614 |
parms.n <- append(parms.n,list(Controls=ct),after=1) |
| 615 |
} |
| 616 |
parms[[n]] <- parms.n |
| 617 |
} |
| 618 |
if(length(control.terms)) |
| 619 |
partypes <- append(partypes,"Controls",after=1) |
| 620 |
parmtab <- array(list(), |
| 621 |
dim=c(length(partypes),length(parms)), |
| 622 |
dimnames=list(partypes,names(parms))) |
| 623 |
|
| 624 |
for(n in 1:length(parms)){ |
| 625 |
mod <- parms[[n]] |
| 626 |
modnames <- names(mod) |
| 627 |
for(m in modnames){ |
| 628 |
mod.m <- mod[[m]] |
| 629 |
parmtab[[m,n]] <- mod.m |
| 630 |
} |
| 631 |
} |
| 632 |
|
| 633 |
parameter.names <- attr(x,"parameter.names") |
| 634 |
parmnames <- list() |
| 635 |
|
| 636 |
for(m in rownames(parmtab)){ |
| 637 |
tmp.pn <- lapply(parmtab[m,],dimnames3) |
| 638 |
tmp.pn <- unique(unlist(tmp.pn)) |
| 639 |
tmp.pn <- parameter.names[parameter.names %in% tmp.pn] |
| 640 |
parmnames[[m]] <- tmp.pn |
| 641 |
} |
| 642 |
if(length(all.control.terms)){ |
| 643 |
if(collapse.controls) |
| 644 |
parmnames$Controls <- "Controls" |
| 645 |
else |
| 646 |
parmnames$Controls <- all.control.terms |
| 647 |
} |
| 648 |
# Make sure that columns and rows match across models |
| 649 |
for(n in 1:ncol(parmtab)){ |
| 650 |
mod <- parms[[n]] |
| 651 |
for(m in rownames(parmtab)){ |
| 652 |
parmtab.mn <- parmtab[[m,n]] |
| 653 |
if(length(parmnames[[m]])){ |
| 654 |
parmtab.mn <- coefxpand(parmtab.mn,parmnames[[m]]) |
| 655 |
parmtab.mn <- prefmt2(parmtab.mn) |
| 656 |
parmtab[[m,n]] <- parmtab.mn |
| 657 |
} |
| 658 |
modm <- mod[[m]] |
| 659 |
} |
| 660 |
maxncol <- max(unlist(lapply(parmtab[,n],ncol)) ) |
| 661 |
parmtab[,n] <- lapply(parmtab[,n],colexpand,maxncol) |
| 662 |
} |
| 663 |
# Drop empty rows |
| 664 |
for(n in 1:nrow(parmtab)){ |
| 665 |
maxnrow <- max(unlist(lapply(parmtab[n,],nrow)) ) |
| 666 |
parmtab[n,] <- lapply(parmtab[n,],rowexpand,maxnrow) |
| 667 |
nz <- lapply(parmtab[n,],nzchar_row) |
| 668 |
if(length(nz)>1) |
| 669 |
nz <- reduce(nz,`|`) |
| 670 |
else |
| 671 |
nz <- nz[[1]] |
| 672 |
parmtab[n,] <- lapply(parmtab[n,],get_rows,i=nz) |
| 673 |
} |
| 674 |
} |
| 675 |
headers <- list() |
| 676 |
if(length(modelnames) > 1 || length(modelnames) == 1 && force.header) { |
| 677 |
modelnames <- do_subs(modelnames,relab.attr) |
| 678 |
headers[[1]] <- Map(structure,modelnames,span=lapply(parmtab[1,],ncol)) |
| 679 |
if(length(modelgroups)){ |
| 680 |
ncols <- sapply(parmtab[1,],ncol) |
| 681 |
sp <- lapply(modelgroups,function(mg)sum(ncols[mg])) |
| 682 |
h <- Map(structure,names(modelgroups),span=sp) |
| 683 |
headers <- c(list(h),headers) |
| 684 |
} |
| 685 |
} |
| 686 |
# show.eqnames <- show.eqnames || has.multieq(x) |
| 687 |
|
| 688 |
get_eq.headers <- function(x){ |
| 689 |
cf <- x$coef |
| 690 |
dn.cf <- dimnames(cf) |
| 691 |
if(length(dn.cf)>2) |
| 692 |
eq.names <- dimnames(cf)[[3]] |
| 693 |
else |
| 694 |
eq.names <- NULL |
| 695 |
} |
| 696 |
eq.headers <- lapply(x,get_eq.headers) |
| 697 |
all.eq.names <- unique(unlist(eq.headers)) |
| 698 |
if(is.na(show.eqnames)) |
| 699 |
show.eqnames <- length(all.eq.names) > 1 |
| 700 |
if(!show.eqnames) |
| 701 |
eq.headers <- NULL |
| 702 |
|
| 703 |
leaders <- vector(mode="list",length=nrow(parmtab)) |
| 704 |
names(leaders) <- rownames(parmtab) |
| 705 |
if(length(partypes)){ |
| 706 |
i <- 0 |
| 707 |
for(m in rownames(parmtab)){ |
| 708 |
i <- i + 1 |
| 709 |
pn <- parmnames[[m]] |
| 710 |
pn <- do_prettyfy(pn, |
| 711 |
contrasts=contrasts, |
| 712 |
xlevels=xlevels, |
| 713 |
factor.style=factor.style, |
| 714 |
show.baselevel=show.baselevel, |
| 715 |
baselevel.sep=baselevel.sep) |
| 716 |
pn <- do_subs(pn,relab.attr) |
| 717 |
span <- nrow(parmtab[[m,1]])/length(pn) |
| 718 |
if(span < 1) |
| 719 |
leaders[[i]] <- NULL |
| 720 |
else |
| 721 |
leaders[[i]] <- lapply(pn,structure,span=span) |
| 722 |
} |
| 723 |
pt <- rownames(parmtab) |
| 724 |
pt <- do_subs(pt,relab.attr) |
| 725 |
rownames(parmtab) <- pt |
| 726 |
names(leaders) <- pt |
| 727 |
} |
| 728 |
|
| 729 |
if(length(summary.stats)){ |
| 730 |
stemplates_ <- Map(select_by_names,stemplates,summary.stats) |
| 731 |
sst <- Map(applyTemplate,sumstats,stemplates_,digits=sdigits) |
| 732 |
sst <- lapply(sst,drop_zchrow) |
| 733 |
snames <- unique(unlist(lapply(sst,rownames))) |
| 734 |
nc <- lapply(parmtab[1,],ncol) |
| 735 |
summary.stats <- Map(smryxpand,sst,list(snames)) |
| 736 |
|
| 737 |
snames <- do_subs(snames,relab.attr) |
| 738 |
snames <- lapply(snames,structure,span=1) |
| 739 |
leaders <- c(leaders,summary.stats=list(snames)) |
| 740 |
} |
| 741 |
else summary.stats <- NULL |
| 742 |
|
| 743 |
if(length(descriptives)){ |
| 744 |
dnames <- unique(unlist(lapply(descriptives,rownames))) |
| 745 |
nc <- lapply(parmtab[1,],ncol) |
| 746 |
descriptives <- Map(smryxpand,descriptives,list(dnames)) |
| 747 |
} |
| 748 |
else descriptives <- NULL |
| 749 |
|
| 750 |
needs.signif <- any(grepl("$p",ctemplate,fixed=TRUE)) |
| 751 |
if(needs.signif){ |
| 752 |
signif.symbols <- signif.symbols |
| 753 |
} |
| 754 |
else |
| 755 |
signif.symbols <- NULL |
| 756 |
|
| 757 |
outtypes <- array("num", |
| 758 |
dim=dim(parmtab), |
| 759 |
dimnames=dimnames(parmtab)) |
| 760 |
if(length(controls)){ |
| 761 |
outtypes["Controls",] <- "text" |
| 762 |
} |
| 763 |
|
| 764 |
structure(list(parmtab=parmtab, |
| 765 |
leaders=leaders, |
| 766 |
headers=headers, |
| 767 |
eq.headers=eq.headers, |
| 768 |
summary.stats = summary.stats, |
| 769 |
other.stats = other.stats, |
| 770 |
signif.symbols=signif.symbols, |
| 771 |
controls=controls, |
| 772 |
outtypes=outtypes), |
| 773 |
class="preformatted.memisc_mtable") |
| 774 |
} |
| 775 |
|
| 776 |
# Avoid NA's while subsetting |
| 777 |
select_by_names <- function(x,nms){ |
| 778 |
nms <- unique(nms) |
| 779 |
nms <- intersect(names(x),nms) |
| 780 |
x[nms] |
| 781 |
} |
| 782 |
|
| 783 |
|
| 784 |
format_signif <- function(syms,tmpl){ |
| 785 |
title <- tmpl[1] |
| 786 |
clps <- tmpl[3] |
| 787 |
tmpl <- tmpl[2] |
| 788 |
res <- c() |
| 789 |
for(i in seq_along(syms)){ |
| 790 |
sym <- names(syms)[i] |
| 791 |
thrsh <- unname(syms[i]) |
| 792 |
res.i <- sub("$sym",sym,tmpl,fixed=TRUE) |
| 793 |
res.i <- sub("$val",thrsh,res.i,fixed=TRUE) |
| 794 |
res <- c(res,res.i) |
| 795 |
} |
| 796 |
res <- paste(res,collapse=clps) |
| 797 |
paste0(title,res) |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
format.memisc_mtable <- function(x, |
| 802 |
target=c("print","LaTeX","HTML","delim"), |
| 803 |
...){ |
| 804 |
target <- match.arg(target) |
| 805 |
x <- preformat_mtable(x) |
| 806 |
switch(target, |
| 807 |
print=pf_mtable_format_print(x,...), |
| 808 |
LaTeX=pf_mtable_format_latex(x,...), |
| 809 |
HTML=pf_mtable_format_html(x,...), |
| 810 |
delim=pf_mtable_format_delim(x,...) |
| 811 |
) |
| 812 |
} |
| 813 |
|
| 814 |
print.memisc_mtable <- function(x,center.at=getOption("OutDec"), |
| 815 |
topsep="=",bottomsep="=",sectionsep="-",...){ |
| 816 |
|
| 817 |
calls <- sapply(x,"[[","call") |
| 818 |
cat("\nCalls:\n") |
| 819 |
for(i in seq(calls)){ |
| 820 |
cat(names(calls)[i],": ",sep="") |
| 821 |
print(calls[[i]]) |
| 822 |
} |
| 823 |
cat("\n") |
| 824 |
cat(format.memisc_mtable(x,target="print", |
| 825 |
center.at=center.at, |
| 826 |
topsep=topsep, |
| 827 |
bottomsep=bottomsep, |
| 828 |
sectionsep=sectionsep,...), |
| 829 |
sep="") |
| 830 |
} |
| 831 |
|
| 832 |
toLatex.memisc_mtable <- function(object,...){ |
| 833 |
structure(format.memisc_mtable(x=object,target="LaTeX",...), |
| 834 |
class="Latex") |
| 835 |
} |
| 836 |
|
| 837 |
write.mtable <- function(object,file="", |
| 838 |
format=c("delim","LaTeX","HTML"), |
| 839 |
...){ |
| 840 |
l <- list(...) |
| 841 |
if(isTRUE(l[["forLaTeX"]])) # Avoid breaking old code |
| 842 |
target <- "LaTeX" |
| 843 |
else |
| 844 |
target <- match.arg(format) |
| 845 |
|
| 846 |
f <- format.memisc_mtable(object,target=target,...) |
| 847 |
if(target %in% c("LaTeX","HTML")) |
| 848 |
f <- paste(f,"\n",sep="") |
| 849 |
cat(f,file=file,sep="") |
| 850 |
} |
| 851 |
|
| 852 |
|