Steps Taken for the Past 3 Months


Most steps taken: May 5, 2018, 11,936 Least steps taken: July 22, 2018, 86

Phone Use in July


Excluding the first few days with zero for both data - minutes on phone and times I pick up the phone: the least number of pick-ups: 9 times, July 21; the most number of pick-ups: 69 times, July 13; most time spent: 399 minutes on July 25; least time spent: 80 minutes on August 1.

Data caveat:ss Because on 2018-07-11, 2018-07-12, 2018-07-13, running of sleep cycle on backend was counted as phone use minute count, the numbers of minuteCount should be deducted by the sleep time for these three dates.

Phone Use on Each Day of Week


I tended to spend more time on my phone on Sunday and Friday on average.

Hours of Sleep


To avoid the wrong data skewing the relationship, for this chart and those charts after this, I excluded one data point where sleep duration was zero, which I think was an error due to app glitch.

On average, I sleep for a little less than 6 hours everyday.

Bedtime


I went to bed later than 2 am for 7 days out of the 19 days with data recorded.

Sleep Quality vs Hours of Sleep


The longer the sleep, the higher the sleep quality. The hours of sleep seems to have a moderate positive correlation with sleep quality.

Duration of Sleep Each Day of the Week


There was one Thursday and one Wednesday without data entry. Without Thursday and Wednesday, Sunday is the day when I sleep the longest

Quality of Sleep Each Day of the Week


Without Thursday and Wednesday, I have higher sleep quality on Saturdays and Sundays.

Sleep Duration vs Daily Steps


I mostly take between 2000 and 3000 steps a day, and on these days I sleep between 4 and 7 hours. When I have really low number of steps, my sleep hour tends to be longer, I think that’s because those days are weekends, when I stay at home, and sleep earlier. Although the linear regression line suggests weak to moderate negative correlation between number of steps and sleep duration, I don’t think there are enough data points to say.

Sleep Duration vs Minite on Phone


There seems to be weak positive correlation between the time I spend on phone and sleep duration. But it could again be because I haven’t collected enough data.

Sleep Quality vs. Minutes on Phone


There seems to be weak positive correlation between the time I spend and sleep quality.

---
title: "ANLY512_final_project_storyboard"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    social: menu
    source: embed
    storyboard: true
---

```{r setup, include=FALSE}
library(flexdashboard)
```

### Steps Taken for the Past 3 Months

```{r}
steps<- read.csv("/Users/yh101251/Desktop/HU/512/final_project_files/steps.csv")


steps <- steps[,-3]

steps$date <- as.Date(steps$date)
```

```{r}
#install.packages("dygraphs")
library(dygraphs)
library(xts)
steps <- steps[3:94,]

ts_obj <- xts(steps[,2],order.by=as.Date(steps$date,"%Y/%m/%d"))
names(ts_obj) <- c("steps")
dygraph(ts_obj,  main = "Steps Taken") %>% dyRangeSelector(dateWindow = c("2018-05-01", "2018-07-31")) %>%
  dyAxis("y", label = "Number of Steps", valueRange = c(0, 12000))

```

***
Most steps taken: May 5, 2018, 11,936
Least steps taken: July 22, 2018, 86


### Phone Use in July 

```{r}
moment<- read.csv("/Users/yh101251/Desktop/HU/512/final_project_files/moment.csv")

moment$date <- gsub("\\T.*","",moment$date)

moment$date <- as.Date(moment$date)


moment <- moment[order(as.Date(moment$date, format="%Y/%m/%d"), decreasing = FALSE),]

moment[8,1] <- moment[8,1] - 7.750000 * 60
moment[9,1] <- moment[9,1]  - 6.316667 * 60
moment[10,1] <- moment[10,1] - 6.416667 * 60

library(xts)

moment_ts_obj <- xts(moment[,1:2], order.by = as.Date(moment$date,"%Y/%m/%d"))
dygraph(moment_ts_obj, main = "Phone Use") %>% dyRangeSelector(dateWindow = c("2018-07-05", "2018-08-02")) %>%
  dyOptions(colors = RColorBrewer::brewer.pal(2, "Set2"))
```

***
Excluding the first few days with zero for both data - minutes on phone and times I pick up the phone: 
the least number of pick-ups: 9 times, July 21;
the most number of pick-ups: 69 times, July 13;
most time spent: 399 minutes on July 25;
least time spent: 80 minutes on August 1.

Data caveat:ss
Because on 2018-07-11, 2018-07-12, 2018-07-13, running of sleep cycle on backend was counted as phone use minute count, the numbers of minuteCount should be deducted by the sleep time for these three dates.

### Phone Use on Each Day of Week

