library(MASS)
df <- shuttle
df$use<-as.numeric(df$use=='auto')
mdl <- glm(use~factor(wind)-1, family = binomial, df)
mdl
##
## Call: glm(formula = use ~ factor(wind) - 1, family = binomial, data = df)
##
## Coefficients:
## factor(wind)head factor(wind)tail
## 0.2513 0.2831
##
## Degrees of Freedom: 256 Total (i.e. Null); 254 Residual
## Null Deviance: 354.9
## Residual Deviance: 350.3 AIC: 354.3
oddh <- exp(mdl$coef[1])
oddt <- exp(mdl$coef[2])
oddh/oddt
## factor(wind)head
## 0.9686888
mdl2 <- glm(use~factor(wind)+ factor(magn)-1, family = binomial, df)
mdl2$coef
## factor(wind)head factor(wind)tail factor(magn)Medium
## 3.635093e-01 3.955180e-01 -1.009525e-15
## factor(magn)Out factor(magn)Strong
## -3.795136e-01 -6.441258e-02
exp(mdl2$coef[[1]])/exp(mdl2$coef[[2]])
## [1] 0.9684981
mdl3 <- glm((1-use)~factor(wind)-1, family = binomial, df)
mdl3
##
## Call: glm(formula = (1 - use) ~ factor(wind) - 1, family = binomial,
## data = df)
##
## Coefficients:
## factor(wind)head factor(wind)tail
## -0.2513 -0.2831
##
## Degrees of Freedom: 256 Total (i.e. Null); 254 Residual
## Null Deviance: 354.9
## Residual Deviance: 350.3 AIC: 354.3
head(InsectSprays)
## count spray
## 1 10 A
## 2 7 A
## 3 20 A
## 4 14 A
## 5 14 A
## 6 12 A
mdl4 <- glm(count~factor(spray)-1, poisson, InsectSprays)
mdl4
##
## Call: glm(formula = count ~ factor(spray) - 1, family = poisson, data = InsectSprays)
##
## Coefficients:
## factor(spray)A factor(spray)B factor(spray)C factor(spray)D
## 2.674 2.730 0.734 1.593
## factor(spray)E factor(spray)F
## 1.253 2.813
##
## Degrees of Freedom: 72 Total (i.e. Null); 66 Residual
## Null Deviance: 2265
## Residual Deviance: 98.33 AIC: 376.6
exp(mdl4$coef[1])/exp(mdl4$coef[2])
## factor(spray)A
## 0.9456522
The intercept changes, but the coefficient estimate is unchanged.
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 <- 0
splineTerms <- (x>0)*(x-0)
xmat <- cbind(1, x, splineTerms)
mdl6 <- lm(y~xmat-1)
mdl6$coef[2]+mdl6$coef[3]
## xmatx
## 1.013067
In the coefficients R subtracted the mean slope of the first line from that of the second, so we can simply add it back to get the true value.