4  Getting Started

All of our examples will be based on a common data set. We’ll begin by creating it and explaining how it works.

Let’s say you are suffering from unexplained headaches that appear somewhat randomly. You suspect they may be associated with something you eat, but you’re not sure, so you’ve been tracking 14 weeks (98 days) worth of your own data in a spreadsheet that looks like this:

Code
library(tidyverse, quietly=TRUE)
library(lubridate, quietly=TRUE)
Code
set.seed(1984)

x <- tibble(date=seq(from = today()-weeks(14),
                     by = "1 day", length.out = 7*14),
            headache = sample(c(TRUE,FALSE), 7*14,
                              prob = c(.05,.95),
                              replace = TRUE))

knitr::kable( head(x) ) %>% kableExtra::kable_styling()
date headache
2022-07-19 FALSE
2022-07-20 FALSE
2022-07-21 FALSE
2022-07-22 FALSE
2022-07-23 FALSE
2022-07-24 FALSE
Code
write_csv(x,"headache-days.csv")

You can download a copy of this file here

It’s easy to add a few more variables (columns) to the dataframe: (download)

Code
z <- function(x){
m = NULL
for(i in 1:14){
  m = c(c(rep(0,6),
              floor(runif(1,min=0,max=3))),
        m)
}

m
}
x <- tibble(date=seq(from = today()-weeks(14),
                     by = "1 day", length.out = 7*14),
            headache = sample(c(TRUE,FALSE), 7*14,
                              prob = c(.05,.95),
                              replace = TRUE),
            icecream = sample(c(TRUE,FALSE), 7*14,
                              prob = c(.10,.90),
                              replace = TRUE),
            z = runif(7*14, min = -2.5, max = .5) + 8,
            wine = z(0))

knitr::kable( head(x,10), digits = 2) %>%
  kableExtra::kable_styling(bootstrap_options = c("striped", "hover", "condensed"))
date headache icecream z wine
2022-07-19 FALSE TRUE 7.56 0
2022-07-20 FALSE FALSE 7.38 0
2022-07-21 FALSE FALSE 5.51 0
2022-07-22 FALSE TRUE 7.60 0
2022-07-23 FALSE FALSE 8.36 0
2022-07-24 FALSE FALSE 6.92 0
2022-07-25 FALSE FALSE 6.32 0
2022-07-26 FALSE FALSE 7.52 0
2022-07-27 FALSE FALSE 7.95 0
2022-07-28 FALSE FALSE 6.99 0
Code
write_csv(x,"headache-variables.csv")