Available on March 15th.
Due March 19th (by midnight)
Covers chapters 1 through 5.
20 multiple choice questions.
You may use your notes, textbook, and course site. Do not consult with anyone else.
What was the most important thing you learned during this class?
What important question remains unanswered for you?
There are three major concepts in calculus that will be helpful to understand:
Limits - the value that a function (or sequence) approaches as the input (or index) approaches some value.
Derivatives - the slope of the line tangent at any given point on a function.
Integrals - the area under the curve.
f(x|μ,σ)=1σ√2πe−(x−μ)22σ2
f <- function(x, mean = 0, sigma = 1) { 1 / (sigma * sqrt(2 * pi)) * exp(1)^(-1/2 * ( (x - mean) / sigma )^2)}
min <- 0; max <- 2ggplot() + stat_function(fun = f) + xlim(c(-4, 4)) + geom_vline(xintercept = c(min, max), color = 'blue', linetype = 2) + xlab('x')
One strategy to find the area between two values is to draw a series of rectangles. Given n rectangles, we know that the width of each is 2−0n and the height is f(x). Here is an example with 3 rectangles.
As n approaches infinity we are going to get the exact value for the area under the curve. This notion of letting a value get increasingly close to infinity, zero, or any other value, is called the limit.
The area under a function is called the integral.
integrate(f, 0, 2)
## 0.4772499 with absolute error < 5.3e-15
DATA606::shiny_demo('calculus')
normal_plot(cv = c(0, 2))
pnorm(2) - pnorm(0)
## [1] 0.4772499
See https://github.com/jbryer/DATA606Fall2021/blob/master/R/distributions.R
n <- 1e5pop <- runif(n, 0, 1)mean(pop)
## [1] 0.5010507
samp1 <- sample(pop, size=10)mean(samp1)
## [1] 0.5462003
hist(samp1)
samp2 <- sample(pop, size=30)mean(samp2)
## [1] 0.5380609
hist(samp2)
M <- 1000samples <- numeric(length=M)for(i in seq_len(M)) { samples[i] <- mean(sample(pop, size=30))}head(samples, n=8)
## [1] 0.4481185 0.5153039 0.5844942 0.4960197 0.4552045 0.3678853 0.4533659## [8] 0.5247244
hist(samples)
Let X1, X2, ..., Xn be independent, identically distributed random variables with mean μ and variance σ2, both finite. Then for any constant z,
limn→∞P(ˉX−μσ/√n≤z)=Φ(z)
where Φ is the cumulative distribution function (cdf) of the standard normal distribution.
The distribution of the sample mean is well approximated by a normal model:
ˉx∼N(mean=μ,SE=σ√n)
where SE represents the standard error, which is defined as the standard deviation of the sampling distribution. In most cases σ is not known, so use s.
library(DATA606)shiny_demo('sampdist')shiny_demo('CLT_mean')
samp2 <- sample(pop, size=30)mean(samp2)
## [1] 0.5950164
(samp2.se <- sd(samp2) / sqrt(length(samp2)))
## [1] 0.05579335
The confidence interval is then μ±CV×SE where CV is the critical value. For a 95% confidence interval, the critical value is ~1.96 since
∫1.96−1.961σ√2πd−(x−μ)22σ2≈0.95
qnorm(0.025) # Remember we need to consider the two tails, 2.5% to the left, 2.5% to the right.
## [1] -1.959964
(samp2.ci <- c(mean(samp2) - 1.96 * samp2.se, mean(samp2) + 1.96 * samp2.se))
## [1] 0.4856615 0.7043714
We are 95% confident that the true population mean is between 0.4856615, 0.7043714.
That is, if we were to take 100 random samples, we would expect at least 95% of those samples to have a mean within 0.4856615, 0.7043714.
ci <- data.frame(mean=numeric(), min=numeric(), max=numeric())for(i in seq_len(100)) { samp <- sample(pop, size=30) se <- sd(samp) / sqrt(length(samp)) ci[i,] <- c(mean(samp), mean(samp) - 1.96 * se, mean(samp) + 1.96 * se)}ci$sample <- 1:nrow(ci)ci$sig <- ci$min < 0.5 & ci$max > 0.5
ggplot(ci, aes(x=min, xend=max, y=sample, yend=sample, color=sig)) + geom_vline(xintercept=0.5) + geom_segment() + xlab('CI') + ylab('') + scale_color_manual(values=c('TRUE'='grey', 'FALSE'='red'))
We start with a null hypothesis ( H0 ) that represents the status quo.
We also have an alternative hypothesis ( HA ) that represents our research question, i.e. what we're testing for.
We conduct a hypothesis test under the assumption that the null hypothesis is true, either via simulation or traditional methods based on the central limit theorem.
If the test results suggest that the data do not provide convincing evidence for the alternative hypothesis, we stick with the null hypothesis. If they do, then we reject the null hypothesis in favor of the alternative.
H0: The mean of samp2
= 0.5
HA: The mean of samp2
≠ 0.5
Using confidence intervals, if the null value is within the confidence interval, then we fail to reject the null hypothesis.
(samp2.ci <- c(mean(samp2) - 1.96 * sd(samp2) / sqrt(length(samp2)), mean(samp2) + 1.96 * sd(samp2) / sqrt(length(samp2))))
## [1] 0.4856615 0.7043714
Since 0.5 fall within 0.4856615, 0.7043714, we fail to reject the null hypothesis.
ˉx∼N(mean=0.49,SE=0.27√30=0.049)
Z=ˉx−nullSE=0.49−0.500.049=−.204081633
pnorm(-.204) * 2
## [1] 0.8383535
DATA606::normal_plot(cv = c(.204), tails = 'two.sided')
There are two competing hypotheses: the null and the alternative. In a hypothesis test, we make a decision about which might be true, but our choice might be incorrect.
fail to reject H0 | reject H0 | |
---|---|---|
H0 true | ✔ | Type I Error |
HA true | Type II Error | ✔ |
If we again think of a hypothesis test as a criminal trial then it makes sense to frame the verdict in terms of the null and alternative hypotheses:
H0 : Defendant is innocent
HA : Defendant is guilty
Which type of error is being committed in the following circumstances?
Declaring the defendant innocent when they are actually guilty
Declaring the defendant guilty when they are actually innocent
Which error do you think is the worse error to make?
(cv <- qnorm(0.05, mean=0, sd=1, lower.tail=FALSE))
## [1] 1.644854
pnorm(cv, mean=cv, lower.tail = FALSE)
## [1] 0.5
mu <- 2.5(cv <- qnorm(0.05, mean=0, sd=1, lower.tail=FALSE))
## [1] 1.644854
Type I Error
pnorm(mu, mean=0, sd=1, lower.tail=FALSE)
## [1] 0.006209665
Type II Error
pnorm(cv, mean=mu, lower.tail = TRUE)
## [1] 0.1962351
Check out this page: https://r.bryer.org/shiny/Why05/
See also:
Kelly M. Emily Dickinson and monkeys on the stair Or: What is the significance of the 5% significance level? Significance 10:5. 2013.
Real differences between the point estimate and null value are easier to detect with larger samples.
However, very large samples will result in statistical significance even for tiny differences between the sample mean and the null value (effect size), even when the difference is not practically significant.
This is especially important to research: if we conduct a study, we want to focus on finding meaningful results (we want observed differences to be real, but also large enough to matter).
The role of a statistician is not just in the analysis of data, but also in planning and design of a study.
First introduced by Efron (1979) in Bootstrap Methods: Another Look at the Jackknife.
Estimates confidence of statistics by resampling with replacement.
The bootstrap sample provides an estimate of the sampling distribution.
The boot
R package provides a framework for doing bootstrapping: https://www.statmethods.net/advstats/bootstrapping.html
Define our population with a uniform distribution.
n <- 1e5pop <- runif(n, 0, 1)mean(pop)
## [1] 0.4990189
We observe one random sample from the population.
samp1 <- sample(pop, size = 50)
boot.samples <- numeric(1000) # 1,000 bootstrap samplesfor(i in seq_along(boot.samples)) { tmp <- sample(samp1, size = length(samp1), replace = TRUE) boot.samples[i] <- mean(tmp)}head(boot.samples)
## [1] 0.5135789 0.5028579 0.5116830 0.4688500 0.5333870 0.5043232
d <- density(boot.samples)h <- hist(boot.samples, plot=FALSE)hist(boot.samples, main='Bootstrap Distribution', xlab="", freq=FALSE, ylim=c(0, max(d$y, h$density)+.5), col=COL[1,2], border = "white", cex.main = 1.5, cex.axis = 1.5, cex.lab = 1.5)lines(d, lwd=3)
c(mean(boot.samples) - 1.96 * sd(boot.samples), mean(boot.samples) + 1.96 * sd(boot.samples))
## [1] 0.4077162 0.5704055
boot.samples.median <- numeric(1000) # 1,000 bootstrap samplesfor(i in seq_along(boot.samples.median)) { tmp <- sample(samp1, size = length(samp1), replace = TRUE) boot.samples.median[i] <- median(tmp) # NOTICE WE ARE NOW USING THE median FUNCTION!}head(boot.samples.median)
## [1] 0.4904070 0.2271723 0.3932846 0.3834050 0.5891012 0.4172554
95% confidence interval for the median
c(mean(boot.samples.median) - 1.96 * sd(boot.samples.median), mean(boot.samples.median) + 1.96 * sd(boot.samples.median))
## [1] 0.3021439 0.6447989
Complete the one minute paper: https://forms.gle/p9xcKcTbGiyYSz368
Available on March 15th.
Due March 19th (by midnight)
Covers chapters 1 through 5.
20 multiple choice questions.
You may use your notes, textbook, and course site. Do not consult with anyone else.
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |