In this take home exercise I will reveal the daily routines of two selected participants.
In this take-home exercise, I will explore the daily life pattern of the least and most jovial participant with the use of the (ViSiElse)[https://cran.r-project.org/web/packages/ViSiElse/index.html] package.
The required packages will be called with the following code chunk:
packages = c('tidyverse', "ViSiElse", "lubridate")
for (p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
The code chunk below will import Participants.csv from the
data folder into R by using read_csv()
of readr
package and save it as a tibble data frame called
Participant.
Participants <- read_csv("data/Participants.csv")
participantMaxJ <- Participants$participantId[Participants$joviality
== max(Participants$joviality)]
participantMinJ <- Participants$participantId[Participants$joviality
== min(Participants$joviality)]
selectedParticipants <- c(participantMinJ, participantMaxJ)
print(paste("Participant with max joviality is: ", participantMaxJ, ".",
"Participant with min joviality is: ", participantMinJ, sep=""))
Participant with the highest joviality is participant 113 and participant with the lowest joviality is participant 758.
# A tibble: 2 x 7
participantId householdSize haveKids age educationLevel
<dbl> <dbl> <lgl> <dbl> <chr>
1 113 2 FALSE 51 Graduate
2 758 3 TRUE 56 HighSchoolOrCollege
# ... with 2 more variables: interestGroup <chr>, joviality <dbl>
keep <- c("participantId", "timestamp", "currentMode", "sleepStatus")
ParticipantsLog <- ParticipantsLog[, keep]
Participant758 <- subset(ParticipantsLog, ParticipantsLog$participantId == 758)
Participant113<- subset(ParticipantsLog, ParticipantsLog$participantId == 113)
keep <- c("timestamp", "currentMode", "sleepStatus")
Participant758 <- ParticipantsLog[, keep]
Participant113 <- ParticipantsLog[, keep]
Participant758 <- Participant758 %>%
mutate(minutes = hour(timestamp)*60 + minute(timestamp))
Participant758 <- Participant758 %>%
mutate(date = date(timestamp))
Participant758$status <- paste(Participant758$currentMode,
Participant758$sleepStatus, sep="-")
Participant758$status[Participant758$status == "AtRestaurant-PrepareToSleep"]
= "AtRestaurant-Awake"Participant758Summary$LunchStart <- NA
Participant758Summary$LunchEnd <- NA
Lunch758$LunchEnd <- as.data.frame(Lunch758$LunchEnd)[[1]]
for (i in as.character(Lunch758$Dates)) {
for (g in as.character(Participant758Summary$Dates)) {
if (g == i) {
Participant758Summary$LunchStart[as.character(Participant758Summary$Dates)
== i] =
Lunch758$LunchStart[as.character(Lunch758$Dates) == i]
Participant758Summary$LunchEnd[as.character(Participant758Summary$Dates)
== i] =
Lunch758$LunchEnd[as.character(Lunch758$Dates) == i]
}
}
}
Participant758Summary$DinnerStart <- NA
Participant758Summary$DinnerEnd <- NA
Dinner758$DinnerEnd <- as.data.frame(Dinner758$DinnerEnd)[[1]]
for (i in as.character(Dinner758$Dates)) {
for (g in as.character(Participant758Summary$Dates)) {
if (g == i) {
Participant758Summary$DinnerStart[as.character(Participant758Summary$Dates)
== i] =
Dinner758$DinnerStart[as.character(Dinner758$Dates) == i]
Participant758Summary$DinnerEnd[as.character(Participant758Summary$Dates)
== i] =
Dinner758$DinnerEnd[as.character(Dinner758$Dates) == i]
}
}
}
Participant758$status[Participant758$status == "AtRecreation-PrepareToSleep"]
= "AtRecreation-Awake"Recreation758 <- Participant758 %>%
filter(status == "AtRecreation-Awake" & minutes != 0 & date != "2023-05-24")
%>%
group_by(date) %>%
summarise(minutes = min(minutes))
names(Recreation758) = c("Dates", "RecreationStart")Recreation758$RecreationEnd<- Participant758 %>%
filter(status == "AtRecreation-Awake" & minutes != 0 & date != "2023-05-24" )
%>%
group_by(date) %>%
summarise(minutes = max(minutes)) %>%
select(minutes)Participant758Summary$RecreationStart <- NA
Participant758Summary$RecreationEnd <- NA
Recreation758$RecreationEnd <- as.data.frame(Recreation758$RecreationEnd)[[1]]
for (i in as.character(Recreation758$Dates)) {
for (g in as.character(Participant758Summary$Dates)) {
if (g == i) {
Participant758Summary$RecreationStart[
as.character(Participant758Summary$Dates) == i] =
Recreation758$RecreationStart[as.character(Recreation758$Dates) == i]
Participant758Summary$RecreationEnd[
as.character(Participant758Summary$Dates) == i] =
Recreation758$RecreationEnd[as.character(Recreation758$Dates) == i]
}
}
}
Participant758Summary$PrepareToSleep <- NA
for (i in as.character(PrepareToSleep$date)) {
for (g in as.character(Participant758Summary$Dates)) {
if (g == i) {
Participant758Summary$PrepareToSleep[
as.character(Participant758Summary$Dates) == i] =
PrepareToSleep$minutes[as.character(PrepareToSleep$date) == i]
}
}
}
Participant758Summary$Sleep <-
as.data.frame(Participant758Summary$Sleep)[[1]]
Participant758Summary$TransportDayStart <-
as.data.frame(Participant758Summary$TransportDayStart)[[1]]
Participant758Summary$TransportEveningStart <- as.data.frame(Participant758Summary$TransportEveningStart)[[1]]
Participant758Summary$Backhome <-
as.data.frame(Participant758Summary$Backhome)[[1]]
Participant758Summary$WorkStart <-
as.data.frame(Participant758Summary$WorkStart)[[1]]
Participant758Summary$WorkEnd <-
as.data.frame(Participant758Summary$WorkEnd)[[1]]
Participant758Summary$StartSleep <- 0
Clean758 <- as.data.frame(lapply(Participant758Summary, as.integer))
p1 <- visielse(Clean758, informer = NULL)
b1 <- ConvertFromViSibook(p1@book)
b1 <- b1[order(as.numeric(b1$showorder)), ] # order the data.frame
b1$label <- c("Wake Up", "Sleep", "Day Transport", "Evening Transport",
"Reach Home", "Start Work", "End Work", "Start Lunch",
"End Lunch", "Start Dinner", "End Dinner", "Start Recreation",
"End Recreation", "Prepare To Sleep", "Start Sleeping")
b1[16,] <- c("sleep", "Sleeping", "l", 1, "StartSleep", "WakeUp")
b1[17,] <- c("work", "Working", "l", 6, "WorkStart", "WorkEnd")
b1[18,] <- c("lunch", "Lunch break", "l", 7, "LunchStart", "LunchEnd")
b1[19,] <- c("dinner", "Dinner", "l", 10, "DinnerStart", "DinnerEnd")
b1$showorder <- c(2, NA, 3, 8, 10, 4, 6, NA, NA, NA, NA, 12, 13, 11, NA, 1,
5,7,9)
b1 <- b1[order(as.numeric(b1$showorder)), ]
v2 <- visielse(Clean758,
book = b1,
informer = NULL,
doplot = F,
pixel = 30)
plot(v2,
vp0w = 0.7,
unit.tps = "min",
scal.unit.tps = 30,
main = "Typical day for Participant 758")
From the visualization it can be seen that the participant is very routine in terms of their when they wake up and head to bed. However, it can be seen that their lunch time is quite erratic. Working hours is very routine as well. One interesting observation is that there are times when the participant heads out to dinner. These occurrences are few and not routine. Furthermore, the start and end of their reacreation period is also not regular, indicating that it may differ depending on the day of the week.
Participant113 <- Participant113 %>%
mutate(minutes = hour(timestamp)*60 + minute(timestamp))
Participant113 <- Participant113 %>%
mutate(date = date(timestamp))
Participant113$status <- paste(Participant113$currentMode,
Participant113$sleepStatus, sep="-")
Participant113$status[Participant113$status == "AtRestaurant-PrepareToSleep"]
= "AtRestaurant-Awake"Participant113Summary$LunchStart <- NA
Participant113Summary$LunchEnd <- NA
Lunch113$LunchEnd <- as.data.frame(Lunch113$LunchEnd)[[1]]
for (i in as.character(Lunch113$Dates)) {
for (g in as.character(Participant113Summary$Dates)) {
if (g == i) {
Participant113Summary$LunchStart[as.character(Participant113Summary$Dates)
== i] =
Lunch113$LunchStart[as.character(Lunch113$Dates) == i]
Participant113Summary$LunchEnd[as.character(Participant113Summary$Dates)
== i] =
Lunch113$LunchEnd[as.character(Lunch113$Dates) == i]
}
}
}
Participant113Summary$DinnerStart <- NA
Participant113Summary$DinnerEnd <- NA
Dinner113$DinnerEnd <- as.data.frame(Dinner113$DinnerEnd)[[1]]
for (i in as.character(Dinner113$Dates)) {
for (g in as.character(Participant113Summary$Dates)) {
if (g == i) {
Participant113Summary$DinnerStart[
as.character(Participant113Summary$Dates) == i] =
Dinner113$DinnerStart[as.character(Dinner113$Dates) == i]
Participant113Summary$DinnerEnd[
as.character(Participant113Summary$Dates) == i] =
Dinner113$DinnerEnd[as.character(Dinner113$Dates) == i]
}
}
}
Participant113$status[Participant113$status == "AtRecreation-PrepareToSleep"]
= "AtRecreation-Awake"Participant113Summary$RecreationStart <- NA
Participant113Summary$RecreationEnd <- NA
Recreation113$RecreationEnd <- as.data.frame(Recreation113$RecreationEnd)[[1]]
for (i in as.character(Recreation113$Dates)) {
for (g in as.character(Participant113Summary$Dates)) {
if (g == i) {
Participant113Summary$RecreationStart[
as.character(Participant113Summary$Dates) == i] =
Recreation113$RecreationStart[as.character(Recreation113$Dates) == i]
Participant113Summary$RecreationEnd[
as.character(Participant113Summary$Dates) == i] =
Recreation113$RecreationEnd[as.character(Recreation113$Dates) == i]
}
}
}
Participant113Summary$PrepareToSleep <- NA
for (i in as.character(PrepareToSleep$date)) {
for (g in as.character(Participant113Summary$Dates)) {
if (g == i) {
Participant113Summary$PrepareToSleep[
as.character(Participant113Summary$Dates) == i] =
PrepareToSleep$minutes[as.character(PrepareToSleep$date) == i]
}
}
}
Participant113Summary$Sleep <-
as.data.frame(Participant113Summary$Sleep)[[1]]
Participant113Summary$TransportDayStart <-
as.data.frame(Participant113Summary$TransportDayStart)[[1]]
Participant113Summary$TransportEveningStart <- as.data.frame(Participant113Summary$TransportEveningStart)[[1]]
Participant113Summary$Backhome <-
as.data.frame(Participant113Summary$Backhome)[[1]]
Participant113Summary$WorkStart <-
as.data.frame(Participant113Summary$WorkStart)[[1]]
Participant113Summary$WorkEnd <-
as.data.frame(Participant113Summary$WorkEnd)[[1]]
Participant113Summary$StartSleep <- 0
Clean113 <- as.data.frame(lapply(Participant113Summary, as.integer))
p2 <- visielse(Clean113, informer = NULL)
b2 <- ConvertFromViSibook(p2@book)
b2 <- b2[order(as.numeric(b2$showorder)), ] # order the data.frame
b2$label <- c("Wake Up", "Sleep", "Day Transport", "Evening Transport",
"Reach Home", "Start Work", "End Work", "Start Lunch",
"End Lunch", "Start Dinner", "End Dinner", "Start Recreation",
"End Recreation", "Prepare To Sleep", "Start Sleeping")
b2[16,] <- c("sleep", "Sleeping", "l", 1, "StartSleep", "WakeUp")
b2[17,] <- c("work", "Working", "l", 6, "WorkStart", "WorkEnd")
b2[18,] <- c("lunch", "Lunch break", "l", 7, "LunchStart", "LunchEnd")
b2[19,] <- c("dinner", "Dinner", "l", 10, "DinnerStart", "DinnerEnd")
b2$showorder <- c(2, NA, 3, 8, 10, 4, 6, NA, NA, NA, NA, 12, 13, 11, NA, 1,
5,7,9)
b2 <- b2[order(as.numeric(b2$showorder)), ]
v3 <- visielse(Clean113,
book = b2,
informer = NULL,
doplot = F,
pixel = 30)
plot(v3,
vp0w = 0.7,
unit.tps = "min",
scal.unit.tps = 30,
main = "Typical day for Participant 113")
The visualization is almost identical to the previous participant. This will be discussed further in the next section.
plot(v2,
vp0w = 0.7,
unit.tps = "min",
scal.unit.tps = 30,
main = "Typical day for Participant 758")
plot(v3,
vp0w = 0.7,
unit.tps = "min",
scal.unit.tps = 30,
main = "Typical day for Participant 113")
There is not much difference seen in the graphs of both participants. This may be due to multiple reasons:
On hindsight the ViSiElse library may not have been the most effective for this visualization as it is very rigid in terms of the data formatting required. This led to extensive hours of data cleaning before any visualization could be acheived to check if the progress made was on the right track.