| 1 |
## The following three functions are contributed by |
| 2 |
## Christopher N. Lawrence, Ph.D. <[email protected]> |
| 3 |
## Assistant Professor of Political Science |
| 4 |
## Texas A&M International University |
| 5 |
## 313 LBVSC, 5201 University Blvd |
| 6 |
## Laredo, Texas 78041-1920 |
| 7 |
|
| 8 |
## Some patches were applied by ME in July 2012, August 2017 |
| 9 |
|
| 10 |
getSummary.polr <- function(obj, |
| 11 |
alpha=.05, |
| 12 |
...){ |
| 13 |
|
| 14 |
smry <- summary(obj) |
| 15 |
N <- if(length(weights(obj))) sum(weights(obj)) |
| 16 |
else nobs(obj) |
| 17 |
|
| 18 |
coef <- smry$coef |
| 19 |
|
| 20 |
if (smry$df.residual) { |
| 21 |
pvals <- 2*pt(-abs(coef[,3]), smry$df.residual) |
| 22 |
} else { |
| 23 |
pvals <- 2*pnorm(-abs(coef[,3])) |
| 24 |
} |
| 25 |
|
| 26 |
lower <- qnorm(p=alpha/2,mean=coef[,1],sd=coef[,2]) |
| 27 |
upper <- qnorm(p=1-alpha/2,mean=coef[,1],sd=coef[,2]) |
| 28 |
|
| 29 |
coef <- cbind(coef,pvals,lower,upper) |
| 30 |
|
| 31 |
colnames(coef) <- c("est","se","stat","p","lwr","upr") |
| 32 |
null.model <- update(obj, .~1) |
| 33 |
|
| 34 |
LR <- deviance(null.model) - deviance(obj) |
| 35 |
df <- null.model$df.residual - smry$df.resid |
| 36 |
|
| 37 |
ll <- logLik(obj) |
| 38 |
dev <- deviance(obj) |
| 39 |
|
| 40 |
if(df > 0){ |
| 41 |
p <- pchisq(LR,df,lower.tail=FALSE) |
| 42 |
L0.pwr <- exp(-deviance(null.model)/N) |
| 43 |
#LM.pwr <- exp(-smry$deviance/N) |
| 44 |
|
| 45 |
Aldrich.Nelson <- LR/(LR+N) |
| 46 |
McFadden <- 1 - dev/deviance(null.model) |
| 47 |
Cox.Snell <- 1 - exp(-LR/N) |
| 48 |
Nagelkerke <- Cox.Snell/(1-L0.pwr) |
| 49 |
} |
| 50 |
else { |
| 51 |
LR <- NA |
| 52 |
df <- NA |
| 53 |
p <- NA |
| 54 |
Aldrich.Nelson <- NA |
| 55 |
McFadden <- NA |
| 56 |
Cox.Snell <- NA |
| 57 |
Nagelkerke <- NA |
| 58 |
} |
| 59 |
|
| 60 |
AIC <- AIC(obj) |
| 61 |
BIC <- AIC(obj,k=log(N)) |
| 62 |
sumstat <- c( |
| 63 |
LR = LR, |
| 64 |
df = df, |
| 65 |
p = p, |
| 66 |
logLik = ll, |
| 67 |
deviance = dev, |
| 68 |
Aldrich.Nelson = Aldrich.Nelson, |
| 69 |
McFadden = McFadden, |
| 70 |
Cox.Snell = Cox.Snell, |
| 71 |
Nagelkerke = Nagelkerke, |
| 72 |
AIC = AIC, |
| 73 |
BIC = BIC, |
| 74 |
N = N |
| 75 |
) |
| 76 |
|
| 77 |
#coef <- apply(coef,1,applyTemplate,template=coef.template) |
| 78 |
|
| 79 |
#sumstat <- drop(applyTemplate(sumstat,template=sumstat.template)) |
| 80 |
list(coef=coef,sumstat=sumstat,contrasts=obj$contrasts,xlevels=smry$xlevels,call=obj$call) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
getSummary.simex <- function(obj, |
| 85 |
alpha=.05, |
| 86 |
...){ |
| 87 |
|
| 88 |
smry <- summary(obj) |
| 89 |
modsmry <- summary(obj$model) |
| 90 |
N <- if(length(weights(obj$model))) sum(weights(obj$model)) |
| 91 |
else { |
| 92 |
if(length(modsmry$df) > 1) sum(modsmry$df[1:2]) |
| 93 |
else obj$model$nobs |
| 94 |
} |
| 95 |
|
| 96 |
## Asymptotics not guaranteed to be here. |
| 97 |
coef <- smry$coef$jackknife |
| 98 |
|
| 99 |
lower <- qnorm(p=alpha/2,mean=coef[,1],sd=coef[,2]) |
| 100 |
upper <- qnorm(p=1-alpha/2,mean=coef[,1],sd=coef[,2]) |
| 101 |
|
| 102 |
coef <- cbind(coef,lower,upper) |
| 103 |
|
| 104 |
colnames(coef) <- c("est","se","stat","p","lwr","upr") |
| 105 |
sumstat <- c(N=N) |
| 106 |
|
| 107 |
list(coef=coef,sumstat=sumstat,contrasts=obj$contrasts,xlevels=smry$xlevels,call=obj$call) |
| 108 |
} |
| 109 |
|
| 110 |
setSummaryTemplate("simex" = c( |
| 111 |
"N" = "($N:d)" |
| 112 |
)) |