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

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

## ----data---------------------------------------------------------------------
data("media_diary")
vars <- c("self_control", "wellbeing", "screen_time", "stress")

## ----correlations, warning = FALSE--------------------------------------------
within_between_correlations(
  data  = media_diary,
  group = "person",
  vars  = vars
)

## ----diverging, warning = FALSE-----------------------------------------------
within_between_correlations(
  data  = media_diary,
  group = "person",
  vars  = c("wellbeing", "screen_time")
)

## ----naive-cor----------------------------------------------------------------
cor(media_diary$screen_time, media_diary$wellbeing)

## ----method-options-1, warning = FALSE, eval = FALSE--------------------------
# within_between_correlations(
#   data   = media_diary,
#   group  = "person",
#   vars   = vars,
#   method = "sem" # or "bayes"
# )

## ----method-options-2, warning = FALSE, eval = FALSE--------------------------
# within_between_correlations(
#   data   = media_diary,
#   group  = "person",
#   vars   = vars,
#   weight = FALSE
# )

## ----mldesc, warning = FALSE--------------------------------------------------
result <- mldesc(
  data  = media_diary,
  group = "person",
  vars  = vars
)

result

## ----mldesc-options, warning = FALSE------------------------------------------
mldesc(
  data                = media_diary,
  group               = "person",
  vars                = vars,
  significance        = "detailed",  # *, **, *** for p < .05, .01, .001
  flip                = TRUE,        # between above diagonal, within below
  remove_leading_zero = FALSE        # keep "0.45" instead of ".45"
)

## ----pipe-friendly------------------------------------------------------------
result_num <- result[c(1, 6:10)]
result_num[-1] <- lapply(result[6:10], as.numeric)
as_tibble(result_num)

## ----bayes-example, warning = FALSE, eval = FALSE-----------------------------
# mldesc(
#   data   = media_diary,
#   group  = "person",
#   vars   = vars,
#   method = "bayes",
#   folder = "brms_models",
#   ci     = 0.95
# )

