我现在非常困难,因为我试图弄清楚如何从R中的Location!
输出计算概率。我知道数据非常微不足道但我真的很想成为展示了如何从这样的输出中获得概率。我正在考虑尝试glm
,但不知道要在括号内放置哪些变量。
数据来自入住率研究。我在评估3种(红松鼠,松貂和侵入性灰松鼠)中的毛发陷阱方法与相机陷阱的成功评估。我想看看是什么影响了各种物种的检测(或非检测)。一个假设是在现场检测到另一个焦点物种会影响红松鼠的可探测性。鉴于松貂是红松鼠的捕食者,灰松鼠是竞争对手,这两个物种在一个地方的存在可能会影响红松鼠的可探测性。
这会显示概率吗? inv.logit()
inv.logit(-1.14 - 0.1322 * nonRS events)
答案 0 :(得分:7)
如果要预测预测变量的指定值集的响应概率:
pframe <- data.frame(NonRSevents_before1stRS=4)
predict(fitted_model, newdata=pframe, type="response")
其中fitted_model
是glm()
拟合的结果,您存储在变量中。您可能不熟悉统计分析的R方法,即将拟合模型作为对象/存储在变量中,然后对其应用不同的方法(summary()
,plot()
,{{1 },predict()
,...)
residuals()
变量的合理值吗?NonRSevents_before1stRS
)data.frame(NonRSevents_before1stRS=c(4,5,6,7,8))
如果您想要原始数据集中观察值的预测概率,只需data.frame(x=4:8,y=mean(orig_data$y), ...)
你是正确的predict(fitted_model, type="response")
(来自一堆不同的包,不知道你在使用哪个)或inv.logit()
(来自基地R,基本上相同)将从logit或log-odds缩放到概率标度,所以
plogis()
也可以工作(默认情况下,plogis(predict(fitted_model))
提供关于链接函数的预测[在本例中为logit / log-odds]比例。)
答案 1 :(得分:4)
逻辑回归中的因变量是对数比值比。我们将说明如何使用MASS
包中的航天飞机自动数据来解释系数。
加载数据后,我们将创建一个二进制因变量,其中:
1 = autolander used,
0 = autolander not used.
我们还将为航天飞机稳定性创建一个二元独立变量:
1 = stable positioning
0 = unstable positioning.
然后,我们将使用glm()
运行family=binomial(link="logit")
。由于系数是对数比值比,我们将它们取幂以将它们转回比值比。
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)
...和输出:
> fit <- glm(use ~ factor(stable),family=binomial(link = "logit"),data=shuttle) # specifies base as unstable
>
> summary(fit)
Call:
glm(formula = use ~ factor(stable), family = binomial(link = "logit"),
data = shuttle)
Deviance Residuals:
Min 1Q Median 3Q Max
-1.1774 -1.0118 -0.9566 1.1774 1.4155
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 4.747e-15 1.768e-01 0.000 1.0000
factor(stable)1 -5.443e-01 2.547e-01 -2.137 0.0326 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 350.36 on 255 degrees of freedom
Residual deviance: 345.75 on 254 degrees of freedom
AIC: 349.75
Number of Fisher Scoring iterations: 4
> exp(fit$coefficients)
(Intercept) factor(stable)1
1.0000000 0.5802469
>
0的截距是不稳定的对数几率,-5443的系数是稳定的对数几率。在对系数进行取幂后,我们观察到在不稳定的航天飞机1.0的条件下使用自动人员的几率,如果航天飞机稳定则乘以.58。这意味着如果梭子具有稳定的定位,则不太可能使用自动人。
我们可以通过两种方式做到这一点。首先,手动方法:使用以下等式对系数进行取幂并将概率转换为概率。
p = odds / (1 + odds)
使用班车自动拨号数据,其工作原理如下。
# 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 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
>
航天飞机不稳定时使用自动人的概率是0.5,当航天飞机稳定时减少到0.37。
生成概率的第二种方法是使用predict()
函数。
# 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
>
我们可以将这些步骤应用于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)]
...和最终输出。
> 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
>