我有一大堆的对数线性模型,这对于我们来说也只是glm()对象称为mx, my, mz的。我想越轨行为分析的一个很好的格式xtable,所以很自然我会想执行xtable(anova(mx, my, mz, test = "Chisq"))

xtable的香草输出,但是,不包括模型规格。我想包括所有我运行ANOVA测试,所以如果没有一个PARAM我缺少做这个我可能就不得不砍了自己的解决方案。但看在帮助页面,那里似乎没有一种简单的方法,包括型号规格。

任何想法?替代?

如果它有助于这是在完成2.9.1与xtable 1.5-5。

有帮助吗?

解决方案

如果a是方差分析表对象,然后attr(a,"heading")确实包含你正在寻找的信息,但我无法弄清楚它提取的一种很好的方式。所以,我抬头anova.glm的代码,它指示我anova.lmlist的代码,以弄清楚他们是如何把这些信息输入标题。这激发到下述溶液:

# fake data
x <- 1:10
y <- x+ rnorm(10)

# two models
m1 <- glm(y~x)
m2 <- glm(y~x+I(x^2))
a <- anova(m1, m2)  # anova object to be printed

# get model formulas
flas <- sapply(list(m1,m2), function(x)paste(deparse(x$formula)))
rownames(a) <- flas  # add formulas as rownames

# convert to latex
xtable(a)

修改,以满足长公式:结果 如果你有很长的公式,需要两个变化:首先我们要确保deparse不分解成线,然后我们需要使乳胶包裹在表中的公式。第一可通过使用deparse的cutoff.width参数,所述第二通过使用胶乳一个p{width}列类型来实现。例如:

# add long formula
m2$formula <- freq ~ sex + attend + birth + politics + sex:attend + sex:birth + 
              sex:politics + attend:birth + attend:politics + birth:politics + 
              sex:attend:birth + sex:attend:politics + sex:birth:politics +
              attend:birth:politics
a <- anova(m1, m2) 

# use a large width
flas <- sapply(list(m1,m2), 
               function(x)paste(deparse(x$formula, cutoff.width=500)))
rownames(a) <- flas  # add formulas as rownames

# convert to latex with first column wrapped in a 5cm wide parbox
xtable(a, align="p{5cm}rrrr")

结果是不是太漂亮,但你的公式是不漂亮任一。在我会用(sex + attend + birth + politics)^3这种特殊情况下 - 得到跨越点和短得多。

其他提示

我觉得要得到LaTeX的表格,但你可以很容易地用HTML模型公式表。

# if we presuppose that <b>a</b> is object from @Aniko's reply
> class(a)
[1] "anova"      "data.frame"
# after doing a bit of that sapply magic you get
> a
Analysis of Deviance Table

Model 1: y ~ x
Model 2: y ~ x + I(x^2)
               Resid. Df Resid. Dev Df Deviance
y ~ x                  8     15.503            
y ~ x + I(x^2)         7     12.060  1   3.4428

您可以做这样的事情:

# load xtable library
library(xtable)
# sink output to html file
sink("~/anova_specs.html")  # suppose you're running R on Linux "~/"
print(xtable(a), type = "html")
sink()

这不是漂亮乳胶表,但它有模型公式...

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top