First, let’s lay out the general steps for hypothesis testing:
Determine your research question in English or whatever language you like.
State your statistical hypotheses and your level of significance. (Absolutely choose your significance level before peeking at the data!)
Determine the form of your test statistic and its sampling distribution under the null hypothesis.
Calculate your test statistic.
Calculate your p-value.
Compare your p-value to your significance level, and determine whether you reject \(H_0\) in favor of \(H_A\).
Interpret in the context of the study.
Perhaps you have 4 data points (1,4,6,3). Enter them and call them vec1.
vec1 <- c(1,4,6,3)
Perhaps the first two measurements are on Freshmen and the last two are on Seniors.
group <- c("F", "F", "S", "S")
group <- factor(group)
Now check your work to make sure the factor command worked.
summary(group)
## F S
## 2 2
# install.packages("alr4")
library(alr4)
## Loading required package: car
## Loading required package: carData
## Loading required package: effects
## Registered S3 methods overwritten by 'lme4':
## method from
## cooks.distance.influence.merMod car
## influence.merMod car
## dfbeta.influence.merMod car
## dfbetas.influence.merMod car
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
First, we’ll use the Australian institute of sport data.
data(ais)
# ?ais # read about the variables
head(ais)
## Sex Ht Wt LBM RCC WCC Hc Hg Ferr BMI SSF Bfat Label
## 1 1 195.9 78.9 63.32 3.96 7.5 37.5 12.3 60 20.56 109.1 19.75 f-b_ball
## 2 1 189.7 74.4 58.55 4.41 8.3 38.2 12.7 68 20.67 102.8 21.30 f-b_ball
## 3 1 177.8 69.1 55.36 4.14 5.0 36.4 11.6 21 21.86 104.6 19.88 f-b_ball
## 4 1 185.0 74.9 57.18 4.11 5.3 37.3 12.6 69 21.88 126.4 23.66 f-b_ball
## 5 1 184.6 64.6 53.20 4.45 6.8 41.5 14.0 29 18.96 80.3 17.64 f-b_ball
## 6 1 174.0 63.7 53.77 4.10 4.4 37.4 12.5 42 21.04 75.2 15.58 f-b_ball
## Sport
## 1 b_ball
## 2 b_ball
## 3 b_ball
## 4 b_ball
## 5 b_ball
## 6 b_ball
summary(ais)
## Sex Ht Wt LBM
## Min. :0.000 Min. :148.9 Min. : 37.80 Min. : 34.36
## 1st Qu.:0.000 1st Qu.:174.0 1st Qu.: 66.53 1st Qu.: 54.67
## Median :0.000 Median :179.7 Median : 74.40 Median : 63.03
## Mean :0.495 Mean :180.1 Mean : 75.01 Mean : 64.87
## 3rd Qu.:1.000 3rd Qu.:186.2 3rd Qu.: 84.12 3rd Qu.: 74.75
## Max. :1.000 Max. :209.4 Max. :123.20 Max. :106.00
##
## RCC WCC Hc Hg
## Min. :3.800 Min. : 3.300 Min. :35.90 Min. :11.60
## 1st Qu.:4.372 1st Qu.: 5.900 1st Qu.:40.60 1st Qu.:13.50
## Median :4.755 Median : 6.850 Median :43.50 Median :14.70
## Mean :4.719 Mean : 7.109 Mean :43.09 Mean :14.57
## 3rd Qu.:5.030 3rd Qu.: 8.275 3rd Qu.:45.58 3rd Qu.:15.57
## Max. :6.720 Max. :14.300 Max. :59.70 Max. :19.20
##
## Ferr BMI SSF Bfat
## Min. : 8.00 Min. :16.75 Min. : 28.00 Min. : 5.630
## 1st Qu.: 41.25 1st Qu.:21.08 1st Qu.: 43.85 1st Qu.: 8.545
## Median : 65.50 Median :22.72 Median : 58.60 Median :11.650
## Mean : 76.88 Mean :22.96 Mean : 69.02 Mean :13.507
## 3rd Qu.: 97.00 3rd Qu.:24.46 3rd Qu.: 90.35 3rd Qu.:18.080
## Max. :234.00 Max. :34.42 Max. :200.80 Max. :35.520
##
## Label Sport
## f-netball:23 row :37
## f-row :22 t_400m :29
## m-t_400m :18 b_ball :25
## m-w_polo :17 netball:23
## m-row :15 swim :22
## f-b_ball :13 field :19
## (Other) :94 (Other):47
attach(ais)
Do Australian athletes’ have a mean body fat greater than 13 percent? We’ll use this as our research question. Our hypotheses are then:
\[\begin{align} H_0: \mu = 13 \\ H_A: \mu > 13 \end{align}\]
Next, we need to state our test stat:
\[\begin{align} \dfrac{\bar{x}- \mu_0}{s/\sqrt{n}} \end{align}\] This test stat follows a T distribution with \(n-1\) df under the null hypothesis.
mean(Bfat)
## [1] 13.50743
sd(Bfat)
## [1] 6.189826
(en <- length(Bfat))
## [1] 202
Next, let’s calculate the test stat for our data.
numer <- mean(Bfat) - 13
denom <- sd(Bfat)/sqrt(en)
(teststat <- numer/denom)
## [1] 1.165118
Next, we calculate the p-value. To calculate probabilities using a t distribution, use the pt( ) function: provide the test statistic, the degrees of freedom, and tell R whether you want the lower tail. Since we are testing whether the mean body feet EXCEEDS 13 percent, we do NOT want the lower tail; we want the upper tail.
pt(teststat, df = en - 1, lower.tail = FALSE)
## [1] 0.1226761
The pvalue is .1227, so we do not reject the null hypothesis in favor of the alternative. We don’t have evidence that athletes’ mean body fat is higher than 13%. In other words, athletes’ mean body fat is not significantly higher than 13%.
Do Australian athletes’ have a mean body fat greater than 13 percent? To answer this question, we use the following command. We provide the variable, the value of the mean under the null hypothesis, and the direction of the alternative hypothesis.
t.test(Bfat, mu = 13, alternative = "greater")
##
## One Sample t-test
##
## data: Bfat
## t = 1.1651, df = 201, p-value = 0.1227
## alternative hypothesis: true mean is greater than 13
## 95 percent confidence interval:
## 12.78775 Inf
## sample estimates:
## mean of x
## 13.50743
Do Australian athletes’ have a mean body fat less than 15 percent?
t.test(Bfat, mu = 15, alternative = "less")
##
## One Sample t-test
##
## data: Bfat
## t = -3.4272, df = 201, p-value = 0.0003698
## alternative hypothesis: true mean is less than 15
## 95 percent confidence interval:
## -Inf 14.2271
## sample estimates:
## mean of x
## 13.50743
Do Australian athletes’ have a mean body fat of 15 percent?
t.test(Bfat, mu = 15, alternative = "two.sided")
##
## One Sample t-test
##
## data: Bfat
## t = -3.4272, df = 201, p-value = 0.0007397
## alternative hypothesis: true mean is not equal to 15
## 95 percent confidence interval:
## 12.64866 14.36619
## sample estimates:
## mean of x
## 13.50743
Start by creating the categorical variable.
Female <- factor(Sex) # create categorical variable
Are male Aussie athletes leaner, on average, than women?
First, get the mean for each group.
wmean <- mean(Bfat[Sex == 1]) # 1 is women
mmean <- mean(Bfat[Sex == 0]) # 0 is men
Get the variance and sample size for each group.
mvar <- var(Bfat[Sex == 0])
wvar <- var(Bfat[Sex == 1])
mn <- length(Bfat[Sex == 0])
wn <- length(Bfat[Sex == 1])
Check your work: do the sample sizes agree with the ?ais description?
wn
## [1] 100
mn
## [1] 102
Do the sample means agree with an alternative calculation?
lm(Bfat ~ 0 + Sex)
##
## Call:
## lm(formula = Bfat ~ 0 + Sex)
##
## Coefficients:
## Sex
## 17.85
Calculate the test stat:
num <- wmean - mmean
inside <- wvar/wn +mvar/mn
denom <- sqrt(inside)
teststat <- num/denom
mydf <- min(wn - 1, mn - 1)
Calculate the p-value:
pt(teststat, df = mydf, lower.tail = FALSE )
## [1] 8.646087e-25
Now let’s think about another test. If we instead wanted to test whether the mean body fat of male and female Aussie athletes differed, the p-value is twice the smaller tail:
2* pt(teststat, df = mydf, lower.tail = FALSE )
## [1] 1.729217e-24
Next, let’s use t.test() to compare two means. Does the mean of men’s lean body mass differ from the women’s mean?
t.test(LBM ~ Female)
##
## Welch Two Sample t-test
##
## data: LBM by Female
## t = 16.482, df = 181.07, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 17.39622 22.12771
## sample estimates:
## mean in group 0 mean in group 1
## 74.65686 54.89490
We can see that the default is a two-sided test (because the output above is identical to the output below).
t.test(LBM ~ Female, alternative = "two.sided")
##
## Welch Two Sample t-test
##
## data: LBM by Female
## t = 16.482, df = 181.07, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## 17.39622 22.12771
## sample estimates:
## mean in group 0 mean in group 1
## 74.65686 54.89490
Here is how to tweak the test if the question is “Are men leaner, on average, than women?”
t.test(LBM ~ Female, alternative = "greater")
##
## Welch Two Sample t-test
##
## data: LBM by Female
## t = 16.482, df = 181.07, p-value < 2.2e-16
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## 17.77969 Inf
## sample estimates:
## mean in group 0 mean in group 1
## 74.65686 54.89490
If our question is “Are women leaner, on average, than men?” then we use:
t.test(LBM ~ Female, alternative = "less")
##
## Welch Two Sample t-test
##
## data: LBM by Female
## t = 16.482, df = 181.07, p-value = 1
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
## -Inf 21.74423
## sample estimates:
## mean in group 0 mean in group 1
## 74.65686 54.89490
NOTE: t.test and your “by hand” calculations will give slightly different p-values because t.test calculates df in a more sophisticated manner.