Last updated on 2017-05-15
ggplot2?joinSo far we've seen simple linear regression
In Lec 36 LC we saw the relationship between \(x =\) dep delay & \(y =\) arr delay for Alaska Airlines flights.
carrier doesn't vary.carrier == F9)So we have:
Is there a difference in delays between Alaska and Frontier?
Is there a difference in delays between Alaska and Frontier?
What does "best fitting line"" mean?
Consider ANY point (in blue).
Now consider this point's deviation from the regression line.
Do this for another point…
Do this for another point…
Regression line minimizes the sum of squared arrow lengths.
n=100Here are your 12 resulting \(\widehat{p}\)'s…
| p_hat | |
|---|---|
| aghall | 0.360 |
| ccrobinson | 0.402 |
| chimstead | 0.380 |
| cwhitedzuro | 0.440 |
| dmortime | 0.430 |
| efeldman | 0.370 |
| jobrien | 0.400 |
| jvolz | 0.420 |
| lschroer | 0.402 |
| rlightman | 0.400 |
| rstoreyfisher | 0.390 |
| zmillslagle | 0.402 |
Let me add 8 of my own so we have 20…
| p_hat | |
|---|---|
| aghall | 0.360 |
| ccrobinson | 0.402 |
| chimstead | 0.380 |
| cwhitedzuro | 0.440 |
| dmortime | 0.430 |
| efeldman | 0.370 |
| jobrien | 0.400 |
| jvolz | 0.420 |
| lschroer | 0.402 |
| rlightman | 0.400 |
| rstoreyfisher | 0.390 |
| zmillslagle | 0.402 |
| aykim | 0.420 |
| aykim | 0.360 |
| aykim | 0.300 |
| aykim | 0.360 |
| aykim | 0.360 |
| aykim | 0.400 |
| aykim | 0.340 |
| aykim | 0.400 |
Let's compute \(\mbox{SE} = \sqrt{\frac{\widehat{p}(1-\widehat{p})}{n}}\)…
p_hat <- p_hat %>%
mutate(
n = 100,
SE = sqrt(p_hat*(1-p_hat)/n)
)
| p_hat | n | SE | |
|---|---|---|---|
| aghall | 0.360 | 100 | 0.048 |
| ccrobinson | 0.402 | 100 | 0.049 |
| chimstead | 0.380 | 100 | 0.049 |
| cwhitedzuro | 0.440 | 100 | 0.050 |
| dmortime | 0.430 | 100 | 0.050 |
| efeldman | 0.370 | 100 | 0.048 |
| jobrien | 0.400 | 100 | 0.049 |
| jvolz | 0.420 | 100 | 0.049 |
| lschroer | 0.402 | 100 | 0.049 |
| rlightman | 0.400 | 100 | 0.049 |
| rstoreyfisher | 0.390 | 100 | 0.049 |
| zmillslagle | 0.402 | 100 | 0.049 |
| aykim | 0.420 | 100 | 0.049 |
| aykim | 0.360 | 100 | 0.048 |
| aykim | 0.300 | 100 | 0.046 |
| aykim | 0.360 | 100 | 0.048 |
| aykim | 0.360 | 100 | 0.048 |
| aykim | 0.400 | 100 | 0.049 |
| aykim | 0.340 | 100 | 0.047 |
| aykim | 0.400 | 100 | 0.049 |
Finally the left and right end points of the 95% confidence interval. Whose CI's captured the true \(p=0.4023\)?
p_hat <- p_hat %>%
mutate(
left = p_hat - 1.96*SE,
right = p_hat + 1.96*SE
)
| p_hat | n | SE | left | right | |
|---|---|---|---|---|---|
| aghall | 0.360 | 100 | 0.048 | 0.266 | 0.454 |
| ccrobinson | 0.402 | 100 | 0.049 | 0.306 | 0.498 |
| chimstead | 0.380 | 100 | 0.049 | 0.285 | 0.475 |
| cwhitedzuro | 0.440 | 100 | 0.050 | 0.343 | 0.537 |
| dmortime | 0.430 | 100 | 0.050 | 0.333 | 0.527 |
| efeldman | 0.370 | 100 | 0.048 | 0.275 | 0.465 |
| jobrien | 0.400 | 100 | 0.049 | 0.304 | 0.496 |
| jvolz | 0.420 | 100 | 0.049 | 0.323 | 0.517 |
| lschroer | 0.402 | 100 | 0.049 | 0.306 | 0.498 |
| rlightman | 0.400 | 100 | 0.049 | 0.304 | 0.496 |
| rstoreyfisher | 0.390 | 100 | 0.049 | 0.294 | 0.486 |
| zmillslagle | 0.402 | 100 | 0.049 | 0.306 | 0.498 |
| aykim | 0.420 | 100 | 0.049 | 0.323 | 0.517 |
| aykim | 0.360 | 100 | 0.048 | 0.266 | 0.454 |
| aykim | 0.300 | 100 | 0.046 | 0.210 | 0.390 |
| aykim | 0.360 | 100 | 0.048 | 0.266 | 0.454 |
| aykim | 0.360 | 100 | 0.048 | 0.266 | 0.454 |
| aykim | 0.400 | 100 | 0.049 | 0.304 | 0.496 |
| aykim | 0.340 | 100 | 0.047 | 0.247 | 0.433 |
| aykim | 0.400 | 100 | 0.049 | 0.304 | 0.496 |
Recall the nycflights data set. For Alaska Air flights, let's explore the relationship between
The correlation coefficient is computed as follows:
cor(alaska_flights$dep_delay, alaska_flights$arr_delay)
## [1] 0.8373792
83.7% is fairly strongly positively associated!
Chalk talk
For large \(n\), the sampling distribution for these point estimates are bell-shaped, thus a 95% C.I. is \(\mbox{PE} \pm 1.96\times \mbox{SE}\).
| Population Parameter | Sample Statistic |
|---|---|
| Mean \(\mu\) | Sample Mean \(\overline{x}\) |
| Proportion \(p\) | Sample Proportion \(\widehat{p}\) |
| Diff of Means \(\mu_1 - \mu_2\) | \(\overline{x}_1 - \overline{x}_2\) |
| Diff of Proportions \(p_1 - p_2\) | \(\widehat{p}_1 - \widehat{p}_2\) |
NPR report on Obama from 2013. Chalk talk…
We are estimating a population parameter using a point estimate based on a sample. Example: Mean (Chalk Talk)
Imagine the \(\mu\) is a fish:
| Point Estimate \(\overline{x}\) | Confidence Interval |
|---|---|
Age example:
n=3 studentsNote:
From the OkCupid population:
nTaking a sample in order to infer about a population:
Let's Google "define infer"…
library(lubridate)
library(mosaic)
library(dplyr)
# Randomly sample three people:
students <-
c("Arthur", "Caroline", "Claire", "Clare", "Conor", "Daniel",
"Dylan", "Elana", "Jacob", "Jay", "Joe", "Julian", "Kelsie",
"Lisa", "Maya", "Naing", "Parker", "Rebecca", "Ry", "Theodora",
"Zebediah", "Albert")
resample(students, size=3, replace=FALSE)
# Get average age:
birthdays <- c("1980-11-05", "2000-01-01", "1955-08-05")
ages <- as.numeric(as.Date("2017-04-27") - as.Date(birthdays))/365.25
ages
mean(ages)
Questions:
Chalk talk…
If we assume \(H_0\) is true (there is no difference in test scores between evens and odds) then:
even_vs_odd is irrelevant| final | even_vs_odd |
|---|---|
| 0.94 | even |
| 0.88 | odd |
| 0.84 | even |
| 0.84 | odd |
| 0.77 | even |
| final | even_vs_odd |
|---|---|
| 0.94 | even |
| 0.88 | odd |
| 0.84 | even |
| 0.84 | even |
| 0.77 | odd |
| final | even_vs_odd |
|---|---|
| 0.94 | even |
| 0.88 | even |
| 0.84 | odd |
| 0.84 | even |
| 0.77 | odd |
| final | even_vs_odd |
|---|---|
| 0.94 | even |
| 0.88 | even |
| 0.84 | odd |
| 0.84 | odd |
| 0.77 | even |
From last lecture: How do we construct null distribution?
In this case, the null distribution is barplot:
| Analytically | Via Simulation |
|---|---|
Only chalk talk today, based on Learning Checks for Lec26.
Not very! Only occurs 0.34% of the time
p-value: Chalk Talk
If guessing at random, here are hypothetical outcomes:
She got 8/8 right!
Critical chalk talk.
ggplot2?joinBinary situations, like
are often coded as 1 vs 0 in many programming languages.
Ezell's Fried Chicken is a famous chicken restaurant in Seattle. Oprah Winfrey has it flown into to Chicago.
One day I was raving about Ezell's Chicken, but my friend accused me of "buying into the hype".
So what did we do?
Fried Chicken Face Off:
| Do people prefer this? | Or this? |
|---|---|
How would you design a taste test to ascertain, independent of hype, which fried chicken tastes better?
Use the relevant principles of designing experiements from above.
The mosaic package has functions for the random simulation.
rflip(): Flip a coinshuffle(): Shuffle a set of valuesdo(): Do the same thing many, many, many timesresample(): the swiss army knife for samplingRun the following in your console:
library(mosaic)
# Define a vector fruit
fruit <- c("apple", "orange", "mango")
# Do this multiple times:
shuffle(fruit)
Two types of sampling:
resample() by default samples with replacement. Run this in the console multiple times:
resample(fruit)
resample()Chalk Talk
Chalk Talk 1
There are two approaches to studying probability:
| Mathematically (MATH 310) | Via Simulations |
|---|---|
Doing this repeatedly by hand is tiring:
All hail the mosaic package: library(mosaic).
Chalk Talk 2
Best viewed in HTML mode, not slide deck mode:
You should draw out what your end data frame should look like in tidy format:
Why? If you don't clearly identify this, not only will your work not be focused, but more importantly, how would you know when you're done?
Before starting any substantive data wrangling using mutate, summarise, arrange, or _join, I like to pare down the necessary data sets to the minimum of what I need by
filter only the absolutely necessary rowsselect only the absolutely neccesary columnsWhy? This has several benefits:
View()s of your work as you progress.nycflights13 data sets: flights, planes, airlines, weather, and airports.Why? If you confuse the what and the how, you'll only get doubly lost. Separate them out!
Done with "Tidy" and "Transform", start with "Model":
Growing up I used to only eat white rice, but now I only eat multigrain rice.
| White Rice | Multigrain Rice |
|---|---|