## 3.4 Hypothesis tests

Now, let’s do some basic hypothesis tests. First, let’s conduct a two-sample t-test to see if there is a significant difference between the ages of pirates who do wear a headband, and those who do not:

# Age by headband t-test
t.test(formula = age ~ headband,
data = pirates,
alternative = 'two.sided')
##
##  Welch Two Sample t-test
##
## data:  age by headband
## t = 0.4, df = 100, p-value = 0.7
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -1.0  1.5
## sample estimates:
##  mean in group no mean in group yes
##                28                27

With a p-value of 0.7259, we don’t have sufficient evidence say there is a difference in the men age of pirates who wear headbands and those that do not.

Next, let’s test if there a significant correlation between a pirate’s height and weight using the cor.test() function:

cor.test(formula = ~ height + weight,
data = pirates)
##
##  Pearson's product-moment correlation
##
## data:  height and weight
## t = 80, df = 1000, p-value <2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.92 0.94
## sample estimates:
##  cor
## 0.93

We got a p-value of p < 2.2e-16, that’s scientific notation for p < .00000000000000016 – which is pretty much 0. Thus, we’d conclude that there is a significant (positive) relationship between a pirate’s height and weight.

Now, let’s do an ANOVA testing if there is a difference between the number of tattoos pirates have based on their favorite sword

# Create tattoos model
tat.sword.lm <- lm(formula = tattoos ~ sword.type,
data = pirates)

# Get ANOVA table
anova(tat.sword.lm)
## Analysis of Variance Table
##
## Response: tattoos
##             Df Sum Sq Mean Sq F value Pr(>F)
## sword.type   3   1588     529    54.1 <2e-16 ***
## Residuals  996   9743      10
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Sure enough, we see another very small p-value of p < 2.2e-16, suggesting that the number of tattoos pirate’s have are different based on their favorite sword.