It's very much my first time on here, so go easy! Honestly with the amount of time I have spent troubleshooting, I could have done each by hand, but I'm determined to make this work....
I want to run 71 different one way ANOVA's comparing the volume of 71 brain regions of interest (ROI) with experimental group (group). There are three groups: control (1), resilient (2) and susceptible (3).
my tibble dataframe looks like this:
> head(df)
# A tibble: 6 x 74
ID group whole_brain amygdala arbor_vitae auditory_cortex bed_nuclei
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 01mc 1 495. 16.5 5.72 5.29 1.30
2 02mc 1 494. 16.8 5.95 5.29 1.30
3 6mc 1 491. 16.9 5.75 5.31 1.17
4 11mc 1 485. 16.5 5.70 5.04 1.31
5 14mc 1 491. 17.1 6.03 5.06 1.21
6 18mc 1 492. 16.5 6.07 5.12 1.23
I've written a for-loop through which I expect R to iterate through the name of the brain region (each column, excluding whole_brain), and then summarise an ANOVA table:
# One-Way ANOVA -----------------------------------------------------------
for(i in 4:ncol(df)){
column <- names(df[i]) # to print each ROI at the top of each ANOVA summary
avz <- aov(df[,i] ~ group, data = df) # Each ANOVA test iterating through each column of my dataframe
result <- summary(avz) # summarize each ANOVA in a table
print(column)
print(result)
}
Yet I get this error message:
Error in model.frame.default(formula = df[, i] ~ group, data = df, drop.unused.levels = TRUE) :
invalid type (list) for variable 'df[, i]'
When I swap the first object (df[,i]
) in the aov()
to a name of one of the ROIs (for example amygdala
), the output gives each iterative ROI column name, with the chosen ANOVA (amygdala ~ group, data=df
) underneath. This tells me the for loop works, but how do I overcome the error message and get it to systematically compare group to each ROI?
Any help would be amazing, even to point out if this is a totally old method and there is something way easier out there to use!
Thank you so much :)