Search code examples
rlinear-regressiondata-fittingdummy-data

linear regression with Quarter dummy


I am trying to fit a linear regression to the data below

Power<-mutate(Power,Year=format(Date,"%Y"),Quarter=quarters(Date),Month=format(Date,"%m"))
head(Power)
       Date    YY    XX  Year    Quarter
2007-01-01     NA     NA 2007      Q1
2007-01-02     NA     NA 2007      Q1
2007-01-03  55.90  71.40 2007      Q1
2007-01-04  55.25  70.75 2007      Q1

The model is

lm(YY~XX+as.factor(Quarter,ref="Q1"),data=Power)

This works fine. However, it automatically creates three dummies for 3 quarters. Is there any way to include only one dummy, say Q2 in this model?


Solution

  • Arguably the most common way to do this is to create a dichotomous variable on the fly with I().

    lm(YY ~ XX + I(Quarter=="Q2"), data=Power)
    

    This includes a binary predictor in the model that's 1 when Quarter=="Q2" and 0 otherwise.