2

我正在尝试将mlogit()结果导出到乳胶表中,但我的尝试都没有成功!

1)首先我尝试使用包xtable()

> library(xtable)
> s<-summary(mx1)
> tab<-xtable(s, caption= "RPL results")
Errore in UseMethod("xtable") : 
no applicable method for 'xtable' applied to an object of class "c('summary.mlogit', 'mlogit')"

2) 然后我尝试使用 memsic() 包中的 toLatex():

> library("memisc")
> s<-summary(mx1)
> toLatex(mtable(s))
Errore in UseMethod("getSummary") : 
no applicable method for 'getSummary' applied to an object of class "c('summary.mlogit', 'mlogit')"

任何想法?似乎 mlogit() 缺少 getSummary() 方法

4

4 回答 4

4

正如@JakobR 所说xtable,不知道如何处理类对象mlogitsummary.mlogit. 但是由于xtable依赖S3OOP 系统很容易添加这样的方法(例如xtable.summary.lm用作模板)

require(mlogit)
require(xtable)

### from help page
data(Fishing)
Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode")
modelsum <- summary(mlogit(mode ~ price + catch, data = Fish))
modelsum$CoefTable

##                     Estimate Std. Error  t-value   Pr(>|t|)
## boat:(intercept)     0.87137  0.1140428   7.6408 2.1538e-14
## charter:(intercept)  1.49889  0.1329328  11.2755 0.0000e+00
## pier:(intercept)     0.30706  0.1145738   2.6800 7.3627e-03
## price               -0.02479  0.0017044 -14.5444 0.0000e+00
## catch                0.37717  0.1099707   3.4297 6.0420e-04

现在我们可以编写自己的方法:

## check the class first
class(modelsum)
[1] "summary.mlogit" "mlogit" 


### write a method from summary.mlogit
xtable.summary.mlogit <- function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
    display = NULL, ...) 
{
    x <- data.frame(x$CoefTable, check.names = FALSE)
    class(x) <- c("xtable", "data.frame")
    caption(x) <- caption
    label(x) <- label
    align(x) <- switch(1 + is.null(align), align, c("r", "r", 
        "r", "r", "r"))
    digits(x) <- switch(1 + is.null(digits), digits, c(0, 4, 
        4, 2, 4))
    display(x) <- switch(1 + is.null(display), display, c("s", 
        "f", "f", "f", "f"))
    return(x)
}

让我们做一个简单的测试

xtable(modelsum, digits = 2)

## % latex table generated in R 2.15.1 by xtable 1.7-0 package
## % Thu Aug  9 09:09:26 2012
## \begin{table}[ht]
## \begin{center}
## \begin{tabular}{rrrrr}
##   \hline
##  & Estimate & Std. Error & t-value & Pr($>$$|$t$|$) \\ 
##   \hline
## boat:(intercept) & 0.87 & 0.11 & 7.64 & 0.00 \\ 
##   charter:(intercept) & 1.50 & 0.13 & 11.28 & 0.00 \\ 
##   pier:(intercept) & 0.31 & 0.11 & 2.68 & 0.01 \\ 
##   price & -0.02 & 0.00 & -14.54 & 0.00 \\ 
##   catch & 0.38 & 0.11 & 3.43 & 0.00 \\ 
##    \hline
## \end{tabular}
## \end{center}
## \end{table}

