如何在R中实现二元逻辑回归中的分类变量?我想测试专业领域(学生,工人,教师,个体户)对购买产品的概率的影响。
在我的例子中,y是一个二元变量(1代表购买产品,0代表不购买)。
set.seed(123)
y<-round(runif(100,0,1))
x1<-round(runif(100,0,1))
x2<-round(runif(100,20,80))
x3<-round(runif(100,1,4))
test<-glm(y~x1+x2+x3, family=binomial(link="logit"))
summary(test)
如果我在上面的回归中实现x3 (专业领域),我会得到对x3的错误估计/解释。
我必须做什么才能获得分类变量(x3)的正确影响/估计?
非常感谢
发布于 2018-01-06 17:44:10
我建议您将x3设置为因子变量,不需要创建虚拟对象:
set.seed(123)
y <- round(runif(100,0,1))
x1 <- round(runif(100,0,1))
x2 <- round(runif(100,20,80))
x3 <- factor(round(runif(100,1,4)),labels=c("student", "worker", "teacher", "self-employed"))
test <- glm(y~x1+x2+x3, family=binomial(link="logit"))
summary(test)
Here is the summary:这是您的模型的输出:
Call:
glm(formula = y ~ x1 + x2 + x3, family = binomial(link = "logit"))
Deviance Residuals:
Min 1Q Median 3Q Max
-1.4665 -1.1054 -0.9639 1.1979 1.4044
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.464751 0.806463 0.576 0.564
x1 0.298692 0.413875 0.722 0.470
x2 -0.002454 0.011875 -0.207 0.836
x3worker -0.807325 0.626663 -1.288 0.198
x3teacher -0.567798 0.615866 -0.922 0.357
x3self-employed -0.715193 0.756699 -0.945 0.345
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 138.47 on 99 degrees of freedom
Residual deviance: 135.98 on 94 degrees of freedom
AIC: 147.98
Number of Fisher Scoring iterations: 4无论如何,我建议你研究一下R-bloggers上的这篇文章:https://www.r-bloggers.com/logistic-regression-and-categorical-covariates/
https://stackoverflow.com/questions/48121325
复制相似问题