--- title: "sleep" author: "Chris Parrish" date: "January 23, 2016" output: pdf_document --- sleep reference: - Cannon, et al., Stat2, chapter 09, example 9.1-9.3 - Cannon, et al., Student R Manual, chapter 9 - [logit regression](http://www.ats.ucla.edu/stat/r/dae/logit.htm) Import the data. {r} data <- matrix(c(12, 34, 35, 79, 37, 77, 39, 65, 27, 41), nrow=2, dimnames=list(c("Less than 7 hours", "7 hours or more"), c("14","15","16","17","18"))) data  addmargins {r} addmargins(data, 1)  prop.table conditional probabilities: $P(Y \mid Age = age)$ {r} sleep.table <- prop.table(data, 2) sleep.table  Illustration. conditional probabilities: $P(Y = yes \mid Age = age)$ {r} age <- 14:18 plot(age, sleep.table[2 , ], pch=20, col="darkred", xlab="Age (yrs)", ylab="Proportion saying Yes")  Logistic regression. $\pi = P(Y = 1)$ $\log(odds) = \log(\frac{\pi}{1 - \pi}) = \beta_0 + \beta_1 \cdot X_1$ {r} sleep.data <- cbind(data[2,], data[1,]) # yes and no data as columns 1 and 2 sleep.data sleep.glm <- glm(sleep.data ~ age, family=binomial) options(show.signif.stars=FALSE) summary(sleep.glm)  \newpage Prediction. {r} new.data <- data.frame(age=0:40) yhat <- predict(sleep.glm, new.data, type="response") plot(0:40, yhat, pch='', type="l", ylim=c(-0.1, 1.1), xlim=c(0,40), xlab="Age", ylab="Proportion Saying Yes") points(age, sleep.table[2, ], pch=20, col="darkred") abline(h=1, lty=2, col="orange") abline(h=0, lty=2, col="orange")  logit and inv.logit are in the boot package. $logit : \pi \rightarrow log.odds \qquad logit(\pi) = \log(\frac{\pi}{1 - \pi})$ $inv.logit : log.odds \rightarrow \pi \qquad inv.logit(lo) = \frac{\exp(lo)}{1 + \exp(lo)}$ {r} library(boot) curve(logit(x), from=0, to=1, col="darkred", main="logit") curve(inv.logit(x), from=-4, to=4, col="darkred", main="inv.logit")  \newpage Parameter values determine the shape of the logistic curve. $midpoint_{\pi=0.5}$ occurs at $x = -\beta_0 / \beta_1$ $slope_{\pi=0.5}$ is $\beta_1 / 4$ {r} beta0 <- 4 beta1 <- -2 curve(inv.logit(beta0 + beta1 * x), from=0, to=4, col="darkred", main="inv.logit") abline(h=0.5, col="orange", lty=2)  Check the value of $inv.logit(\beta_0 + \beta_1 * x)$ and its derivative at $x = 2$ {r} val.at.x.equal.2 <- inv.logit(beta0 + beta1 * 2.00) val.at.x.equal.2 approx.derivative.at.x.equal.2 <- (inv.logit(beta0 + beta1 * 2.01) - inv.logit(beta0 + beta1 * 2.00)) / 0.01 approx.derivative.at.x.equal.2