Coefficients: (1 not defined because of singularities)

Posted Data+Science+Insight

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Coefficients: (1 not defined because of singularities)相关的知识,希望对你有一定的参考价值。

Coefficients: (1 not defined because of singularities)

目录

Coefficients: (1 not defined because of singularities)

问题:

解决:

完整错误:


问题:

#模型的特征存在共线性问题,有些特征强相关;

#define data
df <- data.frame(y = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1),
                 x1 = c(3, 3, 4, 4, 3, 2, 5, 8, 9, 9, 9, 8, 9, 9, 9),
                 x2 = c(6, 6, 8, 8, 6, 4, 10, 16, 18, 18, 18, 16, 18, 18, 18),
                 x3 = c(4, 7, 7, 3, 8, 9, 9, 8, 7, 8, 9, 4, 9, 10, 13))

#fit logistic regression model
model <- glm(y~x1+x2+x3, data=df, family=binomial)

#view model summary
summary(model)

解决:

#进行相关性分析

#发现x1和x2几乎完全正相关;

#create correlation matrix
cor(df)

           y        x1        x2        x3
y  1.0000000 0.9675325 0.9675325 0.3610320
x1 0.9675325 1.0000000 1.0000000 0.3872889
x2 0.9675325 1.0000000 1.0000000 0.3872889
x3 0.3610320 0.3872889 0.3872889 1.0000000

#剔除强相关的变量之后再构建新的模型

#fit logistic regression model
model <- glm(y~x1+x3, data=df, family=binomial)

#view model summary
summary(model)

完整错误:

> #define data
> df <- data.frame(y = c(0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1),
+                  x1 = c(3, 3, 4, 4, 3, 2, 5, 8, 9, 9, 9, 8, 9, 9, 9),
+                  x2 = c(6, 6, 8, 8, 6, 4, 10, 16, 18, 18, 18, 16, 18, 18, 18),
+                  x3 = c(4, 7, 7, 3, 8, 9, 9, 8, 7, 8, 9, 4, 9, 10, 13))

> #fit logistic regression model
> model <- glm(y~x1+x2+x3, data=df, family=binomial)
Warning message:
glm.fit:拟合機率算出来是数值零或一 

> #view model summary
> summary(model)

Call:
glm(formula = y ~ x1 + x2 + x3, family = binomial, data = df)

Deviance Residuals: 
       Min          1Q      Median          3Q         Max  
-1.372e-05  -2.110e-08   2.110e-08   2.110e-08   1.575e-05  

Coefficients: (1 not defined because of singularities)
              Estimate Std. Error z value Pr(>|z|)
(Intercept)    -75.496 176487.031   0.000        1
x1              14.546  24314.459   0.001        1
x2                  NA         NA      NA       NA
x3              -2.258  20119.863   0.000        1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 2.0728e+01  on 14  degrees of freedom
Residual deviance: 5.1523e-10  on 12  degrees of freedom
AIC: 6

Number of Fisher Scoring iterations: 24

以上是关于Coefficients: (1 not defined because of singularities)的主要内容,如果未能解决你的问题,请参考以下文章

[CF 582D]Number of Binominal Coefficients

构建线性模型(Lasso)并通过系数(coefficients)可视化分析特征重要度

CF582D Number of Binominal Coefficients 库默尔定理 数位dp

CF582D Number of Binominal Coefficients 库默尔定理 数位dp

[Statistics] Comparison of Three Correlation Coefficients: Pearson, Kendall, Spearman

Self-Organizing Hierarchical Particle Swarm Optimizer With Time-Varying Acceleration Coefficients总结