Debian Codegraph @

R/mtable-manip.R

raw
1 dim.memisc_mtable <- function(x){
2
3 sapply(dimnames(x),length)
4 }
5
6 dimnames.memisc_mtable <- function(x){
7
8 modn <- names(x)
9
10 allcompo <- unique(unlist(lapply(x,names)))
11 nonparnames <- c("sumstat","contrasts","xlevels","call")
12 partypes <- setdiff(allcompo,nonparnames)
13
14 parms <- lapply(x,`[`,partypes)
15 parms <- do.call(cbind,parms)
16
17 rown <- lapply(1:nrow(parms),function(i)unique(unlist(lapply(parms[i,],rownames))))
18
19 rown <- Map(
20 function(x,n)
21 {
22 names(x) <- rep(n,length(x))
23 x
24 },rown,partypes)
25
26 rown <- do.call(c,rown)
27
28 parn <- attr(x,"parameter.names")
29
30 resp <- unique(unlist(lapply(parms[1,],dimnames3)))
31
32 list(
33 parn,
34 resp,
35 modn
36 )
37 }
38
39 safe.charidx <- function(nms,i){
40
41 lnms <- length(nms)
42 if(!length(i))
43 i <- nms
44 else if(is.character(i))
45 i <- i[i %in% nms]
46 else if(is.logical(i))
47 i <- nms[i]
48 else if(is.numeric(i)){
49 i <- i[abs(i) <= lnms]
50 i <- nms[i]
51 }
52 i
53 }
54
55 "[.memisc_mtable" <- function(x, i, j, k){
56
57 if(nargs() == 2){
58 k <- if(missing(i)) return(x) else i
59 i <- logical(0)
60 j <- logical(0)
61 }
62 else if(nargs() == 3){
63 i <- if(missing(i)) logical(0) else i
64 k <- if(missing(j)) logical(0) else j
65 j <- logical(0)
66 }
67 else { #nargs() == 4
68 i <- if(missing(i)) logical(0) else i
69 j <- if(missing(j)) logical(0) else j
70 k <- if(missing(k)) logical(0) else k
71 }
72
73 dn.x <- dimnames(x)
74 parn <- dn.x[[1]]
75 resp <- dn.x[[2]]
76 modn <- dn.x[[3]]
77
78 allcompo <- unique(unlist(lapply(x,names)))
79 nonparnames <- c("sumstat","contrasts","xlevels","call")
80 partypes <- setdiff(allcompo,nonparnames)
81
82 nparn <- length(parn)
83 nresp <- length(resp)
84 nmodn <- length(modn)
85
86 i <- safe.charidx(parn,i)
87 j <- safe.charidx(resp,j)
88 k <- safe.charidx(modn,k)
89 kk <- match(k,names(x))
90
91 y <- unclass(x)[k]
92 attr.y <- attributes(x)
93 attr.y$names <- k
94 attr.y$stemplates <- attr.y$stemplates[kk]
95 attr.y$summary.stats <- attr.y$summary.stats[kk]
96 attr.y$parameter.names <- i
97
98 if(length(attr.y$controls)){
99 attr.y$controls$coefs <- attr.y$controls$coefs[kk]
100 attr.y$controls$terms <- attr.y$controls$terms[kk]
101 }
102
103 l.y <- length(y)
104 l.pt <- length(partypes)
105
106 lt <- matrix(0,l.pt,l.y)
107
108 for(k in 1:l.pt){
109 pt <- partypes[k]
110 for(m in 1:l.y){
111 tmp <- y[[m]][[pt]]
112 i.tmp <- i[i %in% rownames(tmp)]
113 j.tmp <- j[j %in% dimnames3(tmp)]
114 if(length(i.tmp) && length(j.tmp))
115 tmp <- tmp[i.tmp,,j.tmp,drop=FALSE]
116 else if(length(i.tmp))
117 tmp <- tmp[i.tmp,,0,drop=FALSE]
118 else if(length(j.tmp))
119 tmp <- tmp[0,,j.tmp,drop=FALSE]
120 else
121 tmp <- tmp[0,,0,drop=FALSE]
122 y[[m]][[pt]] <- tmp
123 lt[k,m] <- length(tmp)
124 }
125 if(!any(lt>0)){
126 for(m in 1:l.y)
127 y[[m]][[pt]] <- NULL
128 }
129 }
130 if(all(lt==0))
131 return(NULL)
132 else{
133 attributes(y) <- attr.y
134 return(structure(y,class="memisc_mtable"))
135 }
136 }
137
138
139
140 ##
141 get.summary.stats <- function(x){
142 y <- attr(x,"summary.stats")
143 if(is.logical(y)){
144 if(isTRUE(y)){
145 names(unlist(attr(x,"stemplates")))
146 }
147 else character()
148 }
149 else
150 y
151 }
152
153 combine_mtables <- function(...){
154
155 args <- list(...)
156 argnames <- names(args)
157 names(args) <- NULL
158
159 if(length(argnames)>1){
160
161 mg <- sapply(args,length)
162 mg1 <- lapply(mg,seq.int)
163 mg0 <- c(0,mg[-length(mg)])
164 model.groups <- Map(`+`,mg0,mg1)
165 names(model.groups) <- argnames
166 }
167 else
168 model.groups <- NULL
169
170 coef.style <- attr(args[[1]],"coef.style")
171
172 parameter.names <- lapply(args,attr,"parameter.names")
173 parameter.names <- unique(unlist(parameter.names))
174
175 summary.stats <- lapply(args,get.summary.stats)
176 summary.stats <- unlist(summary.stats,recursive=FALSE)
177
178 signif.symbols <- attr(args[[1]],"signif.symbols")
179 factor.style <- attr(args[[1]],"factor.style")
180 show.baselevel <- attr(args[[1]],"show.baselevel")
181 baselevel.sep <- attr(args[[1]],"baselevel.sep")
182 float.style <- attr(args[[1]],"float.style")
183 digits <- attr(args[[1]],"digits")
184 stemplates <- do.call(c,lapply(args,attr,"stemplates"))
185 sdigits <- attr(args[[1]],"sdigits")
186 show.eqnames <- attr(args[[1]],"show.eqnames")
187
188 args <- lapply(args,unclass)
189
190 res <- do.call(c,args)
191
192 controls <- lapply(args,attr,"controls")
193 controls.coefs <- lapply(controls,"[[","coefs")
194 controls.terms <- lapply(controls,"[[","terms")
195 controls.coefs <- unlist(controls.coefs,recursive=FALSE)
196 controls.terms <- unlist(controls.terms,recursive=FALSE)
197 if(length(controls.terms))
198 controls <- list(coefs = controls.coefs,
199 terms = controls.terms)
200 else controls <- NULL
201
202 collapse.controls <- unlist(lapply(args,attr,"collapse.controls"))
203 collapse.controls <- any(collapse.controls)
204
205 control.var.indicator <- attr(args[[1]],"control.var.indicator")
206
207 structure(res,
208 class="memisc_mtable",
209 parameter.names=parameter.names,
210 coef.style=coef.style,
211 summary.stats=summary.stats,
212 signif.symbols=signif.symbols,
213 factor.style=factor.style,
214 show.baselevel=show.baselevel,
215 baselevel.sep=baselevel.sep,
216 float.style=float.style,
217 digits=digits,
218 stemplates=stemplates,
219 sdigits=sdigits,
220 show.eqnames=show.eqnames,
221 model.groups=model.groups,
222 controls=controls,
223 collapse.controls=collapse.controls,
224 control.var.indicator=control.var.indicator
225 )
226 }
227
228 c.memisc_mtable <- function(...) combine_mtables(...)