我正在尝试制作一个表格,输出通常通过2-way anova进行分析的大型研究的摘要统计信息,查看两个变量的主效应以及交互效应。
我希望能够快速运行统计数据,并以易于阅读的格式输出,如果有漂亮的格式那就更好了。
我已经能够获得2-way anova输出,并且我还使用了gtsummary包和tbl_summary
来制作表格。但是,我无法弄清楚如何按多个变量分组。我的解决方案是创建一个新变量,将两个独立变量组合起来,以便将数据分成正确的组。
可复现的示例如下。
我想知道是否有一种方法可以制作与我所拥有的均值(sem)输出相同的表格,但具有我的2-way anova结果(也在下面粘贴)。在这个titanic示例中,我想要一个针对“性别”主效应的P值列,下一列是“上船”的主效应的p值,然后是一个交互的p值。
有什么想法吗?
library(titanic)
library(tidyverse)
library(gtsummary)
library(plotrix) #has a std.error function
##I really want to look at a 2-way anova, looking for the p-value for Sex, Embarked, and their interaction.
#This code just allows me to make a table with the 4 columns I want, but of course it now won't do the correct stats.
df <- titanic_train %>%
filter(Embarked != "C" & Embarked != "") %>%
mutate(grp = paste(Sex, Embarked)) #add a new column that combines Sex & Pclass
#code to make my table
table1 <- df %>%
select(grp, Age, Fare, Survived) %>%
tbl_summary(
by = grp, ##can't figure out a way to put 2 variables here (Sex & Embarked)
missing = "ifany",
statistic = all_continuous() ~ "{mean} ({std.error})",
digits = all_continuous() ~ 1) %>% #this puts 1 decimal place for all values
modify_header(stat_by = md("**{level}**<br>N = {n}")) %>%
bold_labels() %>%
modify_spanning_header(all_stat_cols() ~ "**These are the Columns I Want**") %>%
add_p(test = everything() ~ "aov", ##This is a 1-way ANOVA, but I need 2 variables
)
table1
#these are the p-values I want in my table:
two_way_anova_age <- aov(Age ~ Sex * Embarked, data = df)
summary(two_way_anova_age)
two_way_anova_fare <- aov(Fare ~ Sex * Embarked, data = df)
summary(two_way_anova_fare)
two_way_anova_surv <- aov(Survived ~ Sex * Embarked, data = df)
summary(two_way_anova_surv)
remotes::install_github("ddsjoberg/gtsummary")
进行安装,检查包版本(获取1.3.7.9010),但是我在"could not find "tbl_strata"和"sound not find "modify_fmt_fun"两个方面都遇到了错误。 - Erin Gilesremotes::install_github("ddsjoberg/gtsummary", force = TRUE)
-- 我想那是唯一的区别。 - Erin Gilesdf <- titanic_train %>% select(Sex, Embarked, (1:10)) %>% select(!("Name" | "Ticket")) %>% filter(Embarked != "") # 删除空的Embarked状态
-- 问题是如何在bind_rows部分上执行类似的操作。老实说,这个问题不需要太漂亮 - 可以循环遍历一堆双向方差分析并导出到CSV。 - Erin Giles