R 如何从GLM输出中获取概率
我现在非常困惑,因为我正试图找出如何从R中的R 如何从GLM输出中获取概率,r,probability,glm,R,Probability,Glm,我现在非常困惑,因为我正试图找出如何从R中的glm输出计算概率。我知道数据非常微不足道,但我真的很想知道如何从这样的输出中获得概率。我想尝试inv.logit(),但不知道要将哪些变量放在括号内 数据来自入住率研究。我正在评估毛发捕捉器与摄像机捕捉器在检测3种物种(红松鼠、松貂和入侵性灰松鼠)方面的成功率。我想看看是什么影响了各种物种的检测(或未检测)。其中一个假设是,在现场检测到另一个焦点物种会影响红松鼠的可检测性。鉴于松貂是红松鼠的捕食者,灰松鼠是竞争对手,这两个物种在同一地点的存在可能会影
glm
输出计算概率。我知道数据非常微不足道,但我真的很想知道如何从这样的输出中获得概率。我想尝试inv.logit()
,但不知道要将哪些变量放在括号内
数据来自入住率研究。我正在评估毛发捕捉器与摄像机捕捉器在检测3种物种(红松鼠、松貂和入侵性灰松鼠)方面的成功率。我想看看是什么影响了各种物种的检测(或未检测)。其中一个假设是,在现场检测到另一个焦点物种会影响红松鼠的可检测性。鉴于松貂是红松鼠的捕食者,灰松鼠是竞争对手,这两个物种在同一地点的存在可能会影响红松鼠的可探测性
这能说明可能性吗<代码>库存逻辑(-1.14-0.1322*非RS事件)
逻辑回归中的因变量是对数优势比。我们将用
MASS
软件包中的航天飞机自动着陆器数据说明如何解释系数
加载数据后,我们将创建一个二进制因变量,其中:
1 = autolander used,
0 = autolander not used.
我们还将为航天飞机稳定性创建一个二元自变量:
1 = stable positioning
0 = unstable positioning.
然后,我们将使用family=binomial(link=“logit”)
运行glm()。由于系数是对数优势比,我们将使其指数化以将其转换回优势比
library(MASS)
str(shuttle)
shuttle$stable <- 0
shuttle[shuttle$stability =="stab","stable"] <- 1
shuttle$auto <- 0
shuttle[shuttle$use =="auto","auto"] <- 1
fit <- glm(use ~ factor(stable),family=binomial(link = "logit"),data=shuttle) # specifies base as unstable
summary(fit)
exp(fit$coefficients)
使用航天飞机自动着陆器数据,其工作原理如下
# convert intercept to probability
odds_i <- exp(fit$coefficients[1])
odds_i / (1 + odds_i)
# convert stable="stable" to probability
odds_p <- exp(fit$coefficients[1]) * exp(fit$coefficients[2])
odds_p / (1 + odds_p)
请注意,输出与手动计算的概率相匹配
> # convert to probabilities with the predict() function
> predict(fit,data.frame(stable="0"),type="response")
1
0.5
> predict(fit,data.frame(stable="1"),type="response")
1
0.3671875
>
将其应用于OP数据
我们可以将这些步骤应用于OP的glm()
输出,如下所示
coefficients <- c(-1.1455,-0.1322)
exp(coefficients)
odds_i <- exp(coefficients[1])
odds_i / (1 + odds_i)
# convert nonRSEvents = 1 to probability
odds_p <- exp(coefficients[1]) * exp(coefficients[2])
odds_p / (1 + odds_p)
# simulate up to 10 nonRSEvents prior to RS
coef_df <- data.frame(nonRSEvents=0:10,
intercept=rep(-1.1455,11),
nonRSEventSlope=rep(-0.1322,11))
coef_df$nonRSEventValue <- coef_df$nonRSEventSlope *
coef_df$nonRSEvents
coef_df$intercept_exp <- exp(coef_df$intercept)
coef_df$slope_exp <- exp(coef_df$nonRSEventValue)
coef_df$odds <- coef_df$intercept_exp * coef_df$slope_exp
coef_df$probability <- coef_df$odds / (1 + coef_df$odds)
# print the odds & probabilities by number of nonRSEvents
coef_df[,c(1,7:8)]
如果您想预测预测变量指定值集的响应概率:
pframe <- data.frame(NonRSevents_before1stRS=4)
predict(fitted_model, newdata=pframe, type="response")
也会起作用(predict
默认情况下提供链接函数[在本例中为logit/log赔率]刻度的预测)。那么,到目前为止您尝试了什么/您到底坚持了什么?对不起,我以前从未发布过这篇文章,所以我意识到我对这一点表达得不太好。因此glm输出显示了站点中其他物种对检测红松鼠的影响。我知道数据显示其他物种的存在对红松鼠的发现没有意义。我想我应该展示发现红松鼠的可能性。我尝试在R上使用反向日志,但不确定使用哪个变量。我对GLMs非常陌生(我确信这是非常明显的),因此,如果这是一个非常愚蠢的问题,我深表歉意。您可以在glm对象的拟合值上使用invlog,或者在您自己的预测Y值上使用invlog,如果您在一段时间内使用监测站,这可能被建模为一个泊松过程:在一段时间内红松鼠的到达率是多少?这是大本钟,非常感谢,对于“拟合的_模型”,会有什么输入?我为这些愚蠢的问题道歉,但我的大脑感觉很兴奋
> # convert intercept to probability
> odds_i <- exp(fit$coefficients[1])
> odds_i / (1 + odds_i)
(Intercept)
0.5
> # convert stable="stable" to probability
> odds_p <- exp(fit$coefficients[1]) * exp(fit$coefficients[2])
> odds_p / (1 + odds_p)
(Intercept)
0.3671875
>
# convert to probabilities with the predict() function
predict(fit,data.frame(stable="0"),type="response")
predict(fit,data.frame(stable="1"),type="response")
> # convert to probabilities with the predict() function
> predict(fit,data.frame(stable="0"),type="response")
1
0.5
> predict(fit,data.frame(stable="1"),type="response")
1
0.3671875
>
coefficients <- c(-1.1455,-0.1322)
exp(coefficients)
odds_i <- exp(coefficients[1])
odds_i / (1 + odds_i)
# convert nonRSEvents = 1 to probability
odds_p <- exp(coefficients[1]) * exp(coefficients[2])
odds_p / (1 + odds_p)
# simulate up to 10 nonRSEvents prior to RS
coef_df <- data.frame(nonRSEvents=0:10,
intercept=rep(-1.1455,11),
nonRSEventSlope=rep(-0.1322,11))
coef_df$nonRSEventValue <- coef_df$nonRSEventSlope *
coef_df$nonRSEvents
coef_df$intercept_exp <- exp(coef_df$intercept)
coef_df$slope_exp <- exp(coef_df$nonRSEventValue)
coef_df$odds <- coef_df$intercept_exp * coef_df$slope_exp
coef_df$probability <- coef_df$odds / (1 + coef_df$odds)
# print the odds & probabilities by number of nonRSEvents
coef_df[,c(1,7:8)]
> coef_df[,c(1,7:8)]
nonRSEvents odds probability
1 0 0.31806 0.24131
2 1 0.27868 0.21794
3 2 0.24417 0.19625
4 3 0.21393 0.17623
5 4 0.18744 0.15785
6 5 0.16423 0.14106
7 6 0.14389 0.12579
8 7 0.12607 0.11196
9 8 0.11046 0.09947
10 9 0.09678 0.08824
11 10 0.08480 0.07817
>
pframe <- data.frame(NonRSevents_before1stRS=4)
predict(fitted_model, newdata=pframe, type="response")
plogis(predict(fitted_model))