r - R:并排几个模型的 Anova 输出
问题描述
我正在尝试以与我通常为回归表所做的类似的方式获得几个 anovas 的良好发布就绪输出。代码看起来有点像:
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
由于我将不得不一遍又一遍地处理更多模型,因此我希望 Word 中的后期处理尽可能少。我知道使用 LaTeX 有非常好的解决方案,但不幸的是,由于合著者,这些完全不可能。我玩弄了“xtable”、“pixiedust”和“export”包,但没能得到我想要的结果。
希望有人能帮忙,在此先感谢!
解决方案
library(kableExtra)
如果您有兴趣定期生成 HTML 和 Latex 表格,我建议您研究一下。这是一个关于HTML和Latex使用的优秀教程。
下面是一个如何创建 html 表格的示例。你会得到以下结果:
# Convert ANOVA results into dataframes allows for easier name manipulation
a1_new <- data.frame(a1)
a2_new <- data.frame(a2)
# Putting all into one dataframe/table
anova_results <- data.frame(cbind(c("Species", "Residuals", "Species", "Residuals"),
rbind(a1_new, a2_new)))
colnames(anova_results) <- c("", "Sum Sq", "Df", "F value", "Pr(>F)")
row.names(anova_results) <- NULL
# create HTML table using kableExtra
library(kableExtra)
anova_results %>% kable("html", digits=2) %>%
kable_styling(bootstrap_options = "striped", full_width = F) %>%
pack_rows(., "Sepal Length", 1, 2) %>% # groups rows with label
pack_rows(., "Petal Width", 3, 4) # groups rows with label
如果你想要并排的价值观,这是一种完成它的黑客方法......
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))
注意:为了获得正常工作的 Latex 表,您需要进行不同的修改。我确实认为,从长远来看,如果您想创建出版质量表,在 RMarkdown 中使用 Latex 可能是最好的方法。
推荐阅读
- julia - 初始化数组以存储对象
- google-apps-script - transfer ownership of spreadsheet file
- react-native - 如何在有条件的本地反应中使用导航
- python - Python Boto3 get_metric_statistics() 失败
- google-contacts-api - Does v3 of Google reCAPTCHA ever prompt suspicious with reCAPTCHA challenge?
- string - Pyspark DD-MMM-YYYY (string format) to timestamp
- adb - adb 是由远程设备自动连接的,但我没有使用 adb connect
- ssh - 无法连接到远程扩展主机服务器(错误:连接错误:版本不匹配,客户端被拒绝。)
- java - How can i create Path space markup element in Anylogic via Code
- android - How about Anroid DownloadManager modify the update interval