Biostar Beta. Not for public use.
Question: How to run several one-way ANOVAs in R using on different categories?
1
Entering edit mode

Thanks for reading my post. I understand how to run non-parametric and parametric tests on three or more groups. But what I haven't figured out was a simple way of doing this multiple times from different categories I am interested in. In my dataset, I have gene copies for various antibiotic classes and I want to see if there are significant differences between samples in each antibiotic class. My data is currently in long format and looks like this:

Sample     Class      Value

  A       Macrolide  0.22 
  A       Macrolide  0.45
  A       Macrolide  0.63
  B       Macrolide  0.25
  B       Macrolide  0.28
 B       Macrolide  0.47
  C       Macrolide  0.22
 C       Macrolide  0.26
 C       Macrolide  0.29
  A       Ceph  0.32 
  A       Ceph  0.42
  A      Ceph  0.62
  B       Ceph  0.42
  B       Ceph  0.20
  B     Ceph  0.91
  C       Ceph  0.82
  C      MCeph  0.92

So essentially I want to do a one-way ANOVA for Macrolides and then another for Ceph etc etc. Can someone help me?

Thanks

ADD COMMENTlink 8 months ago infenit101 • 20 • updated 8 months ago zx8754 7.5k
1
Entering edit mode

Couldn't you just subset your dataframe for Class. i.e.

> library(lmPerm)
> data.df = read.csv("data.txt")

> aov.macro <- lmPerm::aovp(Value ~ Sample, subset(data.df,data.df$Class == "Macrolide")  )
[1] "Settings:  unique SS "
> summary(aov.macro)
Component 1 :
            Df R Sum Sq R Mean Sq Pr(Exact)
Sample       2 0.047089  0.023544         1
Residuals    6 0.115400  0.019233          


> ceph.macro <- lmPerm::aovp(Value ~ Sample, subset(data.df,data.df$Class == "Ceph")  )
[1] "Settings:  unique SS "
> summary(ceph.macro)
Component 1 :
            Df R Sum Sq R Mean Sq Pr(Exact)
Sample       2  0.10293  0.051467         1
Residuals    4  0.31087  0.077717          

> mceph.macro <- lmPerm::aovp(Value ~ Sample, subset(data.df,data.df$Class == "MCeph")  )
Error in ctrfn(levels(x), contrasts = contrasts) : 
  not enough degrees of freedom to define contrasts

Not sure if that was intentional but you have one class labelled as MCeph. Seems like a typo, or the data is truncated? If not, you don't have enough degrees of freedom to do an anova on that class (you'd need at least one of each Sample for the function to even work).

ADD COMMENTlink 8 months ago manuel.belmadani • 830
Entering edit mode
1

a small comment, you don't need to write lmPerm::aovp since you already loaded the library

ADD REPLYlink 8 months ago
H.Hasani
• 730
Entering edit mode
0

Thanks so much! Yes that was a typo.

Can I use the same subsetted dataframe for TukeyHSD?

ADD REPLYlink 8 months ago
infenit101
• 20
Entering edit mode
0

Most likely yes. I'm not familiar with the function but it looking at some examples, it looks like it needs takes an input of an anova object (generated by aov) so the lmPerm::aovp method might not work. Try it, and if it doesn't work use aov like in here (with subsetted dataframes):

https://www.rdocumentation.org/packages/stats/versions/3.6.0/topics/TukeyHSD

ADD REPLYlink 8 months ago
manuel.belmadani
• 830
1
Entering edit mode

Split data by class, then run your favourite function, see example:

# example data
df1 <- read.table(text = "Sample    Class   Value
A   Macrolide   0.22
A   Macrolide   0.45
A   Macrolide   0.63
B   Macrolide   0.25
B   Macrolide   0.28
B   Macrolide   0.47
C   Macrolide   0.22
C   Macrolide   0.26
C   Macrolide   0.29
A   Ceph    0.32
A   Ceph    0.42
A   Ceph    0.62
B   Ceph    0.42
B   Ceph    0.2
B   Ceph    0.91
C   Ceph    0.82
C   MCeph   0.92
A   MCeph   0.2
B   MCeph   0.72
C   MCeph   0.32
", header = TRUE, stringsAsFactors = FALSE)

# split and apply function, result is a named list:
lapply(split(df1, df1$Class), function(i){
  anova(lm(Value ~ Sample, data = i))
})

$Ceph
Analysis of Variance Table

Response: Value
          Df  Sum Sq  Mean Sq F value Pr(>F)
Sample     2 0.10293 0.051467  0.6622 0.5644
Residuals  4 0.31087 0.077717               

$Macrolide
Analysis of Variance Table

Response: Value
          Df   Sum Sq  Mean Sq F value Pr(>F)
Sample     2 0.047089 0.023544  1.2241 0.3582
Residuals  6 0.115400 0.019233               

$MCeph
Analysis of Variance Table

Response: Value
          Df Sum Sq Mean Sq F value Pr(>F)
Sample     2 0.1608  0.0804  0.4467 0.7268
Residuals  1 0.1800  0.1800
ADD COMMENTlink 8 months ago zx8754 7.5k

Login before adding your answer.

Similar Posts
Loading Similar Posts
Powered by the version 2.0