Setup

Here is the data you will use for these exercises.

set.seed(123)

# Student data
hours_sleep <- runif(40, min = 4, max = 10)
test_score <- 40 + 5 * hours_sleep + rnorm(40, mean = 0, sd = 6)

# Condition data
condition <- rep(c("Placebo", "Drug"), each = 20)
reaction_time <- c(rnorm(20, mean = 450, sd = 40), rnorm(20, mean = 410, sd = 40))

student_data <- data.frame(
  hours_sleep = hours_sleep,
  test_score = test_score,
  condition = condition,
  reaction_time = reaction_time
)

Exercise 1: Histogram

Create a histogram of test_score from student_data.

  • Set the main title to “Distribution of Test Scores”
  • Label the x-axis “Score”
  • Use any color you like for the bars
hist(student_data$test_score,
     main = "Distribution of Test Scores",
     xlab = "Score",
     ylab = "Frequency",
     col = "steelblue",
     border = "white")


Exercise 2: Scatter Plot

Create a scatter plot showing the relationship between hours_sleep (x-axis) and test_score (y-axis).

  • Set the main title to “Sleep vs. Test Score”
  • Label the axes appropriately
  • Add a regression line using abline() and lm()
plot(student_data$hours_sleep, student_data$test_score,
     main = "Sleep Hours vs. Test Score",
     xlab = "Sleep Hours",
     ylab = "Test Score",
     pch = 19,
     col = "steelblue")

# Add regression line
abline(lm(test_score ~ hours_sleep, data = student_data), col = "red", lwd = 2)


Exercise 3: Box Plot

Create a box plot comparing reaction_time across the two levels of condition.

  • Set the main title to “Reaction Time by Condition”
  • Label the axes appropriately
  • Use different colors for each group
boxplot(reaction_time ~ condition, data = student_data,
        main = "Reaction Time by Condition",
        xlab = "Condition",
        ylab = "Reaction Time",
        col = c("steelblue", "coral"))


Exercise 4: Bar Chart

Create a bar chart showing the mean reaction_time for each condition.

  • First calculate the means using tapply()
  • Set the main title to “Mean Reaction Time by Condition”
  • Label the axes appropriately
condition_means <- tapply(student_data$reaction_time, student_data$condition, mean)

barplot(condition_means, 
        main = "Mean Reaction Time by Condition",
        xlab = "Condition",
        ylab = "Reaction Time",
        col = c("steelblue", "coral"),
        ylim = c(0, 500)) 


Exercise 5: Error Bars

Create a bar chart with error bars showing the mean reaction_time for each condition.

  • Calculate means, standard deviations, sample sizes, and standard errors
  • Create the bar chart
  • Add error bars using arrows()
means <- tapply(student_data$reaction_time, student_data$condition, mean)
st_devs <- tapply(student_data$reaction_time, student_data$condition, sd)
n_size <- tapply(student_data$reaction_time, student_data$condition, length)
std_err <- st_devs / sqrt(n_size)

bp <- barplot(means, 
              main = "Mean Reaction Time by Condition",
              xlab = "Condition", 
              ylab = "Reaction Time",
              col = c("steelblue", "coral"),
              ylim = c(0, 550)) 

arrows(x0 = bp, y0 = means - std_err, 
       x1 = bp, y1 = means + std_err, 
       angle = 90, code = 3, length = 0.1)


End of Exercises