R使用glm和分类变量复制预测变量
我正在用分类预测和二元反应构建一个glm in R。我的数据是这样的(但要大得多,而且有多个预测因素): 总结如下:R使用glm和分类变量复制预测变量,r,glm,R,Glm,我正在用分类预测和二元反应构建一个glm in R。我的数据是这样的(但要大得多,而且有多个预测因素): 总结如下: 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
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的初始变量
谢谢你的帮助 发生这种情况是因为您将
x
作为一个因子。该系数有三个级别(0、1和2)。在回归模型中放置分类变量时,编码它的一种方法是使用参考类别。在这种情况下,R选择将0级别作为参考类别。那么x1和x2的系数分别是0和1以及0和2之间的级差
这在回归中是相当标准的,所以你不应该感到太惊讶。也许你只是对R如何命名系数感到困惑
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