File: yy-mtable-ext-AchimZeileis.R

package info (click to toggle)
r-cran-memisc 0.99.31.8.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,136 kB
  • sloc: ansic: 5,117; makefile: 2
file content (238 lines) | stat: -rw-r--r-- 6,202 bytes parent folder | download
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
## AER::ivreg

getSummary.ivreg <- function(obj, alpha = 0.05, ...) {
  ## extract summary object
  s <- summary(obj)
  
  ## coefficient matrix and confidence interval
  cf <- cbind(s$coefficients, confint(obj, level = 1 - alpha))
  colnames(cf) <- c("est", "se", "stat", "p", "lwr", "upr")

  # Patch ME 20.08.2017
  dn <- c(
      dimnames(cf),
      names(obj$model)[1]
  )
  dim(cf) <- c(dim(cf)[1],dim(cf)[2],1)
  dimnames(cf) <- dn
  
  ## further summary statistics
  sstat <- c("sigma" = s$sigma,
    "r.squared" = s$r.squared, "adj.r.squared" = s$adj.r.squared,
    "Wald" = s$waldtest[1], "numdf" = s$waldtest[3], "dendf" = s$waldtest[4],
    "p" = s$waldtest[2], "N" = nobs(obj))

  ## return everything
  return(list(
    coef = cf,
    sumstat = sstat,
    contrasts = obj$contrasts,
    xlevels = obj$xlevels,
    call = obj$call
  ))
}

setSummaryTemplate("ivreg" = c(
  "R-squared" = "($r.squared:f#)",
  "adj. R-squared" = "($adj.r.squared:f#)",
  "sigma" = "($sigma:#)",
  "Wald" = "($Wald:f#)",
  "p" = "($p:f#)",
  "N" = "($N:d)"
))
.SummaryStats$ivreg <- c("adj. R-squared","N")

## AER::tobit

getSummary.tobit <- function(obj, alpha = 0.05, ...) {
  ## extract summary object
  s <- summary(obj)
  
  ## coefficient matrix and confidence interval
  ## compute confidence interval manually to include Log(scale)
  cf <- cbind(s$coefficients,
    s$coefficients[, 1] + qnorm(alpha/2) * s$coefficients[, 2],
    s$coefficients[, 1] + qnorm(1 - alpha/2) * s$coefficients[, 2])
  colnames(cf) <- c("est", "se", "stat", "p", "lwr", "upr")

  ## Improvement by ME: deal with log-scale parameter
  sp.row <- match("Log(scale)",rownames(cf))
  sp <- cf[sp.row,,drop=FALSE]
  cf <- cf[-sp.row,,drop=FALSE]

  # Patch ME 20.08.2017
  dn <- c(
      dimnames(cf),
      rownames(attr(obj$terms,"factors"))[1]
  )
  dim(cf) <- c(dim(cf)[1],dim(cf)[2],1)
  dimnames(cf) <- dn

  ## further summary statistics
  sstat <- c(
    "scale" = s$scale,
    "Wald" = s$wald,
    "numdf" = sum(s$df) - s$idf,
    "p" = pchisq(s$wald, sum(s$df) - s$idf, lower.tail = FALSE),
    "N" = nobs(obj),
    "logLik" = as.vector(logLik(obj)),
    "AIC" = AIC(obj),
    "BIC" = BIC(obj))

  ## return everything
  return(list(
      coef=cf,
      scale=sp,
      sumstat = sstat,
      contrasts = obj$contrasts,
      xlevels = obj$xlevels,
      call = obj$call
  ))
}

setSummaryTemplate("tobit" = c(
  "Scale" = "($scale:#)",
  "Wald" = "($Wald:f#)",
  "p" = "($p:f#)",
  "Log-likelihood" = "($logLik:f#)",
  "AIC" = "($AIC:f#)",
  "BIC" = "($BIC:f#)",
  "N" = "($N:d)"
))

## pscl:::hurdle, pscl:::zeroinfl