小编辑,因为 OP 要求重要星星支持(我知道asterisk功能看起来不优雅

## function to add star...

asterisk <- function(y) ifelse(y < 0.001, "***", 
                            ifelse(y < 0.01, "**" ,
                               ifelse(y < 0.05, "*",
                                  ifelse(y < 0.1, ".", ""))))

DF <- read.table(text = capture.output(data.frame(modelsum$CoefTable)))
DF$V6 <- asterisk(DF[,4])

names(DF) <- c(colnames(modelsum$CoefTable), " ")
xtable(DF)


## % latex table generated in R 2.15.1 by xtable 1.7-0 package
## % Thu Aug  9 11:46:31 2012
## \begin{table}[ht]
## \begin{center}
## \begin{tabular}{rrrrrl}
##   \hline
##  & Estimate & Std. Error & t-value & Pr($>$$|$t$|$) &   \\ 
##   \hline
## boat:(intercept) & 0.87 & 0.11 & 7.64 & 0.00 & *** \\ 
##   charter:(intercept) & 1.50 & 0.13 & 11.28 & 0.00 & *** \\ 
##   pier:(intercept) & 0.31 & 0.11 & 2.68 & 0.01 & ** \\ 
##   price & -0.02 & 0.00 & -14.54 & 0.00 & *** \\ 
##   catch & 0.38 & 0.11 & 3.43 & 0.00 & *** \\ 
##    \hline
## \end{tabular}
## \end{center}
## \end{table}

受此线程启发的解决方案

于 2012-08-09T09:10:06.160 回答
4

问题是,xtable现在不知道如何处理类似的事情,summary.mlogit 但是您可以使用例如提取系数表,s$CoefTable因此xtable(s$CoefTable)可以工作。

于 2012-08-09T08:42:05.107 回答
1

如果您只使用包中的函数,您也可以在不编写函数的情况下获得一个漂亮的汇总latexHmisc。尝试

library(Hmisc)
latex(modelsum$CoefTable, digits=3) # using @dickoa's example

如您所见,这为您提供了类似于使用@dickoa 的解决方案获得的东西。

# With caption
latex(modelsum$CoefTable, digits=3, 
      caption='A mlogit summary table')

您可以阅读帮助文件,从中可以获得很多选项(?latex)。

于 2012-08-09T11:56:37.537 回答
0

关于 memisc 包的 mtable() 函数,一种解决方案是编写一个自定义 getSummary 方法,如此处建议的函数 lme4():https ://stat.ethz.ch/pipermail/r-sig-mixed-models /2009q1/002058.html

library(lme4)
library(memisc)

### create three models
fm1 <- lmer(Reaction ~ 1 + (Days|Subject), sleepstudy)
fm1.1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
fm1.2 <- lmer(Reaction ~ as.factor(Days) + (Days|Subject), sleepstudy)

### note: need to run the code below fro setCoefTemplate and
### getSummary.lmer first

mtable("Model 1"=fm1, "Model 2"=fm1.1, "Model 3"=fm1.2,
                coef.style = "est.ci", # using "homegrown" est.ci, specified above
                summary.stats=c("AIC","BIC"),
                getSummary = "getSummary.lmer")#,

setCoefTemplate(
  est.ci=c(
    est = "($est:#)($p:*)",
    ci = "[($lwr:#),($upr:#)]"))

getSummary.lmer <- function (obj, alpha = 0.05, ...)
{
     require(lme4)
     smry <- summary(obj)
     #N <- if (length(weights(obj))) ### NOTE: how to deal with groups/samp size?
     #    sum(weights(obj))
     #else sum(smry$df[1:2])
     coef <- smry at coefs
     lower <- qnorm(p = alpha/2, mean = coef[, 1], sd = coef[,2])
     upper <- qnorm(p = 1 - alpha/2, mean = coef[, 1], sd = coef[,2])
     if (ncol(smry at coefs) == 3) {
        p <- (1 - pnorm(smry at coefs[,3]))*2 # NOTE: no p-values for lmer() due to
                                              # unclear dfs; calculate p-values based on z
        coef <- cbind(coef, p, lower, upper)
        } else {
                coef <- cbind(coef, lower, upper) # glmer will have 4 columns with p-values
                }
     colnames(coef) <- c("est", "se", "stat", "p", "lwr", "upr")
     #phi <- smry$dispersion
     #LR <- smry$null.deviance - smry$deviance
     #df <- smry$df.null - smry$df.residual
     ll <- smry at AICtab[3][,1]
     deviance <- smry at AICtab[4][,1]
     #if (df > 0) {
     #    p <- pchisq(LR, df, lower.tail = FALSE)
     #    L0.pwr <- exp(-smry$null.deviance/N)
     #    McFadden <- 1 - smry$deviance/smry$null.deviance
     #    Cox.Snell <- 1 - exp(-LR/N)
     #    Nagelkerke <- Cox.Snell/(1 - L0.pwr)
     #}
     #else {
     #    LR <- NA
     #    df <- NA
     #    p <- NA
     #    McFadden <- NA
     #    Cox.Snell <- NA
     #    Nagelkerke <- NA
     #}
     AIC <- smry at AICtab[1][,1] # NOTE: these are both data.frames? not sure why...
     BIC <- smry at AICtab[2][,1]
     ### NOTE: don't see a similar slot for "xlevels" to get levels of
     ###        factor variables used as predictors; for time being, force
     ###        user to specify explicitly; nope that didn't work...
     #if (fac != NULL) {
     #  n <- length(fac)
     #  xlevels <- vector(n, mode = "list")
     #  for (i in 1:n) {
     #      xlevels[i] <- levels(obj at frame[,fac[i]])
     #      }
     #  }
     #sumstat <- c(phi = phi, LR = LR, df = df, p = p, logLik = ll,
     #    deviance = deviance, McFadden = McFadden, Cox.Snell = Cox.Snell,
     #    Nagelkerke = Nagelkerke, AIC = AIC, BIC = BIC, N = N)
     sumstat <- c(logLik = ll, deviance = deviance, AIC = AIC, BIC = BIC)
     list(coef = coef, sumstat = sumstat,
         contrasts = attr(model.matrix(obj), "contrasts"),
         xlevels = NULL, call = obj at call)
}
于 2012-08-09T10:56:33.493 回答