在 xtable(anova(...)) 中包括模型规格

发布于 2024-08-21 18:10:10 字数 396 浏览 2 评论 0原文

我有一堆对数线性模型,出于我们的目的,它们只是名为 mx, my, mzglm() 对象。我想要获得一个格式良好的xtable来分析偏差,所以我自然会想要执行xtable(anova(mx, my, mz, test = "Chisq"))< /代码>。

但是,xtable 的普通输出不包括模型规格。我想将这些包含在我正在运行的所有方差分析测试中,因此,如果我缺少一个参数来执行此操作,我可能只需要编写自己的解决方案即可。但查看帮助页面,似乎没有一种简单的方法来包含模型规格。

有什么想法吗?替代品?

如果有帮助的话,这是在 2.9.1 中使用 xtable 1.5-5 完成的。

I have a bunch of loglinear models, which, for our purposes will just be glm() objects called mx, my, mz. I want to get a nicely-formatted xtable of the analysis of deviance, so naturally I would want to perform xtable(anova(mx, my, mz, test = "Chisq")).

The vanilla output of xtable, however, doesn't include the model specifications. I'd like to include those for all the ANOVA tests I'm running, so if there is not a param I'm missing that does this I'll probably just have to hack up my own solution. But looking over the help page, there doesn't seem to be an easy way to include the model specifications.

Any thoughts? Alternatives?

If it helps this was done in 2.9.1 with xtable 1.5-5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心清如水 2024-08-28 18:10:10

如果 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 不会将其分成几行,然后我们需要制作 Latex 将公式包装在表格中。第一个可以通过使用 deparse 的 cutoff.width 参数来实现,第二个可以通过使用 Latex 中的 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")

结果不太漂亮,但你的公式也不太漂亮。在这种特殊情况下,我会使用(性别+出席+出生+政治)^3 - 表达要点并且更短。

if a is the anova table object, then attr(a,"heading") does contain the information you are looking for, but I couldn't figure out a nice way of extracting it. So I looked up the code of anova.glm, which directed me to the code of anova.lmlist to figure out how they put that information into the heading. This inspired to following solution:

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

Edit to cater for long formulas:
If you have long formulas, two changes are needed: first we have to make sure that deparse does not break it into lines, and then we need to make latex to wrap the formula in the table. The first can be achieved by using the cutoff.width argument of deparse, and the second by using a p{width} column type in latex. For example:

# 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")

The result is not overly pretty, but your formula is not pretty either. In this particular case I would use (sex + attend + birth + politics)^3 - gets the point across and is much shorter.

千柳 2024-08-28 18:10:10

我认为您想要获得 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()

它不像 LaTeX 表格那么漂亮,但它有模型公式......

I reckon that you want to get LaTeX table, but you can easily get HTML table with model formula.

# 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

You can do something like this:

# 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()

It's not as pretty as LaTeX table, but it has model formula...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文