getSummary.hurdle <- getSummary.zeroinfl <- function(obj, alpha = 0.05, ...) {
  ## extract coefficient summary
  cf <- summary(obj)$coefficients
  ## augment with confidence intervals
  cval <- qnorm(1 - alpha/2)
  for(i in seq_along(cf)) cf[[i]] <- cbind(cf[[i]],
    cf[[i]][, 1] - cval * cf[[i]][, 2],
    cf[[i]][, 1] + cval * cf[[i]][, 2])
  ## collect in array
  nam <- unique(unlist(lapply(cf, rownames)))
  acf <- array(dim = c(length(nam), 6, length(cf)),
    dimnames = list(nam, c("est", "se", "stat", "p", "lwr", "upr"), names(cf)))
  for(i in seq_along(cf)) acf[rownames(cf[[i]]), , i] <- cf[[i]]

  ## return everything
  return(list(
    coef = acf,
    sumstat = c(
      "N" = obj$n,
      "logLik" = as.vector(logLik(obj)),
      "AIC" = AIC(obj),
      "BIC" = AIC(obj, k = log(obj$n))
    ),
    contrasts = obj$contrasts,
    xlevels = obj$xlevels,
    call = obj$call
  ))
}

setSummaryTemplate("hurdle" = c(
  "Log-likelihood" = "($logLik:f#)",
  "AIC" = "($AIC:f#)",
  "BIC" = "($BIC:f#)",
  "N" = "($N:d)"
))

setSummaryTemplate("zeroinfl" = c(
  "Log-likelihood" = "($logLik:f#)",
  "AIC" = "($AIC:f#)",
  "BIC" = "($BIC:f#)",
  "N" = "($N:d)"
))

## betareg::betareg

getSummary.betareg <- function(obj, alpha = 0.05, ...) {
  ## extract coefficient summary
  s <- summary(obj)
  cf <- s$coefficients
  ## augment with confidence intervals
  cval <- qnorm(1 - alpha/2)
  for(i in seq_along(cf)) cf[[i]] <- cbind(cf[[i]],
    cf[[i]][, 1] - cval * cf[[i]][, 2],
    cf[[i]][, 1] + cval * cf[[i]][, 2])
  ## collect in array
  nam <- unique(unlist(lapply(cf, rownames)))
  acf <- array(dim = c(length(nam), 6, length(cf)),
    dimnames = list(nam, c("est", "se", "stat", "p", "lwr", "upr"), names(cf)))
  for(i in seq_along(cf)) acf[rownames(cf[[i]]), , i] <- cf[[i]]
  
  ## return everything
  return(list(
    coef = acf,
    sumstat = c(
      "N" = obj$n,
      "pseudo.r.squared" = s$pseudo.r.squared,
      "logLik" = as.vector(logLik(obj)),
      "AIC" = AIC(obj),
      "BIC" = AIC(obj, k = log(obj$n))
    ),
    contrasts = obj$contrasts,
    xlevels = obj$xlevels,
    call = obj$call
  ))
}

setSummaryTemplate("betareg" = c(
  "Pseudo R-sq." = "($pseudo.r.squared:f#)",
  "Log-likelihood" = "($logLik:f#)",
  "AIC" = "($AIC:f#)",
  "BIC" = "($BIC:f#)",
  "N" = "($N:d)"
))

getSummary.multinom <- function(obj, alpha = 0.05, ...) {
  ## extract coefficient summary
  s <- summary(obj)
  cf <- s$coefficients

  ## set up array
  acf <- array(dim = c(NCOL(cf), 6, NROW(cf)),
    dimnames = list(colnames(cf), c("est", "se", "stat", "p", "lwr", "upr"), rownames(cf)))
    
  ## coefficients and standard errors
  acf[, 1, ] <- t(cf)
  acf[, 2, ] <- t(s$standard.errors)

  ## compute z-statistic and asymptotic p-value
  acf[, 3, ] <- acf[, 1, ] / acf[, 2, ]
  acf[, 4, ] <- 2 * pnorm(abs(acf[, 3, ]), lower.tail = FALSE)
  
  ## asymptotic confidence intervals
  cval <- qnorm(1 - alpha/2)
  acf[, 5, ] <- acf[, 1, ] - cval * acf[, 2, ]
  acf[, 6, ] <- acf[, 1, ] + cval * acf[, 2, ]

  ## compute number of observations as sum of weights
  nobs <- sum(obj$weights)

  ## return everything
  return(list(
    coef = acf,
    sumstat = c(
      "N" = nobs,
      "deviance" = obj$deviance,
      "logLik" = as.vector(logLik(obj)),
      "AIC" = AIC(obj),
      "BIC" = AIC(obj, k = log(nobs))
    ),
    contrasts = obj$contrasts,
    xlevels = obj$xlevels,
    call = obj$call
  ))
}

setSummaryTemplate("multinom" = c(
  "Deviance" = "($deviance:f#)",
  "Log-likelihood" = "($logLik:f#)",
  "AIC" = "($AIC:f#)",
  "BIC" = "($BIC:f#)",
  "N" = "($N:d)"
))