re-ordering terms in an anova, in which one term is a PCA axis, one way works, the other does not

I have run a PCA, and am running the first axis into a simple univariate anova. I want to know if the order of terms matters, however when I run the second order of terms (with the PCA variation being includd second) i get the following error

Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) :
NA/NaN/Inf in 'y'
In addition: Warning message:
In storage.mode(v) <- "double" : NAs introduced by coercion

I have checked and there are no NAs in either my x or y. The PCA is a double, I coerced it into an integer, and it still did not work, giving me the same message.

Nut<-PCA.all$scores[,1]

summary.aov(aov(Nut~type, courtney.leaf1))
summary.aov(aov(type~Nut, courtney.leaf1))

The output:

> Nut<-PCA.all$scores[,1]
> summary.aov(aov(Nut~type, courtney.leaf1))
            Df Sum Sq Mean Sq F value Pr(>F)    
type         1 170.33  170.33     234 <2e-16 ***
Residuals   63  45.86    0.73                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> summary.aov(aov(type~Nut, courtney.leaf1))
Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'y'
In addition: Warning message:
In storage.mode(v) <- "double" : NAs introduced by coercion

As you can see, the rearrangement of terms where the categorical term is first does not work.

Thank you!

A one-way ANOVA must have a numeric variable as the dependent (aka response) variable and a single categorical variable as the independent (aka explanatory) variable. The dependent variable is always on the left of the tilde in aov() in R. The independent variable(s) are to the right of the tilde.

You ended up trying to put a categorical variable as the dependent variable instead of numeric, and got the error because of it.

If your research question is about a difference in mean PCA variable among groups and you want to use an ANOVA to help answer that question, you can't switch out the categorical dependent with your numeric independent variable.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.