Example 1

  1. In one sample t-test, it is assumed that the data ~ N(\(\mu\), \(\sigma\)) and we wish to test that the null hypothesis \(H_0:\mu=\mu_0\)

Here is the daily energy intake for 11 women in KJ: 5260, 5470, 5640, 6180, 8390, 6515, 6805, 7515, 7515, 8230, 8770. Create a vector from this data.

daily.intake <-c(5260, 5470, 5640, 6180, 8390, 6515, 6805, 7515, 7515, 8230, 8770)
  1. Find mean, standard deviation, and quartiles of the above data.

mean(daily.intake) sd(daily.intake) quantile(daily.intake) ```

  1. Given that \(\mu_0=7725\)KJ, do the following t-test with H_0:mu=7725
t.test(daily.intake, mu=7725)
## 
##  One Sample t-test
## 
## data:  daily.intake
## t = -2.1222, df = 10, p-value = 0.05981
## alternative hypothesis: true mean is not equal to 7725
## 95 percent confidence interval:
##  6106.477 7764.432
## sample estimates:
## mean of x 
##  6935.455
  1. Result and Conclusion:
  1. What is the t score?
t.test(daily.intake, mu=7725)
## 
##  One Sample t-test
## 
## data:  daily.intake
## t = -2.1222, df = 10, p-value = 0.05981
## alternative hypothesis: true mean is not equal to 7725
## 95 percent confidence interval:
##  6106.477 7764.432
## sample estimates:
## mean of x 
##  6935.455
  1. What do you interpret from p value?

p<0.05, which means that the data deviates significantly from the mean, mu = 7725. Therefore, fail to reject \(H_0:\mu=\mu_0\)

Exercise 2.

Body piercing data for American: 3,5,2,1,4,4,6,3,5,4 European: 6,5,7,7,6,3,4,6,5,4

american.bp <- c(3,5,2,1,4,4,6,3,5,4)
european.bp <- c(6,5,7,7,6,3,4,6,5,4)
length(american.bp)
## [1] 10
length(european.bp)
## [1] 10

Create the data frane

bp.survey <-data.frame("bp"=c(american.bp, european.bp), "group"=rep(c("American", "European"), each = 10), stringsAsFactors = FALSE)
library(yarrr)
yarrr::pirateplot(bp~group, data = bp.survey, ylab = "Number of Body Piercing", xlab = "Group", main = "Body Piercing Survey", theme = 2, point.o = 0.8, cap.beans = TRUE)

summary(american.bp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    1.00    3.00    4.00    3.70    4.75    6.00
summary(european.bp)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    3.00    4.25    5.50    5.30    6.00    7.00

Conduct a two-sided t-test comparing the vectors American and European and save the results in an object called bp.test

bp.test <-t.test(x=american.bp, y=european.bp, alternative = "two.sided")
bp.test
## 
##  Welch Two Sample t-test
## 
## data:  american.bp and european.bp
## t = -2.5228, df = 17.783, p-value = 0.0214
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -2.9335927 -0.2664073
## sample estimates:
## mean of x mean of y 
##       3.7       5.3

Independent Exercise

Here are the data for old and young ages:

Old: 45,38,52,48,25,39,51,46,55,46 Young: 34,22,15,27,37,41,24,19,26,36

  1. Create a data frame with the vectors old and young.
old <-c(45,38,52,48,25,39,51,46,55,46)
young <-c(34,22,15,27,37,41,24,19,26,36)
ls.survey <-data.frame("ls"=c(old, young), "group"=rep(c("Old", "Young"), each = 10), stringsAsFactors = FALSE)
  1. Do the two sample-test
ls.test <-t.test(x=old, y=young, alternative= "two.sided")
ls.test
## 
##  Welch Two Sample t-test
## 
## data:  old and young
## t = 4.2575, df = 17.995, p-value = 0.0004739
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   8.307131 24.492869
## sample estimates:
## mean of x mean of y 
##      44.5      28.1
  1. Graph with cap.beans the two arrays: xlab: old and young ylab: age Title: Life Satisfaction Survey
yarrr::pirateplot(ls~group, data = ls.survey, ylab = "Age", xlab = "Old and Young", main = "Life Satisfaction Survey", theme = 2, point.o = 0.8, cap.beans = TRUE)