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
)
Create a histogram of test_score from
student_data.
hist(student_data$test_score,
main = "Distribution of Test Scores",
xlab = "Score",
ylab = "Frequency",
col = "steelblue",
border = "white")
Create a scatter plot showing the relationship between
hours_sleep (x-axis) and test_score
(y-axis).
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)
Create a box plot comparing reaction_time across the two
levels of condition.
boxplot(reaction_time ~ condition, data = student_data,
main = "Reaction Time by Condition",
xlab = "Condition",
ylab = "Reaction Time",
col = c("steelblue", "coral"))
Create a bar chart showing the mean reaction_time for
each condition.
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))
Create a bar chart with error bars showing the mean
reaction_time for each condition.
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