## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
# options(scipen = 10)

## ----setup, message = FALSE---------------------------------------------------
library(mlstats)
library(dplyr)
library(lme4)
library(lmerTest)

## ----data---------------------------------------------------------------------
data("media_diary")

## ----decompose-basic----------------------------------------------------------
media_diary |>
  decompose_within_between(group = "person", vars = "screen_time") |>
  select(starts_with("screen_time_"))

## ----decompose-components-----------------------------------------------------
media_diary |>
  decompose_within_between(
    group = "person",
    vars = "screen_time",
    components = c("within", "between", "gmc")
  ) |>
  select(starts_with("screen_time"))

## ----decompose-names----------------------------------------------------------
media_diary |>
  decompose_within_between(
    group = "person",
    vars = c("screen_time"),
    components = c("within", "between"),
    within_pattern = "{col}_wg",
    between_pattern = "{col}_bg"
  ) |>
  select(starts_with("screen_time"))

## ----decompose-multi----------------------------------------------------------
media_diary |>
  decompose_within_between(
    group = "person",
    vars = c("screen_time", "stress"),
    components = c("within", "between")
  ) |>
  glimpse()

## ----rewb-model-base----------------------------------------------------------
diary_decomp <- decompose_within_between(
  data            = media_diary,
  group           = "person",
  vars            = "screen_time",
  components      = c("within", "between"),
  within_pattern  = "{col}_within",
  between_pattern = "{col}_between"
)

fit_rewb <- lmer(
  wellbeing ~ screen_time_within + screen_time_between + (1 | person),
  data = diary_decomp
)

summary(fit_rewb, correlation = FALSE)

## ----naive-model--------------------------------------------------------------
fit_naive <- lm(wellbeing ~ screen_time, data = diary_decomp)
summary(fit_naive)

## ----rewb-model-confounded----------------------------------------------------
fit_rewb_conf <- lmer(
  wellbeing ~ screen_time_within + screen_time_between + self_control +
    (1 | person),
  data = diary_decomp
)

summary(fit_rewb_conf, correlation = FALSE)

## ----multi-predictor, eval = FALSE--------------------------------------------
# diary_decomp2 <- decompose_within_between(
#   data            = media_diary,
#   group           = "person",
#   vars            = c("screen_time", "stress"),
#   components      = c("within", "between"),
#   within_pattern  = "{col}_within",
#   between_pattern = "{col}_between"
# )
# 
# fit_multi <- lmer(
#   wellbeing ~ screen_time_within + screen_time_between +
#     stress_within + stress_between +
#     self_control + (1 | person),
#   data = diary_decomp2
# )

## ----random-slopes, eval = FALSE----------------------------------------------
# fit_slopes <- lmer(
#   wellbeing ~ screen_time_within + screen_time_between + self_control +
#     (screen_time_within | person),
#   data = diary_decomp
# )

