2011-05-06 71 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