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?
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.
Easiest way would be to create new variable with information you need...
Power$Q2dummy <- 0
Power$Q2dummy[which(Power$Quarter == 'Q2')] <- 1
lm(YY~XX+Q2dummy,data=Power)
However, it is hard to say, because you dont provide your data or even their summary (what is variable Quarter? Factor with 4 states I guess?).
One possibility could be to use the ifelse(rule, if TRUE, if FALSE)
command :
For Example:
Power$Q2dummy <- ifelse(Power$Quarter == "Q2",1,0)
lm(YY~XX+Q2dummy,data=Power)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.