I am trying to analyse variance of difference between pH values measures by two different methods. I have repeated measurements from a number of subjects and I want to analyse within subject variance and between subject variance, but when I use the aov() function in R degrees of freedom seems to be incorrect.

My dataset looks like this:

 id <- c(1,1,1,2,2,3,3,3,3,6,6,6,8,8,9,9,9,9)
 ph1 <- c(7,6,6,8,8,8,4,3,5,6,6,7,8,8,5,4,3,6) # This is the gold standard method. 
 ph2 <- c(8,6,7,8,7,8,5,4,5,6,6,7,8,9,4,3,4,6)
 df <- as.data.frame(cbind(id,ph1,ph2))

And when I calculate one-way ANOVA I get this:

 res.aov <- aov((ph1-ph2) ~ id, data = df)
             Df Sum Sq Mean Sq F value Pr(>F)
 id           1  0.579  0.5792    1.17  0.295
 Residuals   16  7.921  0.4950     

Should Df for between groups analysis be 5 or have I misunderstod something?


You forgot to factorize your id variable, ANOVA doesn't do that automatically

res.aov <- aov((ph1-ph2) ~ factor(id), data = df)
            Df Sum Sq Mean Sq F value Pr(>F)
factor(id)   5  3.083  0.6167   1.366  0.303
Residuals   12  5.417  0.4514
  • 1
    $\begingroup$ Thank you! Now it works. $\endgroup$ Dec 14 '19 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.