R:几个模型并排的方差分析输出

R:几个模型并排的方差分析输出,r,export,output,anova,R,Export,Output,Anova,我正试图以一种类似于我通常对回归表所做的方式,获得几个ANOVA的好的出版物准备输出。代码看起来有点像: head(iris) library(car) # run model 1 lm1 <- lm(Sepal.Length ~ Species, data = iris) summary(lm1) a1 <- Anova(lm1) # run model 2 lm2 <- lm(Petal.Width ~ Species, data = iris) summary(lm2)

我正试图以一种类似于我通常对回归表所做的方式,获得几个ANOVA的好的出版物准备输出。代码看起来有点像:

head(iris)
library(car)

# run model 1
lm1 <- lm(Sepal.Length ~ Species, data = iris)
summary(lm1)
a1 <- Anova(lm1)
# run model 2
lm2 <- lm(Petal.Width ~ Species, data = iris)
summary(lm2)        # long format (regression type output)
a2 <- Anova(lm2)    # short format (anova type output, which I need)

# what I usually do for regression outputs:
library(stargazer)
stargazer(lm1, lm2, type = "html", out ="iris.html")
# which yields a nice table with models side by side, including dependent variable names for each model; 
# this one I'd export as .html to Word and process there

# trying a similar joint table for anova type output:
stargazer(a1, a2, type = "html", out ="iris2.html")
# ... but that yields 2 separated tables and they can't be distinguished by dependent variables etc

# same problem here:
table <- rbind(a1, a2)
write.csv(as.data.frame(table), file = "iris2.csv")
# when I do this, I only get the same two tables underneath each other with their columnwise headers, 
# but without the distinguishing dependent variables 
头部(虹膜)
图书馆(汽车)
#运行模式1

lm1如果您对定期生成HTML和Latex表感兴趣,我建议您查看
库(kableExtra)
。这里是一个关于如何使用的优秀教程

下面是一个如何创建html表的示例。您将获得以下结果:

#将方差分析结果转换为数据帧可以更轻松地进行名称操作
a1_新建%#使用标签对行进行分组
打包行(,“花瓣宽度”,3,4)#使用标签将行分组
如果你想让这些值并排出现,这里有一种简单的方法来完成它

anova_results2 <- data.frame(rbind(c("Sum Sq", "Df", "F value", "Pr(>F)", 
                                     "Sum Sq", "Df", "F value", "Pr(>F)"), 
                                   cbind(round(a1_new, 2), round(a2_new,2)))) 
colnames(anova_results2) <- c("", "", "", "","", "", "", "")
row.names(anova_results2)[1] <- ""

anova_results2 %>% kable("html") %>% 
  kable_styling(bootstrap_options = "striped", full_width = F) %>%
  add_header_above(c("", "Sepal Length" = 4, "Petal Width" = 4))
anova_结果2%
在上面加上标题(c(“,“萼片长度”=4,“花瓣宽度”=4))


注意:为了得到一个功能正常的Latex表,您需要进行不同的修改。我确实认为,从长远来看,如果您想创建出版物质量表,在RMarkdown中使用Latex可能是最好的方法。

为什么不使用非常好的Latex解决方案并将.pdf放入word中?R markdown是怎么回事?谢谢,这目前确实有效,看起来不错。我曾希望有一个更自动化的解决方案,不需要我逐个键入变量名(因为有这么多模型,很容易不小心弄乱顺序…)。此外,我还错过了stargazer的一些不错的功能,比如小p值的自动缩写、带星号的重要性级别,这些级别的自动注释等等。是的,
kable
以这种方式可能有点笨重,这可能会令人沮丧。但有时候,拥有灵活性确实很有帮助。这是我第一次听说《观星者》,似乎它有它的用途!
anova_results2 <- data.frame(rbind(c("Sum Sq", "Df", "F value", "Pr(>F)", 
                                     "Sum Sq", "Df", "F value", "Pr(>F)"), 
                                   cbind(round(a1_new, 2), round(a2_new,2)))) 
colnames(anova_results2) <- c("", "", "", "","", "", "", "")
row.names(anova_results2)[1] <- ""

anova_results2 %>% kable("html") %>% 
  kable_styling(bootstrap_options = "striped", full_width = F) %>%
  add_header_above(c("", "Sepal Length" = 4, "Petal Width" = 4))