########################
# R Course WS 2019-20
########################
# Solutions to exercise sheet 9 - Basic statistics in R
# Updated by NoĆ©mie 17/02/2020
### Exercise 1
# Read data
data.ccrt <- read.table("data/ccrt.txt", header = TRUE)
# Mean and standard deviation
# Total
mean(data.ccrt$ccrt)
sd(data.ccrt$ccrt)
# Population by population
tapply(data.ccrt$ccrt, data.ccrt$population, mean)
tapply(data.ccrt$ccrt, data.ccrt$population, sd)
# t test, not paired
# no knowledge about variances so I will not assume they are equal
t.test(data.ccrt[data.ccrt$population == "KATH",]$ccrt,
data.ccrt[data.ccrt$population == "BKK",]$ccrt,
paired = FALSE,
var.equal = FALSE)
# Answer:
# We can reject the null hypothesis that the means of CCRT in the two
# populations are equal.
### Exercise 2
## Heartbeats
# Open the data
heartbeats <- read.table("data/heartbeats.txt")
# We want to test for a difference in mean of weight increase for the two groups.
# This time the groups are composed of different individuals so we apply an
# unpaired t test.
t.test(heartbeats$wghtincr[heartbeats$treatment == 0],
heartbeats$wghtincr[heartbeats$treatment == 1])
# Conclusion: the difference in means is significant.
# By weight class
t.test(heartbeats$wghtincr[heartbeats$treatment == 0 & heartbeats$wghtcls == 1],
heartbeats$wghtincr[heartbeats$treatment == 1 & heartbeats$wghtcls == 1])
t.test(heartbeats$wghtincr[heartbeats$treatment == 0 & heartbeats$wghtcls == 2],
heartbeats$wghtincr[heartbeats$treatment == 1 & heartbeats$wghtcls == 2])
t.test(heartbeats$wghtincr[heartbeats$treatment == 0 & heartbeats$wghtcls == 3],
heartbeats$wghtincr[heartbeats$treatment==1 & heartbeats$wghtcls==3])
# The difference in means is significant for the three classes.
### Exercise 3
### Mite
# Open the data
mite <- read.table("data/mite.txt", header = TRUE)
# Plot
boxplot(mite$number~mite$first.encounter,
col=c("white", "lightgrey"))
# t test unpaired
t.test(mite$number[mite$first.encounter == "yes"],
mite$number[mite$first.encounter == "no"])
# Conclusion: the difference in means is significant, p.value=0.0028.