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).
library('MASS')
df <- shuttle
df$autolander <- as.integer(df$use == "auto")
fit <- glm(autolander~ wind - 1, 'binomial', df)
#summary(fit)
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.
fit <- glm(autolander~ wind - 1 + magn, 'binomial', df)
#summary(fit)
cf <- exp(coef(fit))
oddsRatio <- cf[1] / cf[2]
oddsRatio
## windhead
## 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?
Answer:
In this case the ones in the autolander will be zeros, and zeros in the autolander will be ones. It means what the \(logit(autolander) = - logit(one{\_}minus{\_}autolander)\) therefore the logistic regression coefficients should change the signs.
The experimental confirmation:
df$one_minus_autolander <- 1 - df$autolander
fit1 <- glm(autolander~ wind - 1, 'binomial', df)
fit1$coefficients
## windhead windtail
## 0.2513144 0.2831263
fit2 <- glm(one_minus_autolander~ wind - 1, 'binomial', df)
fit2$coefficients
## windhead windtail
## -0.2513144 -0.2831263
The correct answer is:
The coefficients reverse their signs.
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).
df <- InsectSprays
fit <- glm(count ~ spray - 1, family='poisson', df)
exp(fit$coef[1])/exp(fit$coef[2])
## sprayA
## 0.9456522
Consider a Poisson glm with an offset, tt. 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 t2<-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.)
Answer:
As the Poisson regression fits log of expectation by the linear regression, when all the regressor data is multiplied by an offset, then the log of expectation is shifted, so the intercept is changes and the slope coefficients remains intact. So, the answer is:
The coefficient estimate is unchanged
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)
fit <- lm(y ~ xMat)
yhat <- predict(fit)
plot(x, y, frame = FALSE, pch = 21, bg = "lightblue", cex = 2)
lines(x, yhat, col = "red", lwd = 2)
print(paste0('Answer: ', fit$coefficients[3] + fit$coefficients[4]))
## [1] "Answer: 1.01306744868035"