[英]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?
有没有办法只包含一个假人,例如在该模型中的Q2?
Arguably the most common way to do this is to create a dichotomous variable on the fly with I()
. 可以说,最常见的方法是使用
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. 这包括模型中的二进制预测变量,当
Quarter=="Q2"
时为1,否则为0。
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?). 但是,很难说,因为您没有提供数据甚至是摘要(什么是变量Quarter?我猜有4个状态的因子?)。
One possibility could be to use the ifelse(rule, if TRUE, if FALSE)
command : 一种可能是使用
ifelse(rule, if TRUE, if FALSE)
命令:
For Example: 例如:
Power$Q2dummy <- ifelse(Power$Quarter == "Q2",1,0)
lm(YY~XX+Q2dummy,data=Power)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.