```{r}
library(dplyr)
library(lubridate)
library(scales)
library(ggplot2)

moment %>% 
  mutate(
    day_of_week = as.Date(date) %>% wday(label = TRUE), 
    what_day = ifelse(grepl("Sat|Sun", day_of_week),"Weekend","Weekday") %>%
                                   as.factor()
  ) %>% 
  ggplot() +
  geom_boxplot(aes(x = day_of_week, y = minuteCount, color = what_day)) +
  scale_y_continuous(breaks = pretty_breaks(5), labels = percent_format()) +
#  scale_fill_manual(values = cols) +
  ggtitle("Distribution of Time Spent on Phone", subtitle = "Source: iPhone Momemnt data (2018/07/05 - 2018/08/02)") +
  labs(x = "Day of week", y = "Minutes") +
  theme_bw() +
  theme(legend.position = "none",
        axis.text.x = element_text(size = 11, face = "bold"),
        axis.text.y = element_text(size = 11, face = "bold"))
```

***
I tended to spend more time on my phone on Sunday and Friday on average.

### Hours of Sleep

```{r message=FALSE}
#install.packages("readr")
library(readr)
library(tidyverse)
library(lubridate)

df <- read_delim("/Users/yh101251/Desktop/HU/512/final_project_files/sleepdata.csv", delim = ";") %>% 
  rename(sleep_quality = `Sleep quality`,
         time_in_bed = `Time in bed`,
         steps = `Activity (steps)`) %>% 
  mutate(sleep_quality = str_replace(sleep_quality, "\\%", "") %>% as.numeric,
         day_of_week = as.Date(Start) %>% wday(label = TRUE),
         what_day = ifelse(grepl("Sat|Sun", day_of_week),"Weekend","Weekday") %>%
                                   as.factor(),
         sleep_hour = ymd_hms(Start) %>% hour(),
         sleep_min = ymd_hms(Start) %>% minute()/60,
         sleep_time = sleep_hour + sleep_min,
         time_hr = period_to_seconds(hms(time_in_bed))/3600,
         date = ymd_hms(Start), 
         day = day(date), 
         month = month(date), 
         year = year(date)) %>% 
  select(date, year, month, day, day_of_week,what_day,sleep_time, Start:time_in_bed, time_hr,steps) %>% 
  mutate_at(vars(year:day),as.factor)

## turn date into 2018-07-12 to match with steps data frame
df$date <- gsub("\\ .*","",df$date)
##turn steps$date into character to match with df
steps$date <- as.character(steps$date)
## change colume name of step into step_2 to avoid contradiction with the step column in df
colnames(steps) <- c("date","steps_2")
## sort the steps data from by date for easier join: d[order(as.Date(d$V3, format="%d/%m/%Y")),]
steps <- steps[order(as.Date(steps$date, format="%Y/%m/%d"), decreasing = FALSE),]
## merge the two datasets
df <- merge(df, steps)
## use the steps_2 column
```

```{r}
#install.packages("viridis")

library(viridis)

df %>% filter(time_hr > 0) %>% 
  ggplot(aes(x = Start, y = time_hr, color = sleep_quality)) +
  geom_point(alpha = 0.6) + 
#  geom_smooth() +
  scale_color_viridis() +
  geom_hline(yintercept=mean(df$time_hr), linetype="dashed", color = "red") +
  labs(title = "Hours of Sleep",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Date",
       y = "Hours of Sleep",
       color = "Sleep Quality\n") +
  theme_bw()
```

***
To avoid the wrong data skewing the relationship, for this chart and those charts after this, I excluded one data point where sleep duration was zero, which I think was an error due to app glitch.

On average, I sleep for a little less than 6 hours everyday.

### Bedtime

```{r}
df %>% 
  ggplot(aes(x = Start, y = sleep_time, color = what_day)) +
  geom_point(alpha = 0.6) +
  scale_y_continuous(expand = c(0,0),
                     breaks = seq(0,24,4),
                     limits = c(0,24.1),
                    labels = c("12 AM","4 AM","8 AM", "12 PM", "4 PM","8 PM","11:59 PM")) +
  labs(title = "Bedtime",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Date",
       y = "Bedtime",
       color = "") +
  theme_bw() +
  geom_hline(yintercept=2, linetype="dashed", color = "red")
```

***
I went to bed later than 2 am for 7 days out of the 19 days with data recorded.

### Sleep Quality vs Hours of Sleep

```{r}
df %>%
  filter(time_hr > 0) %>% 
  ggplot(aes(x = time_hr, y = sleep_quality, color = what_day)) +
  geom_point(size = 2, alpha = 0.3) +
  geom_smooth(aes(group = 1), method = "lm") +
  scale_x_continuous(breaks = seq(0,12,2)) +
  labs(title = "Sleep Quality vs. Hours of Sleep",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Hours of Sleep",
       y = "Sleep Quality",
       color = "") +
  theme_bw()
```

