1 R Markdown

This is an R Markdown document of Rongbin Ye, for JHU certificate specilization in data scientist. This specific file records the answers for the quiz 4 of the week 4. # Questions ## 1.Question 1 Consider the space shuttle data 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).

A. 0.031 B. 0.969 C. -0.031 D. 1.327

1.0.1 Answer:

The answer is B.

library(MASS)
data(shuttle)
?shuttle
head(shuttle)
##   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
mdl1 <- glm(use ~ wind, binomial, shuttle)

exp(mdl1$coefficients)
## (Intercept)    windtail 
##   0.7777778   0.9686888

1.1 Question2

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. A. 1.00 B. 0.969 C. 0.684 D. 1.485

1.1.1 Answer:

The answer is B.

# Need to adjusted by magn
mdl2 <- glm(use ~  magn+wind, binomial, shuttle)

exp(mdl2$coefficients)
## (Intercept)  magnMedium     magnOut  magnStrong    windtail 
##   0.6952323   1.0000000   1.4615736   1.0665323   0.9684981

1.2 Question 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?

A. The coefficients reverse their signs. B. The coefficients change in a non-linear fashion. C. The intercept changes sign, but the other coefficients don’t. D. The coefficients get inverted(One over previous value)

1.2.1 Answer:

The sign has been changed by minus one. This will change the numerator and denoinator for the exponent.

# Based on the discussion in swirl() and rational of turning logits into the distribution between 1 and 0. The answer should be D. 

1.3 Question4

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). A. 0.136 B. 0.9457 C. 0.321 D. -0.056

data("InsectSprays")

mdl3 <- glm(count ~ relevel(spray, "B"), poisson, InsectSprays)

exp(mdl3$coefficients)[2]
## relevel(spray, "B")A 
##            0.9456522

1.3.1 Answer:

Answer is B.

1.4 Question 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) + 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.)

A. The coefficient is subtracted by log(10) B. The coefficient estimate is divided by 10 C. The coefficient estimate is multiplied by 10 D. The coefficient estimate is unchanged.

1.4.1 Answer:

There is nothing to do with the coefficient. Yet the intercepts are probably changed by the log(10) ## Question 6 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 <- c(0,1,2,3,4,5)
y <- c(0.08, 0.93, 2.05, 2.54, 3.87, 4.97)

all <- rbind(x,y)
df_a <- data.frame(all)
fit <- lm(y ~ x, df_a)
coef(fit)
##  (Intercept)            x 
## -0.004761905  0.964571429
# 1.013 is the cloest the answer.