Exercise-1

# read data
dta <- read.table("stateAnxiety.txt", header = T)
# examine data
head(dta)
##   f1 f2 f3 f4 f5 m1 m2 m3 m4 m5
## 1 13 17 18 20 24  6 14 22 20 24
## 2 26 31 33 38 42  4 11 14 12 23
## 3 13 17 24 29 32 17 25 26 29 38
## 4 22 24 26 27 29 19 22 26 30 34
## 5 18 19 19 22 30 12 21 21 23 24
## 6 32 31 30 31 32 11 16 20 19 22
# subset data by gender
dtaf <- dta[,1:5]
dtam <- dta[,6:10]

# transform to long form and create new variable (female)
dtaf_long <- stack(dtaf)
dtaf_long$ind <- substr(dtaf_long$ind, 2, 2)
dtaf_long$gender <- "Female"
dtaf_long$id <- rep(1:50, 5)
colnames(dtaf_long) <- c("score", "weeks", "gender", "id")

# transform to long form and create new variable (male)
dtam_long <- stack(dtam)
dtam_long$ind <- substr(dtam_long$ind, 2, 2)
dtam_long$gender <- "Male"
dtam_long$id <- rep(51:100, 5)
colnames(dtam_long) <- c("score", "weeks", "gender", "id")

# combine two data objects
dta <- rbind(dtaf_long, dtam_long)

gender difference

boxplot

dta$weeks <- factor(dta$weeks, levels = rev(unique(dta$weeks)))
library(ggplot2)
ggplot(data = dta, mapping = aes(weeks, score)) + 
  geom_boxplot(aes(color = gender)) + 
  labs(x = "weeks before exam",
       y = "anxiety score") +
  theme_bw() +
  theme(legend.direction = "horizontal",
        legend.position = c(0.50,0.95),
        legend.box.background = element_rect(color = "black"))

mean and error plot

library(dplyr)
dta %>% group_by(gender, weeks) %>% 
  summarise(mean = mean(score),
            se = sd(score) / sqrt(n())) %>% 
  ggplot(mapping = aes(x = weeks, y = mean)) +
   geom_point(aes(shape = gender, color = gender), size = 3) +
   geom_errorbar(aes(ymin = mean -se,
                    ymax = mean + se),
                width=.1, size=.2) +
  labs(x = "weeks before exam",
       y = "mean axiety score") +
  theme_bw()

Generally, females had higher anxiety score than males. Males’ anxiety score droped much faster than females.

individual difference

ggplot(data = dta, mapping = aes(x = weeks, y = score)) + 
  geom_point()  +
  facet_wrap(.~id) +
  geom_line(aes(group = id, color = gender)) +
  labs(x = "weeks before exam",
       y = "anxiety score")

ggplot(data = dta, mapping = aes(x = weeks, y = score)) +
  geom_point() +
  # facet_grid(.~gender) +
  geom_point(alpha = 0.5) +
  geom_line(aes(group = id), alpha = 0.8, color = "gray10") +
    labs(x = "weeks before exam",
       y = "anxiety score") +
  theme_bw()

ggplot(data = dta, mapping = aes(x = weeks, y = score)) +
  geom_point() +
    facet_grid(.~gender) +
  geom_point(alpha = 0.5) +
  geom_line(aes(group = id), alpha = 0.8, color = "gray10") +
    labs(x = "weeks before exam",
       y = "anxiety score") +
  theme_bw()

If there was no individual difference, the lines should have been parallel other than crossing each other.

Exercise-2

Exercise-3

Exercise-4

Exercise-5