'How to loop with Post-hoc stat tests in R
I have a dataframe:
| S | molecule 1 | molecule 2 | molecule 3 |
|---|---|---|---|
| Sample 1 | 0.1 | 0.2 | 0.3 |
| Sample 2 | 0.2 | 0.3 | 0.4 |
| Negative | 0.5 | 0.5 | 0.5 |
how do I run a dunnetts test comparing sample 1 with negative, sample 2 with negative for each molecule (1,2,3) using loops?
I dont want to do it manually by typing dunnettTest(Molecule 1 ~ S, data = my_data) for each molecule. I actually have over 40 molecules in my real data
Any one can provide help that would be great!
Solution 1:[1]
Here's one possibility. This uses the column numbers of the variables you want to compare. Note, that the example data you provided didn't work because there aren't enough observations per group, so I just made a copy of the data and pasted it to the bottom, replicating each observation twice.
library(DescTools)
dat <- tibble::tribble(
~S, ~molecule1, ~molecule2, ~molecule3,
"Sample 1", 0.1, 0.2, 0.3,
"Sample 2", 0.2, 0.3, 0.4,
"Negative", 0.5, 0.5, 0.5)
dat <- dplyr::bind_rows(dat, dat)
## loop over columns 2-4
for(i in 2:4){
print(DunnettTest(dat[[i]], as.factor(dat$S), control="Negative"))
}
#>
#> Dunnett's test for comparing several treatments with a control :
#> 95% family-wise confidence level
#>
#> $Negative
#> diff lwr.ci upr.ci pval
#> Sample 1-Negative -0.4 -0.4 -0.4 <2e-16 ***
#> Sample 2-Negative -0.3 -0.3 -0.3 <2e-16 ***
#>
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#>
#> Dunnett's test for comparing several treatments with a control :
#> 95% family-wise confidence level
#>
#> $Negative
#> diff lwr.ci upr.ci pval
#> Sample 1-Negative -0.3 -0.3 -0.3 <2e-16 ***
#> Sample 2-Negative -0.2 -0.2 -0.2 <2e-16 ***
#>
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#>
#> Dunnett's test for comparing several treatments with a control :
#> 95% family-wise confidence level
#>
#> $Negative
#> diff lwr.ci upr.ci pval
#> Sample 1-Negative -0.2 -0.2 -0.2 <2e-16 ***
#> Sample 2-Negative -0.1 -0.1 -0.1 <2e-16 ***
#>
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Or, alternatively with lapply():
lapply(2:4, function(i)DunnettTest(dat[[i]], as.factor(dat$S), control="Negative"))
#> [[1]]
#>
#> Dunnett's test for comparing several treatments with a control :
#> 95% family-wise confidence level
#>
#> $Negative
#> diff lwr.ci upr.ci pval
#> Sample 1-Negative -0.4 -0.4 -0.4 <2e-16 ***
#> Sample 2-Negative -0.3 -0.3 -0.3 <2e-16 ***
#>
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#>
#> [[2]]
#>
#> Dunnett's test for comparing several treatments with a control :
#> 95% family-wise confidence level
#>
#> $Negative
#> diff lwr.ci upr.ci pval
#> Sample 1-Negative -0.3 -0.3 -0.3 <2e-16 ***
#> Sample 2-Negative -0.2 -0.2 -0.2 <2e-16 ***
#>
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#>
#> [[3]]
#>
#> Dunnett's test for comparing several treatments with a control :
#> 95% family-wise confidence level
#>
#> $Negative
#> diff lwr.ci upr.ci pval
#> Sample 1-Negative -0.2 -0.2 -0.2 <2e-16 ***
#> Sample 2-Negative -0.1 -0.1 -0.1 <2e-16 ***
#>
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Created on 2022-04-08 by the reprex package (v2.0.1)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DaveArmstrong |
