##### Code for 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 ## 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)) ### lower 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 min(datum$Age) max(datum$Age) exp(2.6016*(10-9)) # the oldest wolves were 123.8 times as likely to die as the youngest wolves ###Prediction #What is the estimated mortality rate of a wild-born, 5-year-old wolf have, on average? exp(-2.8894+2.6016+0.5354*5)/(1+exp(-2.8894+2.6016+0.5354*5)) ##### DataSet 2 ###Example - effects of age, birth location (wild or captive), and body condition (percent body fat) ### on litter sizes in red wolves ### Litter size is not normally distribution (skewed to the left) and isn't continuous ###import the data datum=read.csv(file.choose()) head(datum) ### Run Analysis results=glm(LitterSize~Age+BirthLoc+BodyCond,data=datum,family=poisson) summary(results) #glm(formula = LitterSize ~ Age + BirthLoc + BodyCond, family = poisson, # data = datum) # #Deviance Residuals: # Min 1Q Median 3Q Max #-1.6338 -0.7791 -0.2243 0.4249 2.7210 # #Coefficients: # Estimate Std. Error z value Pr(>|z|) #(Intercept) -0.08328 0.28494 -0.292 0.770082 #Age -0.20223 0.02939 -6.880 5.97e-12 *** #BirthLoc 1.49315 0.17924 8.330 < 2e-16 *** #BodyCond 2.22882 0.64759 3.442 0.000578 *** #--- #Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # #(Dispersion parameter for poisson family taken to be 1) # # Null deviance: 224.25 on 98 degrees of freedom #Residual deviance: 73.30 on 95 degrees of freedom #AIC: 274.91 #Number of Fisher Scoring iterations: 5 ###Examples ## Effects of age exp(-0.20223) #For each 1 year increase in age, wolves had 0.8169 times as many pups # Hard to wrap head around - alternative: exp(0.20223) ### or 1/exp(-0.20223) ### same thing # For each 1 year DECREASE in age, wolves had 1.22413 times as many pups #Alternatively, for each 1 year increase in age, wolves had 18.3% fewer pups #(1-0.8169) ##Effects of Body Condition - Scaling exp(2.22882) # doesn't really make any sense because body condition only ranged from about 0.1 to 0.5 # 1 unit change in body condition is the change from 0 body fat to 100% body fat - doesn't make sense exp(2.22882*0.1) ### scale the value # for each 10% increase in body fat, wolves had 1.25 times as many pups exp(2.22882*(0.5-0.1)) ### range in body fat values # The fatest wolves had 2.438853 times as many pups as the skinniest wolves