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
summary(df)
## stability error sign wind magn vis
## stab :128 LX:64 nn:128 head:128 Light :64 no :128
## xstab:128 MM:64 pp:128 tail:128 Medium:64 yes:128
## SS:64 Out :64
## XL:64 Strong:64
## use
## auto :145
## noauto:111
##
##
head(df)
## stability error sign wind magn vis use
## 1 xstab LX pp head Light no auto
## 2 xstab LX pp head Medium no auto
## 3 xstab LX pp head Strong no auto
## 4 xstab LX pp tail Light no auto
## 5 xstab LX pp tail Medium no auto
## 6 xstab LX pp tail Strong no auto
df$auto <- as.numeric(shuttle$use == "auto")
df$windhead <- as.numeric(shuttle$wind == "head")
fitGLM<-glm(auto ~ windhead , family = binomial, data = df)
exp(coef(fitGLM))
## (Intercept) windhead
## 1.3272727 0.9686888
#or we remove the intercept to get tail and wind coefs
fitGLM<-glm(auto ~ wind-1 , family = binomial, data = df)
exp(coef(fitGLM)[1])/exp(coef(fitGLM)[2])
## 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.
fitGLM<-glm(auto ~ windhead + magn , family = "binomial", data = df)
exp(coef(fitGLM)[2])
## 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?
fitGLM<-glm(auto ~ wind , family = binomial, data = df)
fitGLM2<-glm(1-auto ~ wind , family = binomial, data = df)
cbind(coef(fitGLM),coef(fitGLM2))
## [,1] [,2]
## (Intercept) 0.25131443 -0.25131443
## windtail 0.03181183 -0.03181183
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).
df2<-InsectSprays
fitGLM<-glm(count~spray-1,data=df2,family=poisson)
exp(coef(fitGLM)[1])/exp(coef(fitGLM)[2])
## sprayA
## 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 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.)
glm1 <- glm(count ~ spray +offset(log(count+1)),family="poisson",data=df2)
glm2 <- glm(count ~ spray +offset(log(10)+log(count+1)),family="poisson",data=df2)
coef(glm1)
## (Intercept) sprayB sprayC sprayD sprayE
## -0.066691374 0.003512473 -0.325350713 -0.118451059 -0.184623054
## sprayF
## 0.008422466
coef(glm2)
## (Intercept) sprayB sprayC sprayD sprayE
## -2.369276467 0.003512473 -0.325350713 -0.118451059 -0.184623054
## sprayF
## 0.008422466
cbind(coef(glm1),coef(glm2))
## [,1] [,2]
## (Intercept) -0.066691374 -2.369276467
## sprayB 0.003512473 0.003512473
## sprayC -0.325350713 -0.325350713
## sprayD -0.118451059 -0.118451059
## sprayE -0.184623054 -0.184623054
## sprayF 0.008422466 0.008422466
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?
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)
knots<-0
splineTerms <- sapply(knots, function(knot) (x > knot) * (x - knot))
xMat <- cbind( x, splineTerms)
yhat <- predict(lm(y ~xMat))
{plot(x, y, frame = FALSE, pch = 21, bg = "lightblue", cex = 2)
lines(x, yhat, col = "red", lwd = 2)}