First Load Libraries
library(readxl)
## Warning: package 'readxl' was built under R version 4.3.3
library(ggplot2)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(plotly)
## Warning: package 'plotly' was built under R version 4.3.1
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplotify)
## Warning: package 'ggplotify' was built under R version 4.3.2
library(rempsyc)
## Warning: package 'rempsyc' was built under R version 4.3.3
## Suggested APA citation: Thériault, R. (2023). rempsyc: Convenience functions for psychology.
## Journal of Open Source Software, 8(87), 5466. https://doi.org/10.21105/joss.05466
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.3.1
##
## Attaching package: 'cowplot'
## The following object is masked from 'package:lubridate':
##
## stamp
Loading dataset
final_df = read_excel("Z:\\30_Oscar_Ortiz\\MENTOR\\AL\\final_analysis\\FINAL_ANALYSIS\\final_merged_dataset.xlsx") # change for your use
Lets plot how people did before and after the sleep journaling to see if there is consistency between their responses
p= ggplot(final_df, aes(x=success_rate, y=success_rate_2)) +
geom_point()+
geom_smooth(method=lm, se = FALSE)+
labs(y= "nback_success_rate_1", x = "nback_success_rate_1")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
p = ggplot(final_df, aes(x=percent_match_ordered_mean, y=percent_match_ordered_mean_2)) +
geom_point()+
geom_smooth(method=lm, se = FALSE)+
labs(y= "percent_match_ordered_mean_1", x = "percent_match_ordered_mean_2")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
p = ggplot(final_df, aes(x=matching_words_count_mean, y=matching_words_count_mean_2)) +
geom_point()+
geom_smooth(method=lm, se = FALSE)+
labs(y= "matching_words_count_mean", x = "matching_words_count_mean_2")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
Looks like there is good reliability in the post test.
Plotting performance before and after
df = as.data.frame(final_df |>
select(contains(c("subject","success")))) |>
pivot_longer(cols = c(success_rate, success_rate_2), names_to = "Timepoint", values_to = "Value") |>
mutate(Timepoint = case_when( # change the y and n responses to 1 and zero
Timepoint == "success_rate" ~ "Pre",
Timepoint == "success_rate_2" ~ "Post",
.default ="text")) |>
mutate(Timepoint = factor(Timepoint,levels=c("Pre","Post")))|>
group_by(Timepoint)|>
mutate(paired = seq(1:length(Timepoint)))
p = ggplot(df, aes(x = Timepoint)) +
geom_line(aes(group = paired,y =Value),
linewidth=1,alpha = 0.7,
position = position_jitter(width = 0.1, seed = 3922)) +
geom_point(aes(color=Timepoint,y=Value),size = 6,position = position_jitter(width = 0.1, seed = 3922)) +
labs(y= "% of Correct Letters", x = "Pre or Post Sleep Journaling")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
ggplotly(p)
df = as.data.frame(final_df |>
select(contains(c("subject","percent_match_ordered_mean")))) |>
pivot_longer(cols = c(percent_match_ordered_mean, percent_match_ordered_mean_2), names_to = "Timepoint", values_to = "Value") |>
mutate(Timepoint = case_when( # change the y and n responses to 1 and zero
Timepoint == "percent_match_ordered_mean" ~ "Pre",
Timepoint == "percent_match_ordered_mean_2" ~ "Post",
.default ="text")) |>
mutate(Timepoint = factor(Timepoint,levels=c("Pre","Post")))|>
group_by(Timepoint)|>
mutate(paired = seq(1:length(Timepoint)))
p = ggplot(df, aes(x = Timepoint)) +
geom_line(aes(group = paired,y =Value),
linewidth=1,alpha = 0.7,
position = position_jitter(width = 0.1, seed = 3922)) +
geom_point(aes(color=Timepoint,y=Value),size = 6,position = position_jitter(width = 0.1, seed = 3922)) +
labs(y= "% of Correct Numbers", x = "Pre or Post Sleep Journaling")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
ggplotly(p)
df = as.data.frame(final_df |>
select(contains(c("subject","matching_words_count_mean")))) |>
pivot_longer(cols = c(matching_words_count_mean, matching_words_count_mean_2), names_to = "Timepoint", values_to = "Value") |>
mutate(Timepoint = case_when( # change the y and n responses to 1 and zero
Timepoint == "matching_words_count_mean" ~ "Pre",
Timepoint == "matching_words_count_mean_2" ~ "Post",
.default ="text")) |>
mutate(Timepoint = factor(Timepoint,levels=c("Pre","Post")))|>
group_by(Timepoint)|>
mutate(paired = seq(1:length(Timepoint)))
p = ggplot(df, aes(x = Timepoint)) +
geom_line(aes(group = paired,y =Value),
linewidth=1,alpha = 0.7,
position = position_jitter(width = 0.1, seed = 3922)) +
geom_point(aes(color=Timepoint,y=Value),size = 6,position = position_jitter(width = 0.1, seed = 3922)) +
labs(y= " Words Remebered", x = "Pre or Post Sleep Journaling")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
ggplotly(p)
Question: Does the amount of hours spent sleeping affect memory consolidation?
merged_dataset = read_excel("Z:\\30_Oscar_Ortiz\\MENTOR\\AL\\final_analysis\\\\FINAL_ANALYSIS\\final_merged_dataset.xlsx")
p = ggplot(merged_dataset, aes(x=hours_sleeping_mean, y=success_rate_2)) +
geom_point(size = 6)+
geom_smooth(method=lm, se = FALSE,size = 2) +
labs(y= "% of Correct Letters", x = "Hours Sleeping")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# super positive relationship
p = ggplot(merged_dataset, aes(x=hours_sleeping_mean, y=percent_match_ordered_mean)) +
geom_point(size = 6)+
geom_smooth(method=lm, se = FALSE,size = 2) +
labs(y= "% of Correct Numbers ", x = "Hours Sleeping")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# super negative relationship
p = ggplot(merged_dataset, aes(x=hours_sleeping_mean, y=matching_words_count_mean)) +
geom_point(size = 6)+
geom_smooth(method=lm, se = FALSE,size = 2) +
labs(y= "Words Remembered ", x = "Hours Sleeping")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# Create subject IDs
subject_id <- sprintf("%02d", 1:7)
# Create random values for pre and post timepoints
pre_values <- c(6,5,10,8,9,7,5)
post_values <- c(10,5,9,7,8,7,7)
# Create dataframe
df <- data.frame(subject_id = subject_id,
Pre = pre_values,
Post = post_values)
df = df |>
pivot_longer(cols = c(Pre, Post), names_to = "Timepoint", values_to = "Value") |>
mutate(Timepoint = factor(Timepoint,levels=c("Pre","Post")))|>
group_by(Timepoint)|>
mutate(paired = seq(1:length(Timepoint)))
p = ggplot(df, aes(x = Timepoint)) +
geom_line(aes(group = paired,y =Value),
linewidth=1,alpha = 0.7,
position = position_jitter(width = 0.1, seed = 3922)) +
geom_point(aes(color=Timepoint,y=Value),size = 6,position = position_jitter(width = 0.1, seed = 3922)) +
labs(y= "GLOBAL PSQI Score", x = "Pre or Post Sleep Journaling")+
theme(
axis.text = element_text(size = 14), # Adjust axis text size
axis.title = element_text(size = 16), # Adjust axis title size
plot.title = element_text(size = 18), # Adjust plot title size
legend.text = element_text(size = 12) # Adjust legend text size
)
ggplotly(p)
Now to look into the sleeping behaviour
data<- read_excel("Z:\\30_Oscar_Ortiz\\MENTOR\\AL\\final_analysis\\sleep_journal.xlsx")
cleaning and formatting the data
time_bed <- as.POSIXct(data$time_bed)
time_up <- as.POSIXct(data$time_up)
# Converts time_bed and time_up to a POSIXct object
data$time_24_hour_bed <- format(time_bed, "%H:%M:%S")
data$time_24_hour_up <- format(time_up, "%H:%M:%S")
# Converts POSIXct object time_bed and time_up to a character string
# Note: POSIXct = calendar time, POSIXlt = local time
data$time_12_hour_bed <- format(strptime(data$time_24_hour_bed, "%H:%M:%S"), "%I:%M %p")
data$time_12_hour_up <- format(strptime(data$time_24_hour_up, "%H:%M:%S"), "%I:%M %p")
# Converts time_bed and time_up string to POSIXlt objects
# POSIXlt objects are then formatted with AM/PM by a string
data$numeric_time_bed <- hour(time_bed) + minute(time_bed)/60 + second(time_bed)/3600
data$numeric_time_up <- hour(time_up) + minute(time_up)/60 + second(time_up)/3600
# Lubridate functions extracts time_bed and time_up data
# Data is converted into a numerical value
data$hours_spent_bed <- difftime(time_up, time_bed, units = "hours")
# Calculates difference in hours between time_up and time_bed
if (as.numeric(data$hours_spent_bed[1]) < 0) {
data$hours_spent_bed <- data$hours_spent_bed + 24
}
# 1. Checks if numerical hours_spent_bed is negative
# 2. If true, adds 24 hours to values in hours_spent_bed
data$hours_spent_bed <- ifelse(as.numeric(data$hours_spent_bed) > 23,
data$hours_spent_bed - 24,
data$hours_spent_bed)
earliest_dates <- data|>
group_by(id)|>
summarise(earliest_date = min(date))
# 1. Assigns data set to variable and feeds result down
# 2. Groups data by ID column for ID operations later on
# 3. Creates subset containing earliest_date for each ID
earliest_dates$earliest_date = as.Date(earliest_dates$earliest_date)
data$date = as.Date(data$date)
# 1. Formats variable earliest_date in earliest_dates
# 2. Formats variable date in data
data_clean = data|>
group_by(id)|>
mutate(earliest_date = min(date)) |>
ungroup()|>
mutate(days_from_start = as.numeric(date - as.Date(earliest_date)))
# cannot get time to go to bed yet so we move.
data_clean <- data_clean %>%
mutate(subject_id = case_when(
id == "AL" ~ "S01",
id == "OOA" ~ "S02",
id == "EL" ~ "S03",
id == "mjnt" ~ "S04",
id == "IT" ~ "S05",
id == "pt" ~ "S06",
id == "VM" ~ "S07",
TRUE ~ as.character(id)
))
ChatGPT Time Conversion Formula
convert_to_12_hour <- function(numeric_time) {
hours <- floor(numeric_time)
minutes <- round((numeric_time - hours) * 60)
meridiem <- ifelse(hours < 12, "AM", "PM")
hours <- ifelse(hours == 0, 12, ifelse(hours <= 12, hours, hours - 12))
paste0(hours, ":", sprintf("%02d", minutes), " ", meridiem)
}
# 1. Defines a function called numeric_time
# 2. Extracts integer (hour) part of numeric_time
# 3. Calculates minutes using extracted hour data
# 4. Determines if time is AM or PM using vector conditional
# 5. Converts hours to a 12-hour format, if:
# hours = 0, hours = 12
# hours <= 12, hours = hours
# hours > 12, hours = hours - 12
# 6. Makes a string in 12-hour format
data_clean <- data_clean |>
mutate(formatted_time_12_hour_up = convert_to_12_hour(numeric_time_up))
# 2. Creates a new column with values equal to a function
print(data_clean$formatted_time_12_hour_up)
## [1] "9:50 AM" "8:45 AM" "8:35 AM" "11:00 AM" "7:20 AM" "7:15 AM"
## [7] "8:00 AM" "7:30 AM" "9:17 AM" "9:25 AM" "7:12 AM" "7:12 AM"
## [13] "7:15 AM" "7:27 AM" "7:07 AM" "5:30 AM" "7:57 AM" "7:20 AM"
## [19] "7:15 AM" "7:11 AM" "7:05 AM" "7:08 AM" "7:52 AM" "9:15 AM"
## [25] "8:25 AM" "8:55 AM" "9:23 AM" "9:39 AM" "9:55 AM" "9:30 AM"
## [31] "9:30 AM" "7:12 AM" "8:20 AM" "7:40 AM" "7:35 AM" "8:00 AM"
## [37] "10:00 AM" "9:55 AM" "5:30 AM" "6:45 AM" "5:30 AM" "5:48 AM"
## [43] "6:30 AM" "8:00 AM" "6:30 AM" "7:30 AM" "7:30 AM" "8:00 AM"
## [49] "8:45 AM" "7:15 AM" "6:30 AM" "6:30 AM" "6:30 AM" "7:30 AM"
## [55] "6:00 AM" "6:30 AM" "6:30 AM" "6:30 AM" "6:30 AM" "6:30 AM"
## [61] "7:30 AM" "8:30 AM" "8:30 AM" "8:30 AM" "9:40 AM" "8:30 AM"
## [67] "7:00 AM" "7:00 AM" "3:30 AM" "3:50 AM" "3:30 AM" "4:00 AM"
## [73] "4:15 AM" "4:00 AM" "4:00 AM" "4:00 AM" "4:00 AM" "4:00 AM"
## [79] "4:00 AM" "4:00 AM" "4:00 AM" "3:45 AM" "4:30 AM" "4:00 AM"
## [85] "4:00 AM" "3:40 AM" "3:30 AM" "4:00 AM" "3:45 AM" "3:30 AM"
## [91] "3:45 AM" "3:45 AM" "3:45 AM" "3:45 AM" "6:00 AM" "4:00 AM"
## [97] "3:45 AM" "4:00 AM" "4:00 AM" "6:45 AM" "6:20 AM" "5:00 AM"
## [103] "6:30 AM" "6:45 AM" "6:10 AM" "6:30 AM" "6:30 AM" "6:40 AM"
## [109] "6:00 AM" "7:08 AM" "8:00 AM" "8:00 AM" "8:00 AM" "8:00 AM"
## [115] "8:30 AM" "7:00 AM" "6:45 AM" "6:45 AM" "7:00 AM" "6:30 AM"
## [121] "7:32 AM" "7:00 AM" "6:37 AM" "6:45 AM" "6:32 AM" "8:00 AM"
## [127] "9:00 AM" "6:00 AM" "7:00 AM" "6:45 AM" "8:38 AM" "7:45 AM"
## [133] "8:45 AM" "6:15 AM" "7:45 AM" "1:00 AM" "9:30 AM" "8:47 AM"
## [139] "7:52 AM" "7:15 AM" "7:15 AM" "7:30 AM" "7:30 AM" "7:45 AM"
## [145] "4:50 AM" "5:50 AM" "9:00 AM" "10:30 AM" "7:50 AM" "5:00 AM"
## [151] "2:00 AM" "2:00 AM" "2:00 AM" "2:00 AM" "2:30 AM" "3:00 AM"
## [157] "1:45 AM" "2:00 AM" "2:00 AM" "2:00 AM" "2:00 AM" "2:00 AM"
## [163] "3:00 AM" "1:30 AM" "2:00 AM" "2:00 AM" "2:00 AM" "2:00 AM"
## [169] "4:00 AM" "4:00 AM" "2:00 AM" "2:00 AM" "2:00 AM" "2:00 AM"
## [175] "4:00 AM" "4:00 AM" "2:10 AM" "4:00 AM" "3:45 AM" "4:00 AM"
## [181] "3:30 AM" "8:30 AM" "8:20 AM" "8:00 AM" "8:05 AM" "8:00 AM"
## [187] "9:00 AM" "9:30 AM" "8:00 AM" "7:00 AM" "6:30 AM" "7:15 AM"
## [193] "10:30 AM" "8:00 AM" "11:00 AM" "7:45 AM" "6:20 AM" "8:00 AM"
## [199] "9:00 AM" "8:00 AM" "9:00 AM" "NA:NA NA" "7:30 AM"
Data Visualization - Hours in Bed Consistency
data_clean <- data_clean %>%
mutate(subject_id = case_when(
id == "OOA" ~ "S01",
id == "AL" ~ "S02",
id == "EL" ~ "S03",
id == "mjnt" ~ "S04",
id == "IT" ~ "S05",
id == "pt" ~ "S06",
id == "VM" ~ "S07",
id == "NA" ~ "S01",
TRUE ~ as.character(id)
))
data_clean <- data_clean[-202, ]
p = ggplot(data_clean, aes(x = days_from_start, y = hours_spent_bed, color = factor(subject_id))) +
geom_point() +
geom_line() +
facet_wrap(~subject_id) +
labs(x = "Days Since Start", y = "Hours Spent in bed") +
scale_y_continuous(breaks = seq(0, 12, by = 4),
labels = c("0", "4", "8", "12"),
limits = c(0, 12))
ggplotly(p)
# 1. States x and y-axis variables, color represents a unique ID
# 2. Adds points to the plot for each data point in data_clean
# 3. Connects each point from left to right creating line graph
# 4. Creates distinct subplots using steps 1-3 for each ID
# 5. Labels x and y-axis
Data Visualization - Hours Bed vs. Hours Sleeping
scatter_plot <- ggplot(data_clean, aes(x = days_from_start, y = hours_spent_bed, color = factor(subject_id))) +
geom_point() +
geom_line() +
facet_wrap(~subject_id) +
labs(x = "Days Since Start", y = "Hours Spent in Bed/Sleeping")
# Creates scatter plot based on hours spent in bed data
combined_plot <- scatter_plot +
geom_line(aes(y = as.numeric(hours_sleeping), group = id), color = "black", size = 1.0)
# ----------------------------------------------------------------------------------------- 13h bed value, find way to min to max, readable breaks
# scale_y_continuous(breaks = seq(0, 12, by = 4),
# labels = c("0", "4", "8", "12"),
# limits = c(0, 12))
# -----------------------------------------------------------------------------------------
# Overlays a black line plot based on hours_sleeping on the scatter plot
ggplotly(combined_plot)
Data Visualization - Sleep Rating
p = ggplot(data_clean, aes(x = sleep_rating, fill = subject_id)) +
geom_histogram(bins = 10) +
facet_wrap(~subject_id, nrow = 1) +
labs(x = "Personal Sleep Rating", y = "Frequency")
ggplotly(p)
# Specify the columns containing repeated measures
repeated_measure_columns <- c("subject_id","minutes2asleep", "hours_sleeping", "sleep_rating", "anxiety_rating", "rested_rating", "remember_dream", "dream_vivid_rating", "dream_type", "sleep_interruption", "hours_napped", "numeric_time_bed", "numeric_time_up", "hours_spent_bed" )
sleep_dat_sub = data_clean |>
select(all_of(repeated_measure_columns))# |>
# na.omit()
run some basic correlations to see if more sleep = better rest
p = ggplot(sleep_dat_sub, aes(x=hours_sleeping, y=sleep_rating,color=subject_id)) +
geom_point()+
geom_smooth(method=lm)+
labs(y= "sleep rate", x = "hours of sleep")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# clear positive relationship
p = ggplot(sleep_dat_sub, aes(x=hours_sleeping, y=anxiety_rating,color=subject_id)) +
geom_point()+
geom_smooth(method=lm)+
labs(y= "anxiety rate", x = "hours of sleep")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# not super clear relationship actually
p = ggplot(sleep_dat_sub, aes(x=hours_sleeping, y=rested_rating,color=subject_id)) +
geom_point()+
geom_smooth(method=lm)+
labs(y= "resting rate", x = "hours of sleep")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# also a positive relantionship
p = ggplot(sleep_dat_sub, aes(x=numeric_time_up, y=rested_rating,color=subject_id)) +
geom_point()+
geom_smooth(method=lm)+
labs(y= "resting rate", x = "Wake up time")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# not a clear relationship
Dreaming frequency plots
# Create the bar plot using ggplot
p = sleep_dat_sub |>
select(remember_dream)|>
na.omit()|>
ggplot(aes(x = remember_dream,fill = remember_dream)) +
geom_bar()
ggplotly(p)
# more nights people did not remember their dreams
p = sleep_dat_sub |>
select(dream_vivid_rating,hours_sleeping,subject_id)|>
na.omit()|>
ggplot(aes(x=hours_sleeping, y=dream_vivid_rating,color=subject_id)) +
geom_point()+
geom_smooth(method=lm,se = FALSE)+
labs(y= "dream vivid rating", x = "hours of sleep")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
# interesting... s05 did not remember their dreams at all.
p = ggplot(sleep_dat_sub, aes(x=hours_sleeping, y=sleep_rating,color=remember_dream)) +
geom_point()+
geom_smooth(method=lm)+
labs(y= "sleep rate", x = "hours of sleep")
ggplotly(p)
## `geom_smooth()` using formula = 'y ~ x'
#interesting... nights where the dream is not being remembered have a better relationship between sleep rate and hours of sleep