library(tidyverse)
library(lubridate)
library(esmerald)

Data preparation

Importing state data

Character vector of csv files

csv <- dir("../inst/extdata/",full.names = T)[2:9]

Reading a single file

ESM_1 <- 
  read_ethica_csv("../inst/extdata/survey_responses_5716.csv")

The result is a table of class tbl_esm.

class(ESM_1)

But the fact that we use S3 classes shouldn't bother you. It just makes it easier for us to create a convenient interface. For example, if you just call the ESM_1 object like this:

ESM_1

you get a useful summary of the data set:

ESM_1

ESM_1 is in long format. Every measure gets its own row. The long format also makes it very easy to combine a group of files into one ESM table. If you know a little programming already, you might think that processing a list of objects requires a loop. In R, this is much easier. map_df takes a list of file names as input, applies the function read_ethica_csv a number of times and merges the results into one ESM table.

ESM_2 <- 
  csv[2:6] %>% 
  map_df(read_ethica_csv)
ESM_2

Traits and demographics

Trait and demographics data are stored in a participant table with the following properties:

D_Parts <- 
  read_csv(csv[1]) %>% 
  rename(Part = Name, 
         Device = `Device ID`)

colnames(D_Parts) <- 
  c("Part", "Device", 
    "T_Scheduled", "T_Issued", "T_Response", "duration", 
    "Location", "Informed_consent", "Gender", "Nationality", "Nationality_free", "age")

D_Parts <- D_Parts %>% 
  select(Part, Location:age)

D_Parts

Scales meta data

We export a table to add the meta data:

ESM_2 %>% 
  export_items_template() %>% 
  write_csv(file = "ESM_2_Scales.csv")

When the Items table is complete, it can be joined with the ESM data, and the responses can easily be normalized (reversing items and scaling to unit interval)

After editing, we augment the Ethica table with meta data. The result is a tbl_esm object.

D_Items <- 
  read_csv("../inst/extdata/ESM_2_Scales.csv") %>% 
  select(-Activity)

ESM_2_1 <- 
  ESM_2 %>% 
  augment(Parts = D_Parts,
          Items = D_Items)


ESM_2_1
ESM_2_1 %>% 
  lm(response ~ Gender, data = .)

Data manipulation

ESM_2_1 %>% 
  ggplot(aes(x = T_pos, y = response)) +
  geom_count() +
  geom_smooth() +
  facet_wrap(~Part)

Reporting

We compute the mean response per participant and do correlations

ESM_2_1 %>% 
  group_by(Part, Scale) %>% 
  summarize(mean_resp = mean(response, na.rm = T)) %>% 
  ungroup() %>% 
  pivot_wider(names_from = Scale, values_from = mean_resp) %>% 
  select(-Part) %>% 
  GGally::ggpairs()

We do a mean response per Part, Scale and T_Scheduled and plot a time series. You need package mascutils from Github (restart your R session, before you install):

devtools::install_github("schmettow/mascutils")
ESM_2_1 %>% 
  filter(str_detect(Scale, "^state")) %>% 
  group_by(Part, Scale, T_Scheduled) %>% 
  summarize(mean_resp = mean(response, na.rm = T)) %>% 
  ungroup() %>% 
  ggplot(aes(x = T_Scheduled,
             y = mean_resp,
             col = Scale)) +
  geom_jitter(alpha = .3) +
  geom_smooth(se = F) +
  facet_wrap(Part ~ ., scales = "free_x")

Modelling



schmettow/esmerald documentation built on April 3, 2021, 1:43 a.m.