#Loading necessary libraries library(tidyverse) library(broom) install.packages(‘Matrix’) library(Matrix) library(lme4) library(dplyr) library(ggplot2) install.packages(“patchwork”) library(patchwork)

#TASK 1 #1a # LOADING THE DATA data(“sleepstudy”) ?sleepstudy View(sleepstudy) str(sleepstudy) head(sleepstudy) # checking the first 6 rows of the dataset tail(sleepstudy) #checking the last 6 rows

#Removing day 0 and day 1 from the dataset and setting the label to start from day 0 instead of 2 new_sleep <- sleepstudy %>% filter(!(Days %in% c(0, 1))) %>% mutate(Days = Days - 2) View(new_sleep)

#1b # DATA EXPLORATION head(new_sleep) # checking the first 6 rows of the dataset #Reaction Days Subject #1 250.8006 0 308 #2 321.4398 1 308 #3 356.8519 2 308 #4 414.6901 3 308 #5 382.2038 4 308 #6 290.1486 5 308 tail(new_sleep) # checing the last 6 rows of the dataset #Reaction Days Subject #139 287.1726 2 372 #140 329.6076 3 372 #141 334.4818 4 372 #142 343.2199 5 372 #143 369.1417 6 372 #144 364.1236 7 372 str(new_sleep) # checking the structure of the dataset which is a dataframe and cnsiste 144 observation and 3 variables colnames(new_sleep) # the cdata set has 3 columns which is the reaction column, day column and subject column summary(new_sleep) #checking for missing values in the dataset which(is.na(new_sleep)) #checking data call class(new_sleep) # the dataset is a dataframe

#1c #VISUALIZING #Using Scatter plot plot1 <-new_sleep %>% ggplot(aes(x = Days, y = Reaction)) + geom_point() + labs(title = “Reaction Time Over Days”, x = “Days of Sleep Deprivation”, y = “Reaction Time”) plot1 #saving my plots ggsave(“plot1.png”, plot = plot1, width = 8, height = 6, dpi = 300) ggsave(“plot1.pdf”, plot = plot1, width = 8, height = 6, dpi = 300)

Using Boxplot

plot2 <- new_sleep %>% ggplot(aes(y = Reaction)) + geom_boxplot(fill = “lightblue”) + labs(title = “Boxplot of Reaction Time”, y = “Reaction Time”) plot2 #saving my plots ggsave(“plot2.png”, plot = plot2, width = 8, height = 6, dpi = 300) ggsave(“plot2.pdf”, plot = plot2, width = 8, height = 6, dpi = 300)

Density plot

This density plot shows the reaction time by subject

plot3 <- new_sleep %>% ggplot(aes(x = Reaction, fill = as.factor(Subject))) + geom_density(alpha = 0.7) + labs(title = “Density Plot of Reaction Time by Subject”, x = “Reaction Time”) + theme_minimal() #saving my plots ggsave(“plot3.png”, plot = plot3, width = 8, height = 6, dpi = 300) ggsave(“plot3.pdf”, plot = plot3, width = 8, height = 6, dpi = 300)

This density plot shows the reaction time by days

plot4 <- new_sleep %>% ggplot(aes(x = Reaction, fill = as.factor(Days))) + geom_density(alpha = 0.7) + labs(title = “Density Plot of Reaction Time by Days”, x = “Reaction Time”) + theme_minimal() #saving my plots ggsave(“plot4 .png”, plot = plot4, width = 8, height = 6, dpi = 300) ggsave(“plot4 .pdf”, plot = plot4 , width = 8, height = 6, dpi = 300)

#TASKS 02 #DESCRIPTIVE STATISTICS #computing the summary statistics for Reaction and Days which is the key variables summary(new_sleep) summary(new_sleep\(Reaction) summary(new_sleep\)Days) # Create a visualization to better understand the distribution of reaction times over different days

Using histogram to better understand the distribution of reaction time over different days

Histogram

plot5 <- new_sleep %>% ggplot(aes(x = Reaction)) + geom_histogram(binwidth = 10, fill = “lightblue”, color = “black”) + labs(title = “Histogram of Reaction Time”, x = “Reaction Time”) + theme_minimal() #saving my plots ggsave(“plot5 .png”, plot = plot5, width = 8, height = 6, dpi = 300) ggsave(“plot5 .pdf”, plot = plot5, width = 8, height = 6, dpi = 300)

#Boxplot plot6 <- ggplot(new_sleep, aes(x = as.factor(Days), y = Reaction)) + geom_boxplot(fill = “red”, color = “black”) + labs(title = “Boxplot of Reaction Times by Days”, x = “Days of Sleep Deprivation”, y = “Reaction Time”) + theme_minimal()

#saving my plots ggsave(“plot6 .png”, plot = plot6, width = 8, height = 6, dpi = 300) ggsave(“plot6 .pdf”, plot = plot6, width = 8, height = 6, dpi = 300)

plot7 <- ggplot(new_sleep, aes(x = as.factor(Subject), y = Reaction)) + geom_boxplot(fill = “blue”, color = “black”) + labs(title = “Boxplot of Reaction Times by Days”, x = “Subject”, y = “Reaction Time”) + theme_minimal() #saving my plots plot7 ggsave(“plot7 .png”, plot = plot7, width = 8, height = 6, dpi = 300) ggsave(“plot7 .pdf”, plot = plot7, width = 8, height = 6, dpi = 300)

density plot

plot8 <- new_sleep %>% ggplot(aes(x = Reaction, fill = as.factor(Days))) + geom_density(alpha = 0.5) + labs(title = “Density Plot of Reaction Time by Days”, x = “Reaction Time”) + theme_minimal() #saving my plots plot8 ggsave(“plot8 .png”, plot = plot8, width = 8, height = 6, dpi = 300) ggsave(“plot8 .pdf”, plot = plot8, width = 8, height = 6, dpi = 300)

#TASK 3 #3A Fitting an adequate model which is a mixed effect model because our independence assumption was bridgesd sleep_mdl <- lmer(Reaction ~ Days + (Days|Subject), data = new_sleep, REML = FALSE) summary(sleep_mdl) sleep_mdl

sleep_mdl2 <- lmer(Reaction ~ Days + (Days||Subject) + (0+Days|Subject), data = new_sleep, REML = FALSE) summary(sleep_mdl2) sleep_mdl2

anova(sleep_mdl, sleep_mdl2)

#3Bresult interpretation is inside the report

#3C RESIDUAL ANALYSIS res <- residuals(sleep_mdl)

i will set the graphical parameters to generate the plot matrix

par(mfrow = c(1, 3))

visualizing using an histogram

hist(res)

visualizing using Q-Q plot:

qqnorm(res) qqline(res)

plotting the residual plot

plot(fitted(sleep_mdl), res)