Consider the space shuttle data 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")
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")
shuttle2 <- mutate(shuttle,auto=1*(use=="auto"))
head(shuttle2)
## stability error sign wind magn vis use auto
## 1 xstab LX pp head Light no auto 1
## 2 xstab LX pp head Medium no auto 1
## 3 xstab LX pp head Strong no auto 1
## 4 xstab LX pp tail Light no auto 1
## 5 xstab LX pp tail Medium no auto 1
## 6 xstab LX pp tail Strong no auto 1
fit <- glm(auto~factor(wind)-1,family =binomial(link="logit"),data=shuttle2)
summary(fit)$coef
## Estimate Std. Error z value Pr(>|z|)
## factor(wind)head 0.2513144 0.1781742 1.410499 0.1583925
## factor(wind)tail 0.2831263 0.1785510 1.585689 0.1128099
LogoddRatioHeadTail <- summary(fit)$coef[1,1]-summary(fit)$coef[2,1]
oddRatioHeadTail <- exp(LogoddRatioHeadTail)
oddRatioHeadTail
## [1] 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~factor(wind)+factor(magn)-1,
family=binomial(link="logit"),data=shuttle2)
summary(fit2)
##
## Call:
## glm(formula = auto ~ factor(wind) + factor(magn) - 1, family = binomial(link = "logit"),
## data = shuttle2)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.349 -1.321 1.015 1.040 1.184
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## factor(wind)head 3.635e-01 2.841e-01 1.280 0.201
## factor(wind)tail 3.955e-01 2.844e-01 1.391 0.164
## factor(magn)Medium -1.010e-15 3.599e-01 0.000 1.000
## factor(magn)Out -3.795e-01 3.568e-01 -1.064 0.287
## factor(magn)Strong -6.441e-02 3.590e-01 -0.179 0.858
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 354.89 on 256 degrees of freedom
## Residual deviance: 348.78 on 251 degrees of freedom
## AIC: 358.78
##
## Number of Fisher Scoring iterations: 4
LogoddRatioHeadTail2 <- summary(fit2)$coef[1,1]-summary(fit2)$coef[2,1]
oddRatioHeadTail2 <- exp(LogoddRatioHeadTail2)
oddRatioHeadTail2
## [1] 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?
fit3 <- glm(use~factor(wind)+factor(magn)-1,
family=binomial(link="logit"),data=shuttle2)
summary(fit3)
##
## Call:
## glm(formula = use ~ factor(wind) + factor(magn) - 1, family = binomial(link = "logit"),
## data = shuttle2)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -1.184 -1.040 -1.015 1.321 1.349
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## factor(wind)head -3.635e-01 2.841e-01 -1.280 0.201
## factor(wind)tail -3.955e-01 2.844e-01 -1.391 0.164
## factor(magn)Medium -4.233e-16 3.599e-01 0.000 1.000
## factor(magn)Out 3.795e-01 3.568e-01 1.064 0.287
## factor(magn)Strong 6.441e-02 3.590e-01 0.179 0.858
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 354.89 on 256 degrees of freedom
## Residual deviance: 348.78 on 251 degrees of freedom
## AIC: 358.78
##
## Number of Fisher Scoring iterations: 4
\(Answer:\) 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 comparing spray A (numerator) to spray B (denominator).
data("InsectSprays")
head(InsectSprays)
## count spray
## 1 10 A
## 2 7 A
## 3 20 A
## 4 14 A
## 5 14 A
## 6 12 A
fit4 <- glm(count~factor(spray)-1,
family =poisson(link="log"),data=InsectSprays)
summary(fit4)
##
## Call:
## glm(formula = count ~ factor(spray) - 1, family = poisson(link = "log"),
## data = InsectSprays)
##
## Deviance Residuals:
## Min 1Q Median 3Q Max
## -2.3852 -0.8876 -0.1482 0.6063 2.6922
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## factor(spray)A 2.67415 0.07581 35.274 < 2e-16 ***
## factor(spray)B 2.73003 0.07372 37.032 < 2e-16 ***
## factor(spray)C 0.73397 0.20000 3.670 0.000243 ***
## factor(spray)D 1.59263 0.13019 12.233 < 2e-16 ***
## factor(spray)E 1.25276 0.15430 8.119 4.71e-16 ***
## factor(spray)F 2.81341 0.07071 39.788 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for poisson family taken to be 1)
##
## Null deviance: 2264.808 on 72 degrees of freedom
## Residual deviance: 98.329 on 66 degrees of freedom
## AIC: 376.59
##
## Number of Fisher Scoring iterations: 5
rateSprayASprayB <- exp(summary(fit4)$coef[1,1]-summary(fit4)$coef[2,1])
rateSprayASprayB
## [1] 0.9456522
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 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:\) 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?
z <- (x > 0) * x
fit <- lm(y ~ x + z)
sum(coef(fit)[2:3])
## [1] 1.013067