0

I am trying to reorder factors in a simple lm/aov, so I can vary the intercept.

I have tried various functions to reorder but the lm always defaults to the same intercept (the most significant). To give context have growth plants under 3 treatments, High, Med and Low nutrients

growth1<-aov(log(size)~treat,DF) 

I have tried relevel()

relevel(DF$treat, ref = "Low")

creating a DF where ordering is false

DF$treat <- factor( DF$treat , ordered = FALSE )

rearranging

arrange(DF,desc(treat))

and finally creating an entirely new .csv

But the High treatment is always the intercept irrespective of the order. I want to look at the treatment effects relative to Low nutrients. I know I can do post hoc with Tukeys to get the interactions but am interested in just using the lm comparisons. Clearly it is to do with the lm/aov (I have done both). Wondering if anyone can help point out what I am missing here? I know, clearly something very fundamental.....

1 Answers1

0

relevel is your friend.

lm(Sepal.Length ~  Petal.Width + relevel(Species, ref="virginica"), iris)
# Call:
#   lm(formula = Sepal.Length ~ Petal.Width + relevel(Species, ref = "virginica"), 
#      data = iris)
# 
# Coefficients:
#                               (Intercept)                                    Petal.Width  
#                                   4.73036                                        0.91690  
# relevel(Species, ref = "virginica")setosa  relevel(Species, ref = "virginica")versicolor  
#                                   0.05009                                       -0.01017  

aov(Sepal.Length ~  Petal.Width + relevel(Species, ref="virginica"), iris)
# Call:
#   aov(formula = Sepal.Length ~ Petal.Width + relevel(Species, ref = "virginica"), 
#       data = iris)
# 
# Terms:
#                 Petal.Width relevel(Species, ref = "virginica") Residuals
# Sum of Squares     68.35344                             0.03460  33.78029
# Deg. of Freedom           1                                   2       146
# 
# Residual standard error: 0.4810113
# Estimated effects may be unbalanced

Of course you may also use it beforehand to get a cleaner output.

iris <- transform(iris, Species.r=relevel(Species, ref="virginica"))
jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thanks @jay.sf I totally missed you could add relevel in the model. Only problem is I keep getting that same error Error in relevel.default(Merge, ref = "Provenance") : 'relevel' only for (unordered) factors – Chon Doyle Jul 15 '20 at 22:33
  • Tried making the factors unordered- which worked BUT still "High" treatment is the intercept. If this is the way to do it then, best keep playing. Wondering if it was to do with the way aov/lm operates. Appreciate the reply :) – Chon Doyle Jul 15 '20 at 22:46
  • @ChonDoyle I'm not sure if I understand you. Without `PetalLength`, my model gives you two different intercepts: `lm(Sepal.Length ~ Species, iris)` and `lm(Sepal.Length ~ relevel(Species, ref="virginica"), iris)`. Doesn't it? You should anyway make a copy-pastable reproducible example, see how it works, [_help_](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610) – jay.sf Jul 16 '20 at 02:39