library(MASS)

1. Consider the space shuttle data ?shuttle in the MASS library. Consider modeling the use of the autolander as the outcome (variable name use). 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).

df <- shuttle
df$use<-as.numeric(df$use=='auto')
mdl <- glm(use~factor(wind)-1, family = binomial, df)
mdl
## 
## Call:  glm(formula = use ~ factor(wind) - 1, family = binomial, data = df)
## 
## Coefficients:
## factor(wind)head  factor(wind)tail  
##           0.2513            0.2831  
## 
## Degrees of Freedom: 256 Total (i.e. Null);  254 Residual
## Null Deviance:       354.9 
## Residual Deviance: 350.3     AIC: 354.3
oddh <- exp(mdl$coef[1])
oddt <- exp(mdl$coef[2])
oddh/oddt
## factor(wind)head 
##        0.9686888

2.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.

mdl2 <- glm(use~factor(wind)+ factor(magn)-1, family = binomial, df)
mdl2$coef
##   factor(wind)head   factor(wind)tail factor(magn)Medium 
##       3.635093e-01       3.955180e-01      -1.009525e-15 
##    factor(magn)Out factor(magn)Strong 
##      -3.795136e-01      -6.441258e-02
exp(mdl2$coef[[1]])/exp(mdl2$coef[[2]])
## [1] 0.9684981

3.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?

mdl3 <- glm((1-use)~factor(wind)-1, family = binomial, df)
mdl3
## 
## Call:  glm(formula = (1 - use) ~ factor(wind) - 1, family = binomial, 
##     data = df)
## 
## Coefficients:
## factor(wind)head  factor(wind)tail  
##          -0.2513           -0.2831  
## 
## Degrees of Freedom: 256 Total (i.e. Null);  254 Residual
## Null Deviance:       354.9 
## Residual Deviance: 350.3     AIC: 354.3

4. Consider the insect spray data InsectSprays. Fit a Poisson model using spray as a factor level. Report the estimated relative rate comapring spray A (numerator) to spray B (denominator).

head(InsectSprays)
##   count spray
## 1    10     A
## 2     7     A
## 3    20     A
## 4    14     A
## 5    14     A
## 6    12     A
mdl4 <- glm(count~factor(spray)-1, poisson, InsectSprays)
mdl4
## 
## Call:  glm(formula = count ~ factor(spray) - 1, family = poisson, data = InsectSprays)
## 
## Coefficients:
## factor(spray)A  factor(spray)B  factor(spray)C  factor(spray)D  
##          2.674           2.730           0.734           1.593  
## factor(spray)E  factor(spray)F  
##          1.253           2.813  
## 
## Degrees of Freedom: 72 Total (i.e. Null);  66 Residual
## Null Deviance:       2265 
## Residual Deviance: 98.33     AIC: 376.6
exp(mdl4$coef[1])/exp(mdl4$coef[2])
## factor(spray)A 
##      0.9456522

5.Consider a Poisson glm with an offset, t. So, for example, a model of the form glm(count ~ x + offset(t), family = poisson) where x is a factor variable comparing a treatment (1) to a control (0) and t is the natural log of a monitoring time. What is impact of the coefficient for x if we fit the model glm(count ~ x + offset(t2), family = poisson) where 2 <- log(10) + t? 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.)

The intercept changes, but the coefficient estimate is unchanged.

6. 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?

knots <- 0
splineTerms <- (x>0)*(x-0)
xmat <- cbind(1, x, splineTerms)
mdl6 <- lm(y~xmat-1)
mdl6$coef[2]+mdl6$coef[3]
##    xmatx 
## 1.013067

In the coefficients R subtracted the mean slope of the first line from that of the second, so we can simply add it back to get the true value.