| 1 |
mkRule <- function(x,length) paste(rep_len(x,length),collapse="") |
| 2 |
|
| 3 |
set_length <- function(x,n,fill=""){ |
| 4 |
|
| 5 |
l <- length(x) |
| 6 |
nl <- min(n,l) |
| 7 |
|
| 8 |
y <- rep(fill,length=n) |
| 9 |
y[1:nl] <- x[1:nl] |
| 10 |
y |
| 11 |
} |
| 12 |
|
| 13 |
cols_folded <- function(x,n,sep){ |
| 14 |
|
| 15 |
nc <- ncol(x) |
| 16 |
nr <- nrow(x) |
| 17 |
|
| 18 |
m <- nc/n |
| 19 |
dim(x) <- c(nr,n,m) |
| 20 |
y <- apply(x,c(1,3),paste,collapse=sep) |
| 21 |
y |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
mkGrpSpan <- function(hspan){ |
| 26 |
|
| 27 |
hspan.out <- hspan |
| 28 |
l.hspan <- length(hspan) |
| 29 |
for(k in 2:l.hspan){ |
| 30 |
hspan1 <- hspan[[k-1]] |
| 31 |
hspan2 <- hspan[[k]] |
| 32 |
hspan.out.k <- hspan2 |
| 33 |
j1 <- 1 |
| 34 |
s2 <- 0 |
| 35 |
for(j2 in 1:length(hspan2)){ |
| 36 |
s1 <- s2 |
| 37 |
s2 <- s2 + hspan2[j2] |
| 38 |
c1 <- 0 |
| 39 |
while(s1 < s2){ |
| 40 |
s1 <- s1 + hspan1[j1] |
| 41 |
j1 <- j1 + 1 |
| 42 |
c1 <- c1 + 1 |
| 43 |
} |
| 44 |
if(s1 > s2) stop("misaligned spans") |
| 45 |
hspan.out.k[[j2]] <- c1 |
| 46 |
} |
| 47 |
hspan.out[[k]] <- hspan.out.k |
| 48 |
} |
| 49 |
hspan.out |
| 50 |
} |
| 51 |
|
| 52 |
mtable_format_print <- function(x, |
| 53 |
topsep="=", |
| 54 |
bottomsep="=", |
| 55 |
sectionsep="-", |
| 56 |
interaction.sep = " x ", |
| 57 |
center.at=getOption("OutDec"), |
| 58 |
align.integers=c("dot","right","left"), |
| 59 |
padding=" ", |
| 60 |
...) |
| 61 |
pf_mtable_format_print(preformat_mtable(x), |
| 62 |
topsep=topsep, |
| 63 |
bottomsep=bottomsep, |
| 64 |
sectionsep=sectionsep, |
| 65 |
interaction.sep =interaction.sep, |
| 66 |
center.at=center.at, |
| 67 |
align.integers=align.integers, |
| 68 |
padding=padding, |
| 69 |
...) |
| 70 |
|
| 71 |
pf_mtable_format_print <- function(x, |
| 72 |
topsep="=", |
| 73 |
bottomsep="=", |
| 74 |
sectionsep="-", |
| 75 |
interaction.sep = " x ", |
| 76 |
center.at=getOption("OutDec"), |
| 77 |
align.integers=c("dot","right","left"), |
| 78 |
padding=" ", |
| 79 |
show.parmtypes = nrow(x$parmtab) > 1, |
| 80 |
... |
| 81 |
){ |
| 82 |
|
| 83 |
colsep <- " " |
| 84 |
rowsep <- "\n" |
| 85 |
|
| 86 |
pt <- x$parmtab |
| 87 |
sst <- x$summary.stats |
| 88 |
leaders <- x$leaders |
| 89 |
headers <- x$headers |
| 90 |
eq.headers <- x$eq.headers |
| 91 |
|
| 92 |
l.headers <- length(headers) |
| 93 |
|
| 94 |
res <- NULL |
| 95 |
ncols <- NULL |
| 96 |
|
| 97 |
has.eq.headers <- length(eq.headers) > 0 |
| 98 |
|
| 99 |
for(j in 1:ncol(pt)){ |
| 100 |
|
| 101 |
name.j <- colnames(pt)[j] |
| 102 |
pt.j <- pt[,j] |
| 103 |
l.pt.j <- length(pt.j) |
| 104 |
|
| 105 |
ncol.j <- unique(sapply(pt.j,ncol)) |
| 106 |
stopifnot(length(ncol.j)==1) |
| 107 |
ncols <- c(ncols,ncol.j) |
| 108 |
|
| 109 |
skip.j <- logical(0) |
| 110 |
max.width <- 0 |
| 111 |
|
| 112 |
maxncols.j <- max(unlist(lapply(pt.j,ncol))) |
| 113 |
|
| 114 |
nr.j <- numeric(l.pt.j) |
| 115 |
for(i in 1:l.pt.j){ |
| 116 |
pt.ij <- pt.j[[i]] |
| 117 |
if(show.parmtypes){ |
| 118 |
pt.ij <- rbind(" ",pt.ij) |
| 119 |
} |
| 120 |
nr.j[i] <- nrow(pt.ij) |
| 121 |
skip.ij <- rep(FALSE,nrow(pt.ij)) |
| 122 |
tmp <- matrix("",nrow=nrow(pt.ij),ncol=maxncols.j) |
| 123 |
ii <- 1:ncol(pt.ij) |
| 124 |
tmp[,ii] <- pt.ij |
| 125 |
pt.j[[i]] <- pt.ij |
| 126 |
skip.j <- c(skip.j,skip.ij) |
| 127 |
} |
| 128 |
pt.j <- do.call(rbind,pt.j) |
| 129 |
|
| 130 |
if(length(sst)){ |
| 131 |
tmp <- sst[[j]] |
| 132 |
sst.j <- matrix("",nrow=length(tmp),ncol=maxncols.j) |
| 133 |
sst.j[,1] <- tmp |
| 134 |
pt.j <- rbind(pt.j,sst.j) |
| 135 |
skip.j <- c(skip.j,rep(FALSE,length(tmp))) |
| 136 |
} |
| 137 |
|
| 138 |
pt.j <- apply(pt.j,2,centerAt,skip=skip.j) |
| 139 |
|
| 140 |
if(has.eq.headers){ |
| 141 |
eq.header.j <- eq.headers[[name.j]] |
| 142 |
n.eq.j <- length(eq.header.j) |
| 143 |
eq.span <- ncol(pt.j)/n.eq.j |
| 144 |
dim(pt.j) <- c(nrow(pt.j),eq.span,n.eq.j) |
| 145 |
pt.j <- apply(pt.j,c(1,3),paste,collapse=colsep) |
| 146 |
pt.j <- rbind(eq.header.j,pt.j) |
| 147 |
pt.j <- apply(pt.j,2,format,justify="centre") |
| 148 |
pt.j <- apply(pt.j,1,paste,collapse=colsep) |
| 149 |
} |
| 150 |
else |
| 151 |
pt.j <- apply(pt.j,1,paste,collapse=colsep) |
| 152 |
res <- cbind(res,pt.j) |
| 153 |
} |
| 154 |
|
| 155 |
hlines <- character() |
| 156 |
if(l.headers){ |
| 157 |
headers1 <- Map(structure,list(""),span=ncols) # To take care of multi-eqn models |
| 158 |
headers <- c(headers,list(headers1)) |
| 159 |
headers <- rev(headers) |
| 160 |
hspan <- list() |
| 161 |
|
| 162 |
for(k in 1:length(headers)){ |
| 163 |
hspan[[k]] <- sapply(headers[[k]],attr,"span") |
| 164 |
} |
| 165 |
hspan <- mkGrpSpan(hspan) |
| 166 |
headers <- headers[-1] |
| 167 |
hspan <- hspan[-1] |
| 168 |
|
| 169 |
for(k in 1:l.headers){ |
| 170 |
|
| 171 |
headers.k <- headers[[k]] |
| 172 |
hspan.k <- hspan[[k]] |
| 173 |
res.grpd <- NULL |
| 174 |
cumsum <- 0 |
| 175 |
|
| 176 |
for(j in 1:length(headers.k)){ |
| 177 |
|
| 178 |
header.jk <- headers.k[[j]] |
| 179 |
hspan.jk <- hspan.k[[j]] |
| 180 |
res.cur.grp <- res[,cumsum + 1:hspan.jk,drop=FALSE] |
| 181 |
res.cur.grp <- apply(res.cur.grp,1,paste,collapse=colsep) |
| 182 |
nchar.jk <- nchar(res.cur.grp[1]) |
| 183 |
if(has.eq.headers && k==1 || k > 1 && l.headers > 1){ |
| 184 |
header.sep <- mkRule(sectionsep,nchar.jk) |
| 185 |
res.cur.grp <- c(header.sep,res.cur.grp) |
| 186 |
} |
| 187 |
res.cur.grp <- c(header.jk,res.cur.grp) |
| 188 |
res.cur.grp <- format(res.cur.grp,justify="centre") |
| 189 |
res.grpd <- cbind(res.grpd,res.cur.grp) |
| 190 |
|
| 191 |
cumsum <- cumsum + hspan.jk |
| 192 |
|
| 193 |
} |
| 194 |
res <- res.grpd |
| 195 |
} |
| 196 |
} |
| 197 |
res <- apply(res,2,function(x)paste0(" ",x)) |
| 198 |
|
| 199 |
ld.headlines <- 0 |
| 200 |
if(l.headers) |
| 201 |
ld.headlines <- ld.headlines + l.headers*2 - 1 |
| 202 |
if(has.eq.headers){ |
| 203 |
if(l.headers) |
| 204 |
ld.headlines <- ld.headlines + 2 |
| 205 |
else |
| 206 |
ld.headlines <- ld.headlines + 1 |
| 207 |
} |
| 208 |
l.leaders <- length(leaders) |
| 209 |
if(l.leaders){ |
| 210 |
leaders <- c(list(headers=rep(list(structure("",span=1)), |
| 211 |
ld.headlines)),leaders) |
| 212 |
leaders <- lapply(leaders,ldxp) |
| 213 |
if(show.parmtypes){ |
| 214 |
parmtypes <- rownames(x$parmtab) |
| 215 |
for(p in parmtypes){ |
| 216 |
leaders[[p]] <- rbind(p,leaders[[p]]) |
| 217 |
} |
| 218 |
} |
| 219 |
leaders <- do.call(rbind,leaders) |
| 220 |
leaders <- format(leaders,justify="left") |
| 221 |
res <- cbind(leaders,res) |
| 222 |
} |
| 223 |
|
| 224 |
res <- apply(res,1,paste,collapse=colsep) |
| 225 |
res <- paste0(padding,res,padding) |
| 226 |
width <- nchar(res[1]) |
| 227 |
toprule <- mkRule(topsep,width) |
| 228 |
sectionrule <- mkRule(sectionsep,width) |
| 229 |
bottomrule <- mkRule(topsep,width) |
| 230 |
|
| 231 |
csum <- ld.headlines |
| 232 |
|
| 233 |
sectsep.at <- integer() |
| 234 |
sectseps <- character() |
| 235 |
|
| 236 |
sec.hrules.at <- integer() |
| 237 |
sec.hrules <- character() |
| 238 |
|
| 239 |
leaders.gap <- mkRule(" ",nchar(leaders[i])) |
| 240 |
for(i in 1:nrow(pt)){ |
| 241 |
sectseps <- c(sectseps, sectionrule) |
| 242 |
sectsep.at <- c(sectsep.at, csum) |
| 243 |
csum <- csum + nrow(pt[[i,1]]) |
| 244 |
if(show.parmtypes) |
| 245 |
csum <- csum + 1 |
| 246 |
} |
| 247 |
if(length(sst) && any(sapply(sst,length)>0)){ |
| 248 |
sectseps <- c(sectseps, sectionrule) |
| 249 |
sectsep.at <- c(sectsep.at, csum) |
| 250 |
} |
| 251 |
|
| 252 |
res <- .insert(res, |
| 253 |
c(sec.hrules.at,sectsep.at), |
| 254 |
c(sec.hrules, sectseps) |
| 255 |
) |
| 256 |
|
| 257 |
res <- c(toprule,res,bottomrule) |
| 258 |
|
| 259 |
signif.symbols <- x$signif.symbols |
| 260 |
if(length(signif.symbols)){ |
| 261 |
signif.template <- getOption("signif.symbol.print.template", |
| 262 |
signif.symbol.print.default.template) |
| 263 |
signif.symbols <- format_signif_print(signif.symbols, |
| 264 |
signif.template, |
| 265 |
width=nchar(bottomrule)-2*nchar(padding)) |
| 266 |
signif.symbols <- paste0(padding,signif.symbols,padding) |
| 267 |
res <- c(res,signif.symbols) |
| 268 |
} |
| 269 |
res <- paste0(res,rowsep) |
| 270 |
return(res) |
| 271 |
} |
| 272 |
|
| 273 |
.insert <- function(x,where,what){ |
| 274 |
|
| 275 |
l <- rep(list(NULL),length(x)) |
| 276 |
|
| 277 |
what <- rep_len(what,length(where)) |
| 278 |
|
| 279 |
use <- where > 0 |
| 280 |
where <- where[use] |
| 281 |
what <- what[use] |
| 282 |
|
| 283 |
l[where] <- what |
| 284 |
y <- Map(c,x,l) |
| 285 |
unlist(y,recursive=FALSE) |
| 286 |
} |
| 287 |
|
| 288 |
.last <- function(x){ |
| 289 |
l <- length(x) |
| 290 |
x[[l]] |
| 291 |
} |
| 292 |
|
| 293 |
format_signif_print <- function(syms,tmpl,width,dec="."){ |
| 294 |
title <- tmpl[1] |
| 295 |
clps <- tmpl[3] |
| 296 |
tmpl <- tmpl[2] |
| 297 |
res <- title |
| 298 |
empty.title <- paste(rep(" ",nchar(title)),collapse="") |
| 299 |
|
| 300 |
ns <- length(syms) |
| 301 |
for(i in 1:ns){ |
| 302 |
sym <- names(syms)[i] |
| 303 |
thrsh <- unname(syms[i]) |
| 304 |
thrsh <- gsub(".",dec,thrsh,fixed=TRUE) |
| 305 |
res.i <- sub("$sym",sym,tmpl,fixed=TRUE) |
| 306 |
res.i <- sub("$val",thrsh,res.i,fixed=TRUE) |
| 307 |
if(i < ns) |
| 308 |
res.i <- paste0(res.i,clps) |
| 309 |
len <- length(res) |
| 310 |
res.l <- res[len] |
| 311 |
n.res.l <- nchar(res.l) |
| 312 |
n.res.i <- nchar(res.i) |
| 313 |
if(n.res.l+n.res.i <= width) |
| 314 |
res[len] <- paste0(res.l,res.i) |
| 315 |
else |
| 316 |
res <- c(res,paste0(empty.title,res.i)) |
| 317 |
} |
| 318 |
res |
| 319 |
} |
| 320 |
signif.symbol.print.default.template <- c("Significance: ","$sym = p < $val","; ") |
| 321 |
|
| 322 |
.row_prepend <- function(x,where,value){ |
| 323 |
l <- length(where) |
| 324 |
stopifnot(l == length(value)) |
| 325 |
where_ <- where + 1:l - 1 |
| 326 |
y <- matrix("",nrow(x)+l,ncol(x)) |
| 327 |
nr.y <- nrow(y) |
| 328 |
rows.y <- 1:nr.y |
| 329 |
other <- rows.y[!(rows.y %in% where_)] |
| 330 |
y[other,] <- x |
| 331 |
for(i in 1:l){ |
| 332 |
wh.i <- where_[l] |
| 333 |
y[wh.i,] <- value[[i]] |
| 334 |
} |
| 335 |
y |
| 336 |
} |
| 337 |
.drop_last <- function(x) x[-length(x)] |