使用toLatex()或xtable()将mlogit汇总结果导出到Latex

时间:2012-08-09 08:28:38

标签: r xtable

我尝试导出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 个答案:

答案 0 :(得分:4)

问题是,xtable现在不知道如何处理summary.mlogit之类的内容 但是,您可以使用s$CoefTable提取系数表,从而xtable(s$CoefTable)将起作用。

答案 1 :(得分:4)

正如@JakobR所说,xtable不知道如何处理班级mlogitsummary.mlogit的对象。 但由于xtable依赖于S3 OOP系统,因此添加此类方法很简单(例如使用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要求明星支持(The asterisk function doesn't look elegant I know

## 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}

受此thread

启发的解决方案

答案 2 :(得分:1)

如果你只使用latex包中的函数Hmisc,你也可以在不编写函数的情况下获得一个很好的汇总表。尝试

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)。

答案 3 :(得分: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)
}