library(Lock5withR)
## Warning: package 'Lock5withR' was built under R version 3.4.4
data(SleepStudy)
Set things up:
suballnight <- subset(SleepStudy, AllNighter == 1)
subno <- subset(SleepStudy, AllNighter == 0)
m <- 1e5
outallnight <- outno <- rep(0,m)
set.seed(1234)
for(i in 1:m){
outno[i] <- median(sample(subno$ClassesMissed, 219, replace = TRUE))
outallnight[i] <- median(sample(suballnight$ClassesMissed, 34, replace = TRUE))
}
90 percent CI for median number of classes missed for students who’ve pulled an all nighter recently:
quantile(outallnight, c(.05, .95))
## 5% 95%
## 2 3
90 percent CI for median number of classes missed for students who have NOT pulled an all nighter recently:
quantile(outno, c(.05, .95))
## 5% 95%
## 1 1
The null for both versions is \(\mu = 3315\).
The test stat is \[\dfrac{\bar{x}-\mu_0}{\sigma/\sqrt{n}} \sim N(0,1).\]
Here’s our test stat with numbers plugged in.
(teststat <- (3189-3315)/(450/sqrt(30)))
## [1] -1.533623
It has a standard normal distribution.
The alternative is \(\mu \neq 3315\) so the pvalue is
2*pnorm(abs(teststat), lower.tail =FALSE)
## [1] 0.1251224
which is larger than alpha = .1 so we do not reject the null. There’s no evidence that Jerusalem babies birth weights differ from 3315 grams, on average.
The alternative is \(\mu < 3315\) so the pvalue is
pnorm(teststat, lower.tail = TRUE)
## [1] 0.06256119
which is smaller than alpha = .1, so we do reject the null. Babies born in Jerusalem weigh significantly less than 3315 grams, on average.
Our test stat is \[\dfrac{\bar{x}-\mu_0}{s/\sqrt{n}} \sim T_{n-1}.\]
For both versions, the test stat’s sampling distribution is T with 149 degrees of freedom.
Null is \(\mu = 1\). Alternative hyp is \(\mu \neq 1\) (two sided).
data(iris)
(numerator <- mean(iris$Petal.Width) - 1)
## [1] 0.1993333
(denominator <- sd(iris$Petal.Width)/sqrt(length(iris$Petal.Width)))
## [1] 0.06223645
(teststat <- numerator/denominator)
## [1] 3.202839
(pval <- 2*pt(abs(teststat), df = length(iris$Petal.Width) -1 , lower.tail = FALSE))
## [1] 0.001664123
Pvalue’s tiny so reject in favor of alternative. The mean petal width is not 1.
Null is \(\mu = 4\). Alternative hyp is \(\mu \neq 4\) (two sided).
(numerator <- mean(iris$Petal.Length) - 4)
## [1] -0.242
(denominator <- sd(iris$Petal.Length)/sqrt(length(iris$Petal.Length)))
## [1] 0.144136
(teststat <- numerator/denominator)
## [1] -1.67897
(pval <- 2*pt(abs(teststat), df = length(iris$Petal.Length) -1 , lower.tail = FALSE))
## [1] 0.09525381
Pvalue’s smaller than alpha of .1 so reject in favor of alternative. The mean petal length is not 4.
Solutions are the same for VI and VII.
The null hypothesis is the two means are equal \(\mu_1 = \mu_2\). The alternative is they aren’t equal \(\mu_1 \neq \mu_2\).
attach(chickwts)
data1 <- weight[feed=="casein"]
data2 <- weight[feed=="horsebean"]
meancase <- mean(data1)
meanhorse <- mean(data2)
ncase <- length(data1)
nhorse <- length(data2)
varcase <- var(data1)
varhorse <- var(data2)
top <- (ncase-1)*varcase + (nhorse-1)*varhorse
bottom <- ncase+nhorse-2
varpooled <- top/bottom
spooled <- sqrt(varpooled)
(teststat4 <- (meancase - meanhorse)/ (spooled * sqrt((1/ncase)+(1/nhorse))))
## [1] 7.019741
(pval4 <- 2*pt(teststat4, df=(ncase+nhorse-2), lower.tail = FALSE))
## [1] 8.254541e-07
The test stat is 7.019741 and the test stat’s sampling distribution is T with 20 degrees of freedom. The p-value’s tiny so we reject the null. There is a significant difference in the mean weights of chicks fed casein and horsebean.
The null hyp is that they did not gain weight, on average. In other words, if we define a parameter \(\mu\) to be the mean weight gained, the null says \(\mu =0\). The alternative is they did gain weight: \(\mu>0\) (one sided alternative).
Our test stat is \[\dfrac{\bar{x}-0}{s/\sqrt{n}} \sim T_{n-1}.\]
library(MASS)
data(anorexia)
diffdata <- anorexia$Postwt - anorexia$Prewt
meandiff <- mean(diffdata)
(n5 <- length(diffdata))
## [1] 72
sddiff <- sd(diffdata)
(teststatanorexia <- meandiff / (sddiff/sqrt(n5)))
## [1] 2.93757
(pval5 <- pt(teststatanorexia, df= n5-1, lower.tail = FALSE))
## [1] 0.002228859
Our test-stat follows a t distribution. Since there are 72 women in the study, we have 71 degrees of freedom for our T distribution. Our test stat is 2.9375697 and so our p-value is 0.0022289 \(< \alpha = .01\), so we reject the null hypothesis in favor of the alternative. The anorexic women gained weight, on average. (yay!)
This is the same for versions 1 and 2.
Let p be the proportion of fans who approve this decision. \(H_0: p = p_0\) and \(H_A: p > p_0\).
Our test stat is \[\dfrac{\hat{p}-p_0}{\sqrt{p_0 (1-p_0)/n}} \sim N(0,1)\].
y <- 550
n6 <- 1278
po <- .4
p <- y/n6
varbottom <- (po * (1-po))/ (n6)
sdbot <- sqrt(varbottom)
(teststatbeer <- (p-po)/ (sdbot))
## [1] 2.215443
(thepval <- pnorm(teststatbeer, lower.tail = FALSE))
## [1] 0.01336482
The pvalue is 0.0133648 \(< \alpha = .05\) so we reject the null hypothesis in favor of the alternative. More than 40% of fans approve of the new policy.
\(H-0\) is that Northeasterners and Midwesterners are equally likely to know how to ride a bike. In other words, \(p_N = p_M\). The alternative is that Midwesterners are more likely to know how to, so \(p_M - p_N >0\).
The test stat is \[ \dfrac{\hat{p}_M - \hat{p}_n}{\sqrt{\hat{p}(1- \hat{p}) (1/n_M+1/n_N)}} \sim N(0,1)\]. where \(\hat{p}\) is the pooled proportion.
x1 <- .12*(181)
y1 <- .03*(251)
n1 <- 181
n2 <- 251
phatM <- x1/n1
phatN <- y1/n2
numbike <- phatM - phatN
(phat <- (x1+y1)/(n1+n2))
## [1] 0.06770833
So 0.0677083 is the \(\hat{p}\), 0.12 is the proportion of Midwesterners, and 0.03 is the proportion of Northeasterners.
varbike <- (phat) * (1-phat) * ((1/n1)+(1/n2))
sdbike <- sqrt(varbike)
teststatbike <- (numbike)/(sdbike)
(pval7 <- pnorm(teststatbike, lower.tail = FALSE))
## [1] 0.0001196262
The denominator of our test stat is 0.0244998. Our test stat is 3.6734981 and the pvalue is quite small, so we reject the null hypothesis in favor of the alternative. The proportion of Midwesterners who know how to bike is greater than the proportion of Northeasterners.
Null: smoking and divorce are unrelated. (Or: smoking and divorce are independent.)
Alternative: smoking and divorce are associated/related.
The test stat is \[\sum \dfrac{(obs - exp)^2}{exp} \sim \chi^2_1\].
dat <- matrix(c(238,247,374,810), nrow = 2, byrow = T)
mytest <- chisq.test(dat)
exp8 <- mytest$expected
numerator8 <- (dat-exp8)^2
temp8 <- numerator8/exp8
teststatsmoke <- sum(temp8)
They can report a test stat of 44.5422029 or 45.2919703 (or wherever in the 44 to 46 range). They can report either of the following p-values.
(pval8 <- pchisq(teststatsmoke, lower.tail = FALSE, df=1))
## [1] 1.697436e-11
mytest$p.value
## [1] 2.489329e-11
Either way, the p-value is less than \(\alpha = .05\) so we reject the null in favor of the alternative. There’s an association between smoking and divorce.
library(Lock5withR)
data(SleepStudy)
attach(SleepStudy)
The null hypothesis is that the means are equal. If they’re testing depression status, the null is \(\mu_{moderate} = \mu_{normal} = \mu_{severe}\). If they’re testing alcohol use \(\mu_{abstain} = \mu_{heavy} = \mu_{light} = \mu_{moderate}\).
The alternative is that at least one of the means differs.
For this problem, if they’re missing the distribution (F with df listed in the next sections) or the test statistic (MST/MSE), we’ll let it slide. The hypotheses, p-value, and conclusion are still essential for mastery.
mod <- aov(AverageSleep ~DepressionStatus, data = SleepStudy)
anova(mod)
## Analysis of Variance Table
##
## Response: AverageSleep
## Df Sum Sq Mean Sq F value Pr(>F)
## DepressionStatus 2 0.104 0.05190 0.0553 0.9462
## Residuals 250 234.487 0.93795
The test stat has an F distribution with 250 and 2 df. (The degrees of freedom are shown in the table as 250 and 2.) The p-value is large (.946) so we do not reject the null. There’s no evidence that the mean amount of sleep students get differs depending on their depression status.
mod2 <- aov(AverageSleep ~AlcoholUse, data = SleepStudy)
anova(mod2)
## Analysis of Variance Table
##
## Response: AverageSleep
## Df Sum Sq Mean Sq F value Pr(>F)
## AlcoholUse 3 2.166 0.72197 0.7735 0.5098
## Residuals 249 232.425 0.93343
The test stat has an F distribution with 249 and 3 df. The p-value is large (.5098) so we do not reject the null. There’s no evidence that the mean amount of sleep students get differs depending on their alcohol consumption.