---
title: "Inference for Regression"
#author: "Mark"
date: "11/29/2017"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

You might recall that [we talked about linear regression](https://marksmath.org/classes/Fall2017Stat185/outline/09.13.17.Regression.html) a couple of months ago. A simple example is givrn by this look at our CDC data relating height and weight:

```{r, echo=F}
set.seed(1)
cdc = read.csv("../../../data/cdc.csv")
men = subset(cdc, gender=='m')
subset = men[sample(1:20000,2000),]
cdc_fit = lm(subset$weight~subset$height)
plot(subset$height, subset$weight, pch=21,  bg="blue", xlab='height', ylab='weight')
abline(cdc_fit, lwd=2)
#title(paste('correlation = ', sprintf('%.5f', 
#    cor(subset$weight,subset$height, use='na.or.complete')))
#)
title(
  sprintf('R = %.4f\n W = %.2f H%.2f', 
    cor(subset$height, subset$weight, use='na.or.complete'),
    cdc_fit$coefficients['subset$height'], cdc_fit$coefficients['(Intercept)'])
)
```

As we mentioned before, the correlation of about $0.42$ is a quantitative assessment of the relationship between the variables and the formula $W=4.87H-154.24$ yields an estimate of the weight $W$ in terms of the height $H$.

Here's the thing though: If you [look back at those previous notes](https://marksmath.org/classes/Fall2017Stat185/outline/09.13.17.Regression.html) you'll find slightly different numbers. The reason is that the data is a random sample of the $20,000$ men in the study. If we take a different random sample, then we'll get different numbers. Thus, the coefficients in linear regression can be considered as sample statistics so we have standard errors and $p$-values associated with them.


### Using R to run a regression test

Let's discuss how we might interpret the following:

```{r}
set.seed(1)
cdc = read.csv("https://www.marksmath.org/data/cdc.csv")
men = subset(cdc, gender=='m')
subset = men[sample(1:length(men$height),50),]
cdc_fit = lm(subset$weight~subset$height)
summary(cdc_fit)
```
 
#### Questions
 
  * What is the formula relating weigth to height?
  * What does the formula predict for the weight of a man who is 72 inches tall?
  * What hypothesis might we check with this model?
  * Write down the result of the hypothesis test.

If I could run this type of hypothesis test in Javascript, I'd add it to my [CFB Data demo](https://marksmath.org/visualization/CFB_Stats/)!

 ------
 
 ------
 
 