***
The longer the sleep, the higher the sleep quality. The hours of sleep seems to have a moderate positive correlation with sleep quality.

### Duration of Sleep Each Day of the Week
```{r}
#install.packages("ggridges")

library(ggridges)
library(ggplot2)

df %>% filter(time_hr > 0) %>% 
  ggplot(aes(x = time_hr, y = day_of_week, fill = ..x..)) +
  geom_density_ridges_gradient(scale = 3) +
  scale_x_continuous(expand = c(0.01, 0),
                     breaks= seq(0,12,4)) +
  scale_y_discrete(expand = c(0.01, 0)) +
  scale_fill_viridis(name = "Hours", option = "C") +
  labs(title = "Duration of Sleep",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Time spent sleeping (hr)") + 
  theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank()) 
```

***
There was one Thursday and one Wednesday without data entry. Without Thursday and Wednesday, Sunday is the day when I sleep the longest

### Quality of Sleep Each Day of the Week
```{r}
library(ggridges)
library(ggplot2)


df %>% 
  filter(time_hr > 0) %>% 
  ggplot(aes(x = sleep_quality, y = day_of_week, fill = ..x..)) +
  geom_density_ridges_gradient(scale = 3) +
  scale_x_continuous(expand = c(0.01, 0)) +
  scale_y_discrete(expand = c(0.01, 0)) +
  scale_fill_viridis(name = "Quality of Sleep", option = "C") +
  labs(title = 'Quality of Sleep Each Day of the Week',
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)") +
  theme_ridges(font_size = 13, grid = TRUE) + theme(axis.title.y = element_blank())
```

***
Without Thursday and Wednesday, I have higher sleep quality on Saturdays and Sundays.

### Sleep Duration vs Daily Steps

```{r}
df %>%
  filter(steps > 0,
         steps < 15000) %>% 
  filter(time_hr > 0) %>% 
  ggplot(aes(x = steps_2, y = time_hr, color = sleep_quality)) +
  geom_point(alpha = 0.9) +
  geom_smooth(color = "Purple", fill = "White", method = "lm") +
  scale_color_viridis() +
  scale_y_continuous(breaks = seq(0,12,2)) +
  labs(title = "Sleep Duration vs Daily Steps",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Number of Steps",
       y = "Sleep Duration (hr)",
       color = "Sleep Quality\n") +
  theme_dark()
```

***
I mostly take between 2000 and 3000 steps a day, and on these days I sleep between 4 and 7 hours.
When I have really low number of steps, my sleep hour tends to be longer, I think that's because those days are weekends, when I stay at home, and sleep earlier.
Although the linear regression line suggests weak to moderate negative correlation between number of steps and sleep duration, I don't think there are enough data points to say.

### Sleep Duration vs Minite on Phone
```{r}
## select the first 3 columns
moment <- moment[,1:3]
## turn moment$date to match sleep data df
moment$date <- as.character(moment$date)
## sort the steps data from by date for easier join: d[order(as.Date(d$V3, format="%d/%m/%Y")),]
moment <- moment[order(as.Date(moment$date, format="%Y/%m/%d"), decreasing = FALSE),]
## merge the two datasets
df <- merge(df, moment)
```


```{r}
df %>%
  filter(steps > 0,
         steps < 15000) %>% 
  filter(time_hr > 0) %>% 
  ggplot(aes(x = minuteCount, y = time_hr, color = sleep_quality)) +
  geom_point(alpha = 0.9) +
  geom_smooth(color = "Purple", fill = "White", method = "lm") +
  scale_color_viridis() +
  scale_y_continuous(breaks = seq(0,12,2)) +
  labs(title = "Sleep Duration vs Minite on Phone",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Minite Count on Phone",
       y = "Sleep Duration (hr)",
       color = "Sleep Quality\n") +
  theme_dark()
```

***
There seems to be weak positive correlation between the time I spend on phone and sleep duration. But it could again be because I haven't collected enough data.

### Sleep Quality vs. Minutes on Phone
```{r}
df %>% filter(time_hr > 0) %>% 
  ggplot(aes(x = minuteCount, y = sleep_quality, color = what_day)) +
  geom_point(size = 2, alpha = 0.3) +
  geom_smooth(aes(group = 1), method = "lm") +
  scale_x_continuous(breaks = seq(0,12,2)) +
  labs(title = "Sleep Quality vs. Minutes on Phone",
       subtitle = "Source: iPhone sleep cycle data (2018/07/12 - 2018/07/31)",
       x = "Minites on Phone",
       y = "Sleep Quality",
       color = "") +
  theme_bw()
```

***
There seems to be weak positive correlation between the time I spend and sleep quality.