## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, comment = "#>", message = FALSE, warning = FALSE,
  fig.width = 7, fig.height = 5.5
)
library(idiographic)
data(srl)
has_cograph <- requireNamespace("cograph", quietly = TRUE)

## ----vars-audit---------------------------------------------------------------
vars <- c("efficacy", "value", "planning", "monitoring", "effort")
preprocess(srl, vars = vars, id = "name", subject = "Grace")

## ----fit-rolling-var----------------------------------------------------------
rolling_ols <- fit_rolling_var(
  srl, vars = vars, id = "name", subject = "Grace",
  window_size = 50, step = 20, scale = TRUE, keep_fits = TRUE
)
rolling_ols

## ----fit-rolling-gvar---------------------------------------------------------
rolling_gvar <- fit_rolling_graphical_var(
  srl, vars = vars, id = "name", subject = "Grace",
  window_size = 70, step = 25, n_lambda = 8, gamma = 0,
  keep_fits = TRUE
)
rolling_gvar

## ----rolling-table------------------------------------------------------------
head(as.data.frame(rolling_ols), 12)

## ----rolling-matrices---------------------------------------------------------
matrices(rolling_ols, fit = 1)

## ----rolling-gvar-table-------------------------------------------------------
edge_counts <- do.call(rbind, lapply(seq_along(rolling_gvar$fits), function(i) {
  data.frame(window = i, summary(rolling_gvar$fits[[i]]))
}))
edge_counts
edges(rolling_gvar$fits[[1]])
matrices(rolling_gvar, fit = 1)

## ----plot-rolling-var-all, eval=has_cograph-----------------------------------
plot(rolling_ols, fit = 1)

## ----plot-rolling-var-temporal, eval=has_cograph------------------------------
plot(rolling_ols, fit = 1, layer = "temporal")

## ----plot-rolling-var-contemporaneous, eval=has_cograph-----------------------
plot(rolling_ols, fit = 1, layer = "contemporaneous")

## ----plot-rolling-gvar, eval=has_cograph--------------------------------------
plot(rolling_gvar, fit = 1, layer = "contemporaneous")

