Consider the space shuttle data ?𝚜𝚑𝚞𝚝𝚝𝚕𝚎 in the 𝙼𝙰𝚂𝚂 library. Consider modeling the use of the autolander as the outcome (variable name 𝚞𝚜𝚎). Fit a logistic regression model with autolander (variable auto) use (labeled as “auto” 1) versus not (0) as predicted by wind sign (variable wind). Give the estimated odds ratio for autolander use comparing head winds, labeled as “head” in the variable headwind (numerator) to tail winds (denominator).
library(MASS)
## Warning: package 'MASS' was built under R version 3.2.2
library(dplyr)
##
## Attaching package: 'dplyr'
##
## The following object is masked from 'package:MASS':
##
## select
##
## The following objects are masked from 'package:stats':
##
## filter, lag
##
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data(shuttle)
new_shuttle=mutate(shuttle,autobin= ifelse(use=='auto',1,0))
logfit = glm(new_shuttle$autobin~factor(new_shuttle$wind)-1,family="binomial")
coeff=summary(logfit)$coeff[,1]
ans=exp(coeff[1]-coeff[2])
ans
## factor(new_shuttle$wind)head
## 0.9686888
Consider the previous problem. Give the estimated odds ratio for autolander use comparing head winds (numerator) to tail winds (denominator) adjusting for wind strength from the variable magn.
## factor(new_shuttle$wind)head
## 0.9684981
If you fit a logistic regression model to a binary variable, for example use of the autolander, then fit a logistic regression model for one minus the outcome (not using the autolander) what happens to the coefficients?
The coefficients reverse their signs (1-outcome ~. is equal to fitting -outcome~.+1 so it changes sign to the intercept, by means of the +1 addition, and to the slope, by means of the minus in front of the outcome)
Consider the insect spray data 𝙸𝚗𝚜𝚎𝚌𝚝𝚂𝚙𝚛𝚊𝚢𝚜. Fit a Poisson model using spray as a factor level. Report the estimated relative rate comparing spray A (numerator) to spray B (denominator).
data("InsectSprays")
fit = glm(InsectSprays$count ~factor(InsectSprays$spray)-1,family='poisson')
coeff3=summary(fit)$coeff[,1]
ans = exp(coeff3[1]-coeff3[2])
ans
## factor(InsectSprays$spray)A
## 0.9456522
Consider a Poisson glm with an offset, t. So, for example, a model of the form 𝚐𝚕𝚖(𝚌𝚘𝚞𝚗𝚝 ~ 𝚡 + 𝚘𝚏𝚏𝚜𝚎𝚝(𝚝), 𝚏𝚊𝚖𝚒𝚕𝚢 = 𝚙𝚘𝚒𝚜𝚜𝚘𝚗) where 𝚡 is a factor variable comparing a treatment (1) to a control (0) and 𝚝 is the natural log of a monitoring time. What is impact of the coefficient for 𝚡 if we fit the model 𝚐𝚕𝚖(𝚌𝚘𝚞𝚗𝚝 ~ 𝚡 + 𝚘𝚏𝚏𝚜𝚎𝚝(𝚝𝟸), 𝚏𝚊𝚖𝚒𝚕𝚢 = 𝚙𝚘𝚒𝚜𝚜𝚘𝚗) where 𝟸 <- 𝚕𝚘𝚐(𝟷𝟶) + 𝚝? In other words, what happens to the coefficients if we change the units of the offset variable. (Note, adding log(10) on the log scale is multiplying by 10 on the original scale.)
Ans: given the formula for the expected value of the outcome \[ \exp(\beta_0+\beta_1 x+\beta_2 offset(t))=\exp(\beta_0+\beta_1 x)\exp(\beta_2 offset(t))) \] by applying the transformation \(offset(t)\Rightarrow offset(t2)=offset(t)+\log(10)\) we multiply all coefficients by 10
Consider the data
x <- -5:5
y <- c(5.12, 3.93, 2.67, 1.87, 0.52, 0.08, 0.93, 2.05, 2.54, 3.87, 4.97)
Using a knot point at 0, fit a linear model that looks like a hockey stick with two lines meeting at x=0. Include an intercept term, x and the knot point term. What is the estimated slope of the line after 0?
spline_term = x*(x>0)
regr = cbind(1,x,spline_term)
fit = lm(y~regr-1)
yhat = predict(fit)
plot(x,y,frame=FALSE,pch=21,bg='lightblue',cex=2)
lines(x,yhat,col='red',lwd=2)
ans=(yhat[11]-yhat[7])/4
ans
## 11
## 1.013067