Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
51 lines (42 sloc) 1.6 KB
---
title: "R Notebook"
output: html_notebook
---
```{r}
library(tidyverse)
library(lubridate)
```
```{r}
df <- momentum_data %>%
filter(X1 == "Meditate") %>%
gather() %>% # this makes it tidier!
mutate(dt = as.Date(key, "%m/%d/%y")) %>% # converting dates properly always takes longer than you'd expect
rename(meditate = value) %>%
select(dt, meditate) %>% # only keeping date and binary meditate here
filter(meditate != "Meditate") # get rid of first row that is left over from the gather()
df %>% write_csv("momentum_data_clean.csv")
```
Add a lag variable to see how previous day predicts the next day
```{r}
df %>%
filter(dt > '2018-08-06') %>%
mutate(lead = lead(meditate)) %>% # what did I do the day before?
group_by(lead) %>%
summarise(meditate_percentage = mean(as.numeric(meditate), na.rm = T)) %>%
filter(! is.na(lead)) %>%
ggplot(aes(x = lead, y = meditate_percentage)) +
geom_col(fill = "navyblue") +
geom_text(label = c("No", "Yes"),
color = "white",
position = position_stack(.5)) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1),
breaks = c(.1, .2, .3, .4, .5, .6, .7)) +
theme(legend.position = "none",
panel.background = element_rect(fill = "white"),
panel.grid.major = element_line(colour = "lightgrey"),
panel.grid.minor = element_blank()) +
scale_x_discrete(breaks = NULL) +
labs(x = "Did I Meditate the Previous Day?",
y = "Probability of Meditating",
title = "I am more than twice as likely to meditate on a given day \nif I meditated the day before")
```
You can’t perform that action at this time.