我正在尝试将mlogit()结果导出到latex表中,但没有一次尝试成功!
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()方法
发布于 2012-08-09 16:42:05
问题是,xtable现在不能处理像summary.mlogit这样的东西,但是你可以例如用s$CoefTable提取系数表,因此xtable(s$CoefTable)可以工作。
发布于 2012-08-09 17:10:06
正如@JakobR所说,xtable不知道如何处理mlogit或summary.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启发的解决方案
发布于 2012-08-09 19:56:38
如果只使用Hmisc包中的function latex,也可以在不编写函数的情况下获得一个很好的汇总表。试一试
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)。
https://stackoverflow.com/questions/11879473
复制相似问题