当一个或多个预测变量保持不变时,绘制来自二元逻辑回归的估计概率

时间:2017-02-12 02:03:29

标签: r logistic-regression

我是一名生物学专业的学生,​​在下一期问题上我已经将我的车轮旋转了大约30个小时。总之,我想从我生成的glm二元逻辑回归模型中绘制估计概率的数字。我已经完成了模型选择,验证等工作,现在我只是想制作数字。我没有问题为我选择的模型绘制概率曲线,但我真正感兴趣的是产生一个数字,当另一个预测变量保持不变时,该数字显示预测变量的二元结果的概率。

我无法弄清楚如何将此常量值仅分配给其中一个预测变量并绘制另一个变量的概率。最后,我希望生成类似于附加desired output的原始示例的数字。我承认我是R的新手,我当然感谢大家和他人。时间,但我已经筋疲力尽在线搜索,尚未找到充分解释的方法或解决方案。这是与我的问题相关的最接近的信息,但是我发现这个解释含糊不清,但它没有提供一个例子,用于在绘制另一个预测值的概率时为一个预测值分配一个常数值。 https://stat.ethz.ch/pipermail/r-help/2010-September/253899.html

下面我提供了一个模拟数据集和我的进度。非常感谢您的专业知识,我相信解决方案和代码示例对于使用逻辑回归的其他生态学家会有所帮助。

模拟数据集显示了蜥蜴冬季的生存结果。预测变量是"质量"和"深度"。

x<-read.csv('logreg_example_data.csv',header = T)
x
 survival      mass depth
1         0  4.294456   262
2         0  8.359857   261
3         0 10.740580   257
4         0 10.740580   257
5         0  6.384678   257
6         0  6.384678   257
7         0 11.596380   270
8         0 11.596380   270
9         0  4.294456   262
10        0  4.294456   262
11        0  8.359857   261
12        0  8.359857   261
13        0  8.359857   261
14        0  7.920406   258
15        0  7.920406   258
16        0  7.920406   261
17        0 10.740580   257
18        0 10.740580   258
19        0 38.824960   262
20        0  9.916840   239
21        1  6.384678   257
22        1  6.384678   257
23        1 11.596380   270
24        1 11.596380   270
25        1 11.596380   270
26        1 23.709520   288
27        1 23.709520   288
28        1 23.709520   288
29        1 38.568970   262
30        1 38.568970   262
31        1  6.581013   295
32        1  6.581013   298
33        1  0.766564   269
34        1  5.440803   262
35        1  5.440803   262
36        1 19.534710   252
37        1 19.534710   259
38        1  8.359857   263
39        1 10.740580   257
40        1 38.824960   264
41        1 38.824960   264
42        1 41.556970   239

#Dataset name is x 
# time to run the glm model
model1<-glm(formula=survival ~ mass + depth, family = "binomial", data=x)
model1
summary(model1)
#Ok now heres how i predict the probability of a lizard "Bob" surviving    the winter with a mass of 32.949 grams and a burrow depth of 264 mm
newdata<-data.frame(mass = 32.949, depth = 264)
predict(model1, newdata, type = "response")
# the lizard "Bob" has a 87.3% chance of surviving the winter
#Now lets assume the glm. model was robust and the lizard was endangered,
#from all my research I know the average burrow depth is 263.9 mm at a  national park 
#lets say i am also interested in survival probabilities at burrow depths of 200 and 100 mm, respectively
#how do i use the valuable glm model produced above to generate a plot 
#showing the probability of lizards surviving with average burrow depths stated above 
#across a range of mass values from 0.0 to 100.0 grams??????????
#i know i need to use the plot and predict functions but i cannot figure out how to tell R that i 
#want to use the glm model i produced to predict "survival" based on "mass" when the other predictor "depth" is held at constant values of biological relevance
#I would also like to add dashed lines for 95% CI 

0 个答案:

没有答案