# Code for Lecture 23 - glm continued, logistic regression ####Example - effects of age and birth location (wild or captive) on Survival in wolves ### Survival is not normally distribution (binomial) and isn't continuous ###import the data datum=read.csv(file.choose()) ###Analyze the data using glm results=glm(Mortality~Age+BirthLoc,data=datum,family=binomial) ### runs a 'logistic' regression summary(results) ### examine results #Call: #glm(formula = Mortality ~ Age + BirthLoc, family = binomial, # data = datum) # #Deviance Residuals: # Min 1Q Median 3Q Max #-2.2567 -0.6995 0.2488 0.7646 1.9844 # #Coefficients: # Estimate Std. Error z value Pr(>|z|) #(Intercept) -2.8894 0.7143 -4.045 5.24e-05 *** #Age 0.5354 0.1298 4.123 3.74e-05 *** #BirthLoc 2.6016 0.6323 4.115 3.88e-05 *** #--- #Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # #(Dispersion parameter for binomial family taken to be 1) # # Null deviance: 133.574 on 98 degrees of freedom #Residual deviance: 92.696 on 96 degrees of freedom #AIC: 98.696 # #Number of Fisher Scoring iterations: 5 ### Note that coefficient estimates cannot be interpreted as is. ### To make conclusions about effect, must take exp to the coefficient power ### Value represents a multiplier effect of dependent variable on ODDS of an outcome ### Examples ### Average mortality rate of of Age 0 individuals in the captive born group exp(-2.8894)/(1+exp(-2.8894)) ### Effect of Birth Location exp(2.6016) ### Wild born wolves are 13.48 times as likely to die as captive born wolves ### Alternatively, The odds of wild-born wolves dying was 13.48 times that of captive born wolves ###confidence limits exp(confint(results)) ### confint(results) ### exp(1.337) ### lower confidence limit on odds ratio ### exp(3.8752) ### upper confidence limit on odds ratio ### Effect of Age exp(0.5354) ### For each 1 year increase in age, wolves were 1.708131 times as likely to die. ### Alternatively, for each 1 year increase in age, the odds of mortality increased by 1.708131 times #Predictions ### What is the probability of mortality for a 5-year-old, captive-born wolf? NewData=data.frame(BirthLoc=0,Age=5) predict(results,NewData,type="response") ### There are no prediction intervals for a non-normal response. ### You have to give R the argument type="response" if you want the predicted Y value. Otherwise it just gives you the prediction of the linear model