将TukeyHSD测试的结果与基本描述性统计数据按因子组导出到Excel。

huangapple go评论59阅读模式
英文:

Exporting the results of TukeyHSD test with basic descriptive statistics by factor group to excel

问题

我想要将TukeyHSD测试的结果与基本描述性统计数据按分组因子导出到Excel文件。我有一个包含16个水平的因子的数据框:

main.data <- runif(256)
main.factor <- c("1st control", "2nd control", "Control A", "Control B",
               "Variant A", "Variant B", "Variant C",
               "1st Variant", "2nd Variant", "3rd Variant",
               "Treatment 1", "Treatment 2", "Treatment 3",
               "1st Treatment", "2nd Treatment", "3rd Treatment")

df <- data.frame(experiment=rep(main.factor, each=16), Main.value=main.data)
df$experiment <- factor(df$experiment, levels = c("1st control", "2nd control", "Control A", "Control B",
                                                "Variant A", "Variant B", "Variant C",
                                                "1st Variant", "2nd Variant", "3rd Variant",
                                                "Treatment 1", "Treatment 2", "Treatment 3",
                                                "1st Treatment", "2nd Treatment", "3rd Treatment"))

我正在使用单因素方差分析(ANOVA),然后进行TukeyHSD测试:

model.1 <- lm(Main.value ~ experiment, data = df)
anova(model.1)
library(agricolae)
result <- HSD.test(model.1, "experiment", group = F)

我想要导出TukeyHSD测试的结果和基本描述性统计数据按分组因子。我尝试过以下代码:

result.1 <- result$comparison
library("xlsx")
write.xlsx(result.1, file = "main.result.xlsx", col.names = TRUE, row.names = TRUE, sheetName = "TukeyHSD_test")

主要问题是比较结果以字母顺序排列,但我想使用以下顺序:

factor.order <- c("1st control", "2nd control", "Control A", "Control B", "Variant A", "Variant B", "Variant C", "1st Variant", "2nd Variant", "3rd Variant", "Treatment 1", "Treatment 2", "Treatment 3", "1st Treatment", "2nd Treatment", "3rd Treatment")

是否可以更改排序?是否可以导出结果到Excel时添加空行?我想要在第一个因子水平与其他因子水平进行比较时添加空行(第一个空行),第二个因子水平与其他进行比较时添加空行(第二个空行),第三个因子水平与其他进行比较时添加空行(第三个空行)等。

当我导出按分组因子的描述性统计结果时,它也按字母顺序排列。如何更改?

result.2 <- result$mean
write.xlsx(result.2, file = "main.result.xlsx", col.names = TRUE, row.names = TRUE,
       sheetName = "descriptive_statistics", append = TRUE)

是否有人可以帮助我?非常感谢。

英文:

I would like to export the results of TukeyHSD test with basic descriptive statistics by grouping factor to excel file. I have a data frame with factor containing 16 levels:

main.data &lt;- runif(256) 
main.factor &lt;- c(&quot;1st control&quot;, &quot;2nd control&quot;, &quot;Control A&quot;, &quot;Control B&quot;,
           &quot;Variant A&quot;, &quot;Variant B&quot;, &quot;Variant C&quot;,
           &quot;1st Variant&quot;, &quot;2nd Variant&quot;, &quot;3rd Variant&quot;,
           &quot;Treatment 1&quot;, &quot;Treatment 2&quot;, &quot;Treatment 3&quot;,
           &quot;1st Treatment&quot;, &quot;2nd Treatment&quot;, &quot;3rd Treatment&quot;)

df &lt;- data.frame(experiment=rep(main.factor,each=16), Main.value=main.data)
df$experiment &lt;- factor(df$experiment, levels = c(&quot;1st control&quot;, &quot;2nd control&quot;, &quot;Control A&quot;, &quot;Control B&quot;,
                                                &quot;Variant A&quot;, &quot;Variant B&quot;, &quot;Variant C&quot;,
                                                &quot;1st Variant&quot;, &quot;2nd Variant&quot;, &quot;3rd Variant&quot;,
                                                &quot;Treatment 1&quot;, &quot;Treatment 2&quot;, &quot;Treatment 3&quot;,
                                                &quot;1st Treatment&quot;, &quot;2nd Treatment&quot;, &quot;3rd Treatment&quot;))

I am using one-way ANOVA and after TukeyHSD test:

model.1 &lt;- lm(Main.value~experiment, data=df)
anova(model.1)
library(agricolae)
result &lt;- HSD.test(model.1, &quot;experiment&quot;, group=F); result

I would like to export results of TukeyHSD test and basic descriptive statistics by grouping factor. I have tried this:

result.1 &lt;- result$comparison
library(&quot;xlsx&quot;)
write.xlsx(result.1, file = &quot;main.result.xlsx&quot;, col.names = TRUE, row.names = TRUE, sheetName = &quot;TukeyHSD_test&quot;)

The main problem is that comparison is in alphabetical order, but I want to use this order:

factor.order &lt;- c(&quot;1st control&quot;, &quot;2nd control&quot;, &quot;Control A&quot;, &quot;Control B&quot;, &quot;Variant A&quot;, &quot;Variant B&quot;, &quot;Variant C&quot;, &quot;1st Variant&quot;, &quot;2nd Variant&quot;, &quot;3rd Variant&quot;, &quot;Treatment 1&quot;, &quot;Treatment 2&quot;, &quot;Treatment 3&quot;, &quot;1st Treatment&quot;, &quot;2nd Treatment&quot;, &quot;3rd Treatment&quot;)

It is possible to change? It can be also export result to excel with empty row? I would like to add empty row when the first factor level was compaired to other factor levels (first empty row), the second factor level was compaired to others (second empty row), third factor level was compaired to other (third empty row) etc.

When I export the result of descriptive statistics by grouping factor, it is also in alphabetical order. How can I change?

result.2 &lt;- result$mean
write.xlsx(result.2, file = &quot;main.result.xlsx&quot;, col.names = TRUE, row.names = TRUE,
       sheetName = &quot;descriptive_statistics&quot;, append = TRUE)

Can somebody help me? Thank You very much.

答案1

得分: 1

Here is the translated code:

library(agricolae)
library(tidyverse)
library(xlsx)

main.data <- runif(256) 
main.factor <- c("1st control", "2nd control", "Control A", "Control B",
                 "Variant A", "Variant B", "Variant C",
                 "1st Variant", "2nd Variant", "3rd Variant",
                 "Treatment 1", "Treatment 2", "Treatment 3",
                 "1st Treatment", "2nd Treatment", "3rd Treatment")

df <- data.frame(experiment=rep(main.factor, each=16), Main.value=main.data)
df$experiment <- factor(df$experiment, levels = c("1st control", "2nd control", "Control A", "Control B",
                                                  "Variant A", "Variant B", "Variant C",
                                                  "1st Variant", "2nd Variant", "3rd Variant",
                                                  "Treatment 1", "Treatment 2", "Treatment 3",
                                                  "1st Treatment", "2nd Treatment", "3rd Treatment"))

factor.order <- c("1st control", "2nd control", "Control A", "Control B", "Variant A", "Variant B", "Variant C", "1st Variant", "2nd Variant", "3rd Variant", "Treatment 1", "Treatment 2", "Treatment 3", "1st Treatment", "2nd Treatment", "3rd Treatment")

model.1 <- lm(Main.value~experiment, data=df)
result <- HSD.test(model.1, "experiment", group=F)

result.1 <- result$comparison
result.2 <- result$mean

result_1_sheet = result.1 %>%
  rownames_to_column("grouping_factor") %>%
  separate(grouping_factor, into = c("factor_a", "factor_b"), sep = " - ") %>%
  mutate(
    factor_a = fct_drop(factor(factor_a, levels = factor.order)),
    factor_b = fct_drop(factor(factor_b, levels = factor.order))
  ) %>%
  arrange(factor_a, factor_b) %>%
  group_by(factor_a) %>%
  group_modify(~ add_row(.x)) %>%
  mutate(
    factor_a = as.character(factor_a),
    factor_a = ifelse(is.na(factor_b), NA, factor_a)
  ) %>%
  as.data.frame()

result_2_sheet = result.2 %>%
  rownames_to_column("group") %>%
  mutate(group = fct_drop(factor(group, levels = factor.order)) %>%
  arrange(group) %>%
  as.data.frame()

write.xlsx(result_1_sheet, file = "main.result.xlsx", col.names = TRUE, sheetName = "TukeyHSD_test")
write.xlsx(result_2_sheet, file = "main.result.xlsx", col.names = TRUE, row.names = FALSE,
           sheetName = "descriptive_statistics", append = TRUE)

I have translated the code and left out any non-code content.

英文:
library(agricolae)
library(tidyverse)
library(xlsx)
main.data &lt;- runif(256) 
main.factor &lt;- c(&quot;1st control&quot;, &quot;2nd control&quot;, &quot;Control A&quot;, &quot;Control B&quot;,
&quot;Variant A&quot;, &quot;Variant B&quot;, &quot;Variant C&quot;,
&quot;1st Variant&quot;, &quot;2nd Variant&quot;, &quot;3rd Variant&quot;,
&quot;Treatment 1&quot;, &quot;Treatment 2&quot;, &quot;Treatment 3&quot;,
&quot;1st Treatment&quot;, &quot;2nd Treatment&quot;, &quot;3rd Treatment&quot;)
df &lt;- data.frame(experiment=rep(main.factor,each=16), Main.value=main.data)
df$experiment &lt;- factor(df$experiment, levels = c(&quot;1st control&quot;, &quot;2nd control&quot;, &quot;Control A&quot;, &quot;Control B&quot;,
&quot;Variant A&quot;, &quot;Variant B&quot;, &quot;Variant C&quot;,
&quot;1st Variant&quot;, &quot;2nd Variant&quot;, &quot;3rd Variant&quot;,
&quot;Treatment 1&quot;, &quot;Treatment 2&quot;, &quot;Treatment 3&quot;,
&quot;1st Treatment&quot;, &quot;2nd Treatment&quot;, &quot;3rd Treatment&quot;))
factor.order &lt;- c(&quot;1st control&quot;, &quot;2nd control&quot;, &quot;Control A&quot;, &quot;Control B&quot;, &quot;Variant A&quot;, &quot;Variant B&quot;, &quot;Variant C&quot;, &quot;1st Variant&quot;, &quot;2nd Variant&quot;, &quot;3rd Variant&quot;, &quot;Treatment 1&quot;, &quot;Treatment 2&quot;, &quot;Treatment 3&quot;, &quot;1st Treatment&quot;, &quot;2nd Treatment&quot;, &quot;3rd Treatment&quot;)
model.1 &lt;- lm(Main.value~experiment, data=df)
result &lt;- HSD.test(model.1, &quot;experiment&quot;, group=F)
result.1 &lt;- result$comparison
result.2 &lt;- result$mean
result_1_sheet = result.1 %&gt;% 
rownames_to_column(&quot;grouping_factor&quot;) %&gt;% 
separate(grouping_factor, into = c(&quot;factor_a&quot;, &quot;factor_b&quot;), sep = &quot; - &quot;) %&gt;%
mutate(
factor_a = fct_drop(factor(factor_a, levels = factor.order)),
factor_b = fct_drop(factor(factor_b, levels = factor.order))
) %&gt;% 
arrange(factor_a, factor_b) %&gt;% 
group_by(factor_a) %&gt;% 
group_modify(~ add_row(.x)) %&gt;% 
mutate(
factor_a = as.character(factor_a),
factor_a = ifelse(is.na(factor_b), NA, factor_a)
) %&gt;% 
as.data.frame()
result_2_sheet = result.2 %&gt;% 
rownames_to_column(&quot;group&quot;) %&gt;% 
mutate(group = fct_drop(factor(group, levels = factor.order))) %&gt;% 
arrange(group) %&gt;% 
as.data.frame()
write.xlsx(result_1_sheet, file = &quot;main.result.xlsx&quot;, col.names = TRUE, sheetName = &quot;TukeyHSD_test&quot;)
write.xlsx(result_2_sheet, file = &quot;main.result.xlsx&quot;, col.names = TRUE, row.names = FALSE,
sheetName = &quot;descriptive_statistics&quot;, append = TRUE)

huangapple
  • 本文由 发表于 2023年3月4日 05:04:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定