example of dataframe:
>
df
ID B C D
1 A 1 1 3
2 B 2 3 1
3 C 1 1 1
4 D 3 1 1
5 E 1 0 0
I've looped anova on various variables on a dataframe with this code (found online)
data:df
library(car)
LLA <- rep(NA, ncol (df))
sink("dfresults.doc")
for (i in 4:ncol(df)) {
column <- names(df[i])
contrasts(df$Group)<-contr.helmert(2)
contrasts(df$Gender)<-contr.helmert(2)
model= aov(df[,i] ~ Group + Gender, data= df)
SBCna=Anova(model, type="III")
tk=TukeyHSD(aov(df[,i] ~ Group + Gender, data= df))
print(column)
print(LLA)
print(tk)
}
sink()
(both Group and Gender are factorial) This produced a .doc file with the output of the analyses (very useful), sample of output:
[1] "variable"
Anova Table (Type III tests)
Response: df[, i]
Sum Sq Df F value Pr(>F)
(Intercept) 14313489 1 6922.5653 < 2.2e-16 ***
Group 280 1 0.1354 0.7133
Gender 40487 1 19.5809 1.635e-05 ***
Residuals 386652 187
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Tukey multiple comparisons of means
95% family-wise confidence level
Fit: aov(formula = df[, i] ~ Group + Gender, data =df)
$Group
diff lwr upr p adj
Group1-Group2 0.09016515 -14.99211 15.17244 0.990603
$Gender
diff lwr upr p adj
Male-Female 32.62016 18.02386 47.21646 1.75e-05
Now what I'd like to do is to create data.frame (that I can later save in .csv file) with the Sum f Square, f Value, P-values (the Pr(>F)) from the anova tables produced in the output.