简体   繁体   English

R lm 捕获交互项,但不是分类变量

[英]R lm Capture interaction terms, but not categorical variable

I would like to estimate the following regression model: y = b0 + b1 * X + b2 * x * dummy我想估计以下回归 model:y = b0 + b1 * X + b2 * x * dummy

where y and x are continuous, and dummy is a categorical (dummy variable).其中 y 和 x 是连续的, dummy是分类变量(虚拟变量)。

In other words, I would like my estimated model to estimate three coefficients: bo, b1, and b2.换句话说,我希望我估计的 model 来估计三个系数:bo、b1 和 b2。

I have tried the following...我尝试了以下...

lm(y ~ x + x * dummy, data)

but it adds the variable dummy in the model and estimates the coefficient of dummy .但它在 model 中添加了变量dummy并估计了dummy的系数。

The following comes close to what I want to do, but it converts the interaction term to a binary variable (true/false).以下接近我想要做的,但它将交互项转换为二进制变量(真/假)。

lm(y ~ x + I(!x * dummy), data)

For replication consider the following example:对于复制,请考虑以下示例:

data <- tibble(y=rnorm(10), x=runif(10), dummy=ifelse(x>.5,1,0))
lm(y ~ x + x * dummy, data)
lm(y ~ x + I(!x * dummy), data)

Thanks谢谢

Here:这里:

> summary(lm(y ~ x+ x : dummy, data))

Call:
lm(formula = y ~ x + x:dummy, data = data)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.61312 -0.15558 -0.00354  0.23965  0.47351 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)
(Intercept)  0.06755    0.36162   0.187    0.857
x            0.94953    1.18299   0.803    0.449
x:dummy     -1.10220    0.88112  -1.251    0.251

Residual standard error: 0.4148 on 7 degrees of freedom
Multiple R-squared:  0.2645,    Adjusted R-squared:  0.05438 
F-statistic: 1.259 on 2 and 7 DF,  p-value: 0.3412

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有类别和平方连续变量的R lm交互项 - R lm interaction terms with categorical and squared continuous variables 可以指定 R 中 lm() 中主效应与交互项的顺序吗? - Can the order of main effect vs interaction terms in lm() in R be specified? 解释 R 中 lm() summary() 中分类变量系数的估计 - Interpreting Estimate of categorical variable coefficient in lm() summary() in R 在带有分类变量的 r output 中使用 lm() 的多元线性回归不完整? - Multiple Linear Regression using lm() in r output with categorical variable is incomplete? R lm 如何选择与分类变量和连续变量之间的交互作用的对比? - How does R lm choose contrasts with interaction between a categorical and continuous variables? 将交互项序列添加到线性模型中,lm() - Add sequence of interaction terms to linear model, lm() R中连续变量和分类变量之间的相互作用:是否可以包含所有类别? - Interaction between continuous and categorical variable in R: is there a way to include all categories? R:与GLMM(lme4)的连续和分类变量的交互图 - R: Interaction Plot with a continuous and a categorical variable for a GLMM (lme4) R带有类别变量和交互作用项的线性回归的嵌套横截面的可视化 - R Visualization of nested cross-sections for linear regression with categorical variables and interaction terms 如何在lm()中删除R中的特定类别预测变量? - How to remove particular categorical predictor in R in lm()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM