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)
shuttle$auto <- as.integer(shuttle$use == "auto")
fit <- glm(auto ~ wind - 1 , "binomial", shuttle)
cf <- exp(coef(fit))
oddsratio <- cf[1]/cf[2]
oddsratio
## windhead
## 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.
fit2 <- glm(auto ~ wind + magn - 1 , "binomial", shuttle)
cf2 <- exp(coef(fit2))
oddsratio2 <- cf[1]/cf[2]
oddsratio2
## windhead
## 0.9686888
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?
fit3 <- glm(I(1 - auto) ~ wind - 1 , "binomial", shuttle)
rbind(coef(fit),coef(fit3))
## windhead windtail
## [1,] 0.2513144 0.2831263
## [2,] -0.2513144 -0.2831263
The coefficients reverse their signs.
Consider the insect spray data πΈππππππππππ’π. Fit a Poisson model using spray as a factor level. Report the estimated relative rate comapring spray A (numerator) to spray B (denominator).
fit4 <- glm(count ~ spray -1, poisson, InsectSprays)
cf4 <- exp(coef(fit4))
cf4[1]/cf4[2]
## sprayA
## 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.)
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 <- c(0)
splineTerms <- sapply(knots, function(knot) (x>knot)*(x-knot))
xMat <- cbind(1, x, splineTerms)
fit6 <- lm(y ~ xMat - 1)
yHat <- predict(fit6)
maxPos <- which.max(x)
minPos <- which(x == 0)
len <- maxPos - minPos
(yHat[maxPos] - yHat[minPos]) / len
## 11
## 1.013067