Delimit periods
Packages: dplyr, ggplot2
Delimit weeks
Delimit using colors
%>%
data filter(id == 1) %>%
ggplot(aes(x=obsno,y=PA1, color=factor(daycum))) +
geom_path(aes(group=1)) +
geom_point()
Delimit using lines
= data %>% filter(id == 1)
data_pp = floor_date(min(data_pp$sent), unit="day")
min_ = floor_date(max(data_pp$sent), unit="day")
max_ = seq(min_, max_, "day")
dates = dates[wday(dates) == 2]
week_ints %>%
data_pp ggplot(aes(x=sent, y=PA1)) +
geom_line() +
geom_point() +
geom_vline(xintercept=week_ints, color="blue")
= floor_date(min(data$sent), unit="day")
min_ = floor_date(max(data$sent), unit="day")
max_ = seq(min_, max_, "day")
dates = as.Date(dates[wday(dates) == 2])
week_ints
= data %>%
data mutate(week = ifelse(as.Date(sent) %in% week_ints & !lag(as.Date(sent)) %in% week_ints, obsno, NA))
= data %>%
data group_by(id) %>%
mutate(weekcum = difftime(floor_date(sent, 'week'), as.Date(min(sent, na.rm=TRUE)), units="weeks") + 1)
= data %>% arrange(id, sent) %>%
data group_by(id, weekcum) %>%
mutate(obsnoweek = 1:n(), obsnoweek_inv = n():1)
= ifelse(data$beepperiod == 1 | data$beepperiod_inv == 1, data$obsno, NA)
obsno_week
%>% filter(id <= 15) %>%
data ggplot( aes(x=obsno, y=PA1)) +
geom_line() +
geom_point() +
geom_vline(aes(xintercept=week), color="blue", size=1) +
facet_wrap(.~id)
Delimit weekend
Delimit using colors
%>%
data filter(id == 1) %>%
mutate(wday = ifelse(wday(sent) %in% c(1,7), "weekend", "weekday")) %>%
ggplot(aes(x=obsno,y=PA1, color=factor(wday))) +
geom_path(aes(group=1)) +
geom_point()
WITH LINES:
For one person
= floor_date(min(data_pp$sent), unit="day")
min_ = floor_date(max(data_pp$sent), unit="day")
max_ = seq(min_, max_, "day")
dates = dates[wday(dates) %in% c(2,7)]
week_ints
%>%
data_pp ggplot( aes(x=sent, y=PA1)) +
geom_line(color="#69b3a2") +
geom_point() +
geom_vline(xintercept=week_ints, color="blue")
For everyone in the dataset
= floor_date(min(data$sent), unit="day")
min_ = floor_date(max(data$sent), unit="day")
max_ = seq(min_, max_, "day")
dates = as.Date(dates[wday(dates) == 1])
weekend_end = as.Date(dates[wday(dates) == 7])
weekend_start
= data %>%
data group_by(id) %>%
mutate(daycum = difftime(as.Date(sent), as.Date(min(sent, na.rm=TRUE)), units="days") + 1) %>%
group_by(id,daycum) %>%
mutate(obsnoday = 1:n(), obsnoday_inv = n():1)
= data$obsno == 1
first_beep = data$obsno == 70
max_beep = as.Date(data$sent) %in% weekend_start & data$obsnoday == 1
start_weekend = as.Date(data$sent) %in% weekend_end & data$obsnoday_inv == 1
end_weekend = ifelse(first_beep | max_beep | start_weekend | end_weekend, data$obsno, NA)
obsno_week = data.frame(id=data$id, sent=data$sent, start=obsno_week) %>%
df_ filter(!is.na(start)) %>%
group_by(id) %>%
mutate(end = lead(start),
color = ifelse(wday(sent) %in% c(1:5,7), "red", "blue")) %>% #TODO: it is weird that it is 1 and 7 (issue?)
filter(!is.na(end))
= 1
id_ = df_ %>% filter(id == id_)
df_id = data %>% filter(id == id_)
data_id
ggplot() +
geom_line(color="#69b3a2") +
geom_rect(data = df_id, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = color), alpha = 0.5) +
geom_point(data=data_id, aes(x=obsno, y=PA1)) +
geom_line(data=data_id, aes(x=obsno, y=PA1))
ggplot() +
geom_line(color="#69b3a2") +
geom_rect(data = df_, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = color), alpha = 0.5) +
geom_point(data=data, aes(x=obsno, y=PA1)) +
geom_line(data=data, aes(x=obsno, y=PA1)) +
facet_wrap(.~id)
Delimit days
= data %>% arrange(id, sent) %>%
data group_by(id,daycum) %>%
mutate(obsnoday = 1:n(), obsnoday_inv = n():1)
= ifelse(data$obsnoday == 1 | data$obsnoday_inv == 1, data$obsno, NA)
obsno_week
= data.frame(id=data$id, sent=data$sent, daycum=data$daycum, start=obsno_week) %>%
df_ filter(!is.na(start)) %>%
group_by(id) %>%
mutate(end = lead(start)) %>%
filter(!is.na(end)) %>%
mutate(color = ifelse(as.numeric(daycum) %% 2 == 1, "red", "blue"))
= 1
id_ = df_ %>% filter(id == id_)
df_id = data %>% filter(id == id_)
data_id
ggplot() +
geom_line(color="#69b3a2") +
geom_rect(data = df_id, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = color), alpha = 0.5) +
geom_point(data=data_id, aes(x=obsno, y=PA1)) +
geom_line(data=data_id, aes(x=obsno, y=PA1))
ggplot() +
geom_line(color="#69b3a2") +
geom_rect(data = df_, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = color), alpha = 0.5) +
geom_point(data=data, aes(x=obsno, y=PA1)) +
geom_line(data=data, aes(x=obsno, y=PA1)) +
facet_wrap(.~id)
period in a day
library(hms)
$period = case_when(as_hms(data$sent) < as_hms("12:00:00") ~ "morning",
dataas_hms(data$sent) < as_hms("18:00:00") ~ "afternoon",
as_hms(data$sent) < as_hms("24:00:00") ~ "evening")
= data %>% arrange(id, sent) %>%
data group_by(id, daycum, period) %>%
mutate(beepperiod = 1:n(), beepperiod_inv = n():1)
= ifelse(data$beepperiod == 1 | data$beepperiod_inv == 1, data$obsno, NA)
obsno_week
= data.frame(id=data$id, sent=data$sent, period=data$period, start=obsno_week) %>%
df_ filter(!is.na(start)) %>%
group_by(id) %>%
mutate(end = lead(start)) %>%
filter(!is.na(end)) %>%
mutate(color = case_when(period == "morning" ~ "red",
== "afternoon" ~ "blue",
period == "evening" ~ "green")) period
= 1
id_ = df_ %>% filter(id == id_)
df_id = data %>% filter(id == id_)
data_id
ggplot() +
geom_line(color="#69b3a2") +
geom_rect(data = df_id, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = color), alpha = 0.5) +
geom_point(data=data_id, aes(x=obsno, y=PA1)) +
geom_line(data=data_id, aes(x=obsno, y=PA1))
ggplot() +
geom_line(color="#69b3a2") +
geom_rect(data = df_, aes(xmin = start, xmax = end, ymin = - Inf, ymax = Inf, fill = color), alpha = 0.5) +
geom_point(data=data, aes(x=obsno, y=PA1)) +
geom_line(data=data, aes(x=obsno, y=PA1)) +
facet_wrap(.~id)