简体   繁体   English

四分之一假人的线性回归

[英]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.

相关问题 使用xts对象和虚拟交互项进行线性回归 - linear regression with xts objects and dummy interaction terms 在R中的面板线性模型(回归)中添加虚拟变量 - Adding dummy variables in panel linear model (regression) in R R中具有多个虚拟编码预测变量的线性回归模型的箱线图 - Boxplot of Linear Regression Model with several Dummy coded predictors in R R编程-具有等式约束的线性回归(两组虚拟变量) - R programming - Linear regression (two sets of dummy variables) with equality constraints 线性回归 model 与 R 中的虚拟(因)变量和分类(独立)变量 - Linear Regression model with dummy (dependent) variable and categorical (independent) variable in R 在 R 中对多个处理加对照组进行线性回归的虚拟编码时如何避免虚拟变量陷阱 - How to avoid the dummy variable trap when dummy coding several treatments plus control group for linear regression in R R中具有二分预测变量的多元线性回归:伪代码还是让R处理? - Multiple Linear Regression with Dichotomous Predictor Variables in R: to dummy-code or let R handle it? R中虚拟变量的回归 - Regression of dummy variables in R R 中的非线性回归与虚拟变量 - Nonlinear regression in R with dummy variables 具有虚拟变量的多元Logistic回归 - Multivariate Logistic Regression with Dummy Variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM