【问题标题】:R duplicating predictor variables with glm and categorical variablesR用glm和分类变量复制预测变量
【发布时间】:2014-07-06 06:00:17
【问题描述】:

我正在 R 中构建一个带有分类预测变量和二元响应的 glm。我的数据是这样的(但要大得多并且有多个预测变量):

y <- c(1,1,1,0,0) #response
x <- c(0,0,0,1,2) #predictor

由于这些数据是分类的(但它是用数字表示的),我这样做了:

y <- as.factor(y)
x <- as.factor(x)

然后我建立了我的模型:

g1 <- glm(y~x, family=binomial(link="logit"))

但是模型的细节如下:

g1
Call:  glm(formula = y ~ x, family = binomial(link = "logit"))
Coefficients:
(Intercept)           x1           x2  
      24.57       -49.13       -49.13  
Degrees of Freedom: 4 Total (i.e. Null);  2 Residual
Null Deviance:      6.73 
Residual Deviance: 2.143e-10    AIC: 6 

总结是:

summary(g1)
Call:
glm(formula = y ~ x, family = binomial(link = "logit"))

Deviance Residuals: 
         1           2           3           4           5  
 6.547e-06   6.547e-06   6.547e-06  -6.547e-06  -6.547e-06  

Coefficients:
             Estimate Std. Error z value Pr(>|z|)
(Intercept)     24.57   75639.11       0        1
x1             -49.13  151278.15       0        1
x2             -49.13  151278.15       0        1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 6.7301e+00  on 4  degrees of freedom
Residual deviance: 2.1434e-10  on 2  degrees of freedom
AIC: 6

Number of Fisher Scoring iterations: 23

我不明白为什么 R 在 x1 和 x2 中复制了 x 预测器? x1 和 x2 是什么意思?

我还需要用估计值明确写下模型,格式如下:y ~ B0 + B1*x 所以我现在卡住了,因为 x 已被一分为二,并且没有称为 x1 和 x2 的初始变量...

感谢您的帮助!

【问题讨论】:

    标签: r glm


    【解决方案1】:

    发生这种情况是因为您将x 变成了一个因素。该因子具有三个水平(0、1 和 2)。当您将分类变量放入回归模型时,一种编码方式是使用参考类别。在这种情况下,R 选择将 0 级别作为参考类别。那么 x1 和 x2 的系数分别是 0 和 1 以及 0 和 2 之间的水平差。

    这在回归中是相当标准的,所以你不应该觉得它太令人惊讶。也许您只是对 R 如何命名系数感到困惑。

    【讨论】:

      猜你喜欢
      • 2021-08-26
      • 2020-07-12
      • 1970-01-01
      • 2013-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多