Q2(a).

t5q2 <- read.table("C:/Users/Wei Hao/Desktop/ST2137/Tutorials/Data/stress.txt", header=T)
names(t5q2) <- c("stress", "time")
attach(t5q2)
table1 <- table(stress, time)
chisq.test(table1)
## 
##  Pearson's Chi-squared test
## 
## data:  table1
## X-squared = 9.8311, df = 4, p-value = 0.04337

Since p-value \(= 0.04337 < 0.05\), we reject \(H_0\) at the \(5\)% level of significance.

Q2(b).

stressgrp <- ifelse(stress=="L", "(3) Low", ifelse(stress=="M", "(2) Moderate", "(1) High"))
timename <- c("(1) Under 15 min", "(2) Between 15 to 45 min", "(3) Over 45 min")
stressname <- c("(1) High", "(2) Moderate", "(3) Low")
barplot(matrix(table(stressgrp, time), 3, 3, byrow = F, dimnames=list(stressname, timename)), beside=TRUE, col=c(1:3))
legend(1,28,stressname,fill=c(1:3))