2

我对 R 语言比较陌生,并使用它来分析我的数据。我正在使用一个名为“agricolae”的包及其 HSD.test 组件来输出 tukeyHSD 结果基于 anova 处理它。我已经简化了下面的代码,但本质上 tukey 代码是在一个 for 循环中运行的,该循环通过一个因素/交互列表运行。它工作正常,但是,我只希望循环在相应因子的方差分析结果显着的地方运行。(即 <= 0.05)。

f1 <- as.formula(paste(m,'~ Treatment + Genotype + GENOTYPExTREATMENT', sep = ''))
anova.result <- aov(f1, data = licor.data2)

treatment.list <- c('Treatment','Genotype', 'GENOTYPExTREATMENT')
        
for(t in treatment.list){ 
   tukey.result <- HSD.test(anova.result, trt = t)$groups
}

使用rownames(summary(anova.result)[[1]]) 退货[1] "Treatment " "Genotype " "GENOTYPExTREATMENT" "Residuals "str(summary(anova.result)[[1]])退货:

Classes ‘anova’ and 'data.frame':   4 obs. of  5 variables:
 $ Df     : num  4 6 24 99
 $ Sum Sq : num  0.01215 0.0019 0.00255 0.00527
 $ Mean Sq: num  3.04e-03 3.16e-04 1.06e-04 5.33e-05
 $ F value: num  57.02 5.94 1.99 NA
 $ Pr(>F) : num  7.22e-25 2.50e-05 9.68e-03 NA

只是想知道是否有人知道如何构造一个whenorif/else语句,它只会运行循环代码,其中相应的 't' 循环因子或交互的 $Pr(>F) 值小于 0.05。

提前致谢,

卢克

4

2 回答 2

0

我使用 if 语句到达那里,并将 for 循环代码更改1:length为输出列表中的数字位置而不是文本本身。这反过来意味着我必须更新 HSD.test 函数中的处理,而不是输出一个数字来代替“t”,而是使用treatment.list[t]希望它有意义的文本!

for(t in 1:length(treatment.list)){ 
   if(summary(anova.result)[[1]][['Pr(>F)']][[t]] <= 0.05){
   tukey.result <- HSD.test(anova.result, trt = treatment.list[t])$groups


 } # end of if statement
} # end of for loop
于 2020-07-31T13:11:14.663 回答
0

这是一个使用的解决方案dplyrDescTools您可能还想查看我的Plot2WayANOVA内置函数...

library(dplyr)
library(DescTools)

mtcars$am <- factor(mtcars$am)
mtcars$cyl <- factor(mtcars$cyl)
MyAOV <- aov(mpg ~ am * cyl, mtcars)

sigfactors <- 
  filter(summary(MyAOV)[[1]], `Pr(>F)` <= 1 - .95) %>% 
  rownames %>% 
  trimws

if (length(sigfactors) > 0) {
  posthocresults <- PostHocTest(MyAOV,
                                method = "hsd",
                                conf.level = .95,
                                which = sigfactors)
} else {
  posthocresults <- "No signfiicant effects"
}

posthocresults

哪个产量

  Posthoc multiple comparisons of means : Tukey HSD 
    95% family-wise confidence level

$am
        diff  lwr.ci   upr.ci    pval    
1-0 7.244939 5.00149 9.488388 4.8e-07 ***

$cyl
         diff     lwr.ci     upr.ci    pval    
6-4 -4.756706  -8.399753 -1.1136596  0.0088 ** 
8-4 -7.329581 -10.365453 -4.2937086 7.2e-06 ***
8-6 -2.572874  -6.060826  0.9150773  0.1788    

---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
于 2020-07-31T12:17:41.750 回答