Skip to content
Prev 1833 / 20628 Next

(no subject)

The formatting is a bit garbled, but hopefully this addresses the correct question.

You ask if there is a way to change which level in a factor is the baseline with treatment contrasts (the default) in one line rather than using relevel on the factor before calling mle (is this correct?).

First one issue to be aware of is whether the baseline level is considered to be a property of the data or a property of the analysis (some packages (maybe most or all other than S/R) don't allow for this distinction so people don't consider it, even though it can be quite important).

If the baseline level (or even the full ordering) is a property of the data (or even if the correct set of contrasts to be used is a property of the data), then this information should be stored as part of the meta-data for the factor.  R/S allows for you to set the ordering of the levels in the factor as part of creating/editing the factor itself.  This information will then be used for analysis and plotting of that factor (unless overridden).  We can even set up a set of contrasts to be used for the factor (see ?contrasts) that will override the default (but can also be overridden in a specific analysis).

In my opinion, in most cases the order of the levels is either irrelevant or a property of the data itself and therefore should be set as part of the data, not the analysis (and this saves work by not having to specify the order in every analysis/plot/etc.)

If you want to keep the current order and just override some specifics for one particular analysis, then one option is to use the C function (note uppercase) along with the appropriate contr function.  For example:
Call:
lm(formula = Sepal.Width ~ Species, data = iris)

Coefficients:
      (Intercept)  Speciesversicolor   Speciesvirginica  
            3.428             -0.658             -0.454
Call:
lm(formula = Sepal.Width ~ C(Species, contr.treatment, base = 2),     data = iris)

Coefficients:
                           (Intercept)  C(Species, contr.treatment, base = 2)1  
                                 2.770                                   0.658  
C(Species, contr.treatment, base = 2)3  
                                 0.204
[1] 2.77

Note that the intercept in the 2nd case is the mean of Species 2 (as can be seen by combining the values from the 1st case) and that the first slope changed sign but not absolute value since it is still the difference between the first 2 species, just which one is baseline changed.

Hope this helps,