r - 预测响应与手动逻辑回归概率
问题描述
我正在尝试使用逻辑回归模型手动计算给定 x 的概率。
我的模型看起来像这样fit2 <- glm(ability~stability, data = df2)
我创建了一个给我响应的函数:
estimator <- function(x){
predict(fit2, type = "response", newdata = data.frame(stability=x))
}
这个函数给了我 0.5304603 的值 x=550
然后我创建手动版本。为此,我使用该功能p = e^(B0+B1*x)/(1 + e^(B0+B1*x))
所以我们的代码看起来像这样
est <- function(par, x){
x = c(1,x)
exp(par%*%x)/(1+exp(par%*%x))
}
其中 par = fit2$ 系数,x = 550
但此代码返回 0.6295905
为什么?
编辑:
summary(fit2):
Call:
glm(formula = ability ~ stability, data = df2)
Deviance Residuals:
Min 1Q Median 3Q Max
-0.78165 -0.33738 0.09462 0.31582 0.72823
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) -1.531574 0.677545 -2.26 0.03275 *
stability 0.003749 0.001229 3.05 0.00535 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for gaussian family taken to be 0.1965073)
Null deviance: 6.7407 on 26 degrees of freedom
Residual deviance: 4.9127 on 25 degrees of freedom
AIC: 36.614
Number of Fisher Scoring iterations: 2
解决方案
推荐阅读
- reactjs - 在测试中模拟localStorage数据
- r - 如何在多列中有条件地用 NA 替换值
- popup - 打开地图上可见的所有标记的弹出窗口,缩放 > 10
- c# - 有没有办法链接 Autodesk Forge 存储桶?
- python-3.6 - 如何在 python3 的一行中从用户那里获取 10 个输入?
- javascript - 如何在 Salesforce Lightning 组件中实现 reCAPTCHA V3?
- mysql - 如何一次打印多个查询,生成 1 个表?
- python - 没有 NAN 值的标签编码
- amazon-web-services - DynamoDB 排序是不可能的吗?
- scala - 在 Spark 中使用 RDD 查找键的平均值