2011-05-06 100 views
6

如果xtable不知道特殊命令,我应该怎么做。例如,假设一个Tobit模型的估计,如下所示:xtable不支持的功能(与R)

require(AER) require(xtable)

attach(cars)

tob<-tobit(dist~speed) summary(tob)

xtable(summary(tob))

detach(cars)

摘要的输出与线性模型的输出相比非常相似......我能做些什么来使xtable理解,我想要在Latex表中使用系数? Samme与其他功能如summary(zeroinfl(<model>))从pakage pscl? 你们会推荐做什么?

+1

@ user734124 - 我通常通过'coeftest()''中和lmtest'然后提取列我想(例如'coeftest(glm.object)[,c(1,3)]'),然后传递给'xtable()'。它不是太有限制,因为我通常不希望所有的xtable输出都是这样,并且喜欢将多个回归合并到一个表中的能力。如果我想将N或R^2添加到合并表中,我使用'print.xtable()'中的'add.to.row'选项。 – 2011-05-06 17:52:35

+0

嗨理查德!不幸的是,它不可能使用xtable(coeftest(Tobit.object))。但感谢输入添加行到表! – user734124 2011-05-06 19:20:06

+0

@ user734124 - 你应该能够用'[,1:4]'提取行。 – 2011-05-06 19:26:55

回答

4

这是另一个可以使用的功能。它是为lm定义的xtable的修改版本。 即我刚刚修改了函数xtable.summary.lm for tobit情况。 它也将对齐到其他xtable功能

xtable.summary.tobit <- 
function (x, caption = NULL, label = NULL, align = NULL, digits = NULL, 
display = NULL, ...) 
{ 
x <- data.frame(unclass(x$coef), 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) 
} 
## Now this should give you the desired result 
xtable(summary(tob)) 

希望它有助于获得期望的结果

+0

真棒和令人印象深刻!谢谢sayan! – user734124 2011-05-06 19:23:39

4

简短的答案是“将其转换为类xtable理解”。例如,

tmp <- summary(tob) 
str(tmp) ## a list 
names(tmp) ## the contents of tmp 
str(tmp$coefficients) ## the class of the coeffients table ("coeftest") 
is.matrix(tmp$coefficients) # TRUE 
class(tmp$coefficients) <- "matrix" 
xtable(tmp$coefficients) 
+0

这太棒了!谢谢 – user734124 2011-05-06 19:31:58