Visualising and Analysing Daily Routines
The main purpose of this take-home exercise is to use ViSiElse and other proper R packages to visualise and analyse the daily pattern of two selected participants.
packages = c('scales','viridis','lubridate','ggthemes',
'gridExtra','tidyverse','readxl','knitr',
'data.table','ViSiElse')
for (p in packages){
if(!require(p, character.only = T)){
install.packages(p)
}
library(p,character.only = T)
}
logs <- list.files(path = "./data/ActivityLogs/",
pattern = "*.csv",
full.names = T) %>%
map_df(~read_csv(.,
col_types = cols(.default = "c")))
logs <- logs[ -c(2,7:12) ]
Extract logs of participant 10.
logs <- logs[logs$participantId == '10', ]
logs_10 <- logs[-c(2)]
Extract day from timestamp using lubridate package.
logs_10$day <-day(logs_10$timestamp)
Calculate time elapse with the start of midnight in minute.
logs_10_new <-logs_10 %>%
group_by(day) %>%
mutate(min_time=first(timestamp))
logs_10_new$timeElapse <-
interval(logs_10_new$min_time, logs_10_new$timestamp)/dminutes(1)
log_10 <- logs_10_new[-c(1,6)]
head(log_10)
# A tibble: 6 x 5
# Groups: day [1]
currentMode hungerStatus sleepStatus day timeElapse
<chr> <chr> <chr> <int> <dbl>
1 AtHome JustAte Sleeping 1 0
2 AtHome JustAte Sleeping 1 5
3 AtHome JustAte Sleeping 1 10
4 AtHome JustAte Sleeping 1 15
5 AtHome JustAte Sleeping 1 20
6 AtHome JustAte Sleeping 1 25
Create separate logs of different types of activities: current mode, hunger status, and sleep status.
log_10_currentMode <-log_10_currentMode %>%
group_by(day,currentMode) %>%
filter(row_number()==1)
As for hunger status table, using group by, cut, filter to extract time for breakfast, lunch and dinner.
log_10_hungerStatus <-log_10_hungerStatus %>%
group_by(day) %>%
filter(hungerStatus=='JustAte')
log_10_hungerStatus$meal <-cut(
log_10_hungerStatus$timeElapse,
breaks = c(100, 500, 900, Inf),
labels = c("EatBreakfast", "EatLunch", "EatDinner"))
log_10_hungerStatus <-log_10_hungerStatus %>%
group_by(day, meal) %>%
filter(row_number()==1)
log_10_hungerStatus<-log_10_hungerStatus %>% drop_na(meal)
log_10_hungerStatus<-log_10_hungerStatus [-c(1)]
head(log_10_hungerStatus)
# A tibble: 6 x 3
# Groups: day, meal [6]
day timeElapse meal
<int> <dbl> <fct>
1 1 395 EatBreakfast
2 1 815 EatLunch
3 1 1240 EatDinner
4 2 390 EatBreakfast
5 2 810 EatLunch
6 2 1240 EatDinner
As for sleep status log, extract time for go to sleep, sleep and wake up.
log_10_sleepStatus <-log_10_sleepStatus %>%
group_by(day,sleepStatus) %>%
filter(row_number()==1)
log_10_sleepStatus$sleepStatus[log_10_sleepStatus$sleepStatus=="Awake"]<-"Wakeup"
log_10_sleepStatus$sleepStatus[log_10_sleepStatus$sleepStatus=="Sleeping"]<-"GoToSleep"
head(log_10_sleepStatus)
# A tibble: 6 x 3
# Groups: day, sleepStatus [6]
sleepStatus day timeElapse
<chr> <int> <dbl>
1 GoToSleep 1 0
2 Wakeup 1 390
3 PrepareToSleep 1 1290
4 GoToSleep 2 0
5 Wakeup 2 390
6 PrepareToSleep 2 1290
Join two tables together with proper structure for visualization.
library("writexl")
write_xlsx(log_10_sleepStatus,"D:\\JessieLee\\in-class-exercise\\th_ex\\data\\sleepStatus.xlsx")
write_xlsx(log_10_hungerStatus,"D:\\JessieLee\\in-class-exercise\\th_ex\\data\\hungerStatus.xlsx")
Status <- read_csv("data/Status.csv")
new_status <- read_csv("data/new_status.csv")
head(new_status)
# A tibble: 6 x 7
Day GoToSleep Wakeup EatBreakfast EatLunch EatDinner
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 0 390 395 815 1240
2 2 0 390 390 810 1240
3 3 0 390 390 810 1235
4 4 0 390 390 810 1235
5 5 0 390 440 885 905
6 6 0 390 440 885 905
# ... with 1 more variable: PrepareToSleep <dbl>
p1 <- visielse(new_status, informer = NULL)
b1 <- ConvertFromViSibook(p1@book)
Create ViSibook for further visualization.
b1 <- b1[order(as.numeric(b1$showorder)), ] # order the data.frame
b1$label <- c("Sleep","Wake up", "Eat breakfast","Eat lunch","Eat dinner","Go to sleep")
b1[7,] <- c("Sleep","Sleeping","l", 1,"GoToSleep","Wakeup")
b1$showorder <- c(NA,NA,3,4,5,6,1)
b1 <- b1[order(as.numeric(b1$showorder)), ]