---
title: "Examples: Recency and Decay with Online Beta Update"
author: "Philip I. Pavlik Jr."
date: "2026-06-08"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Examples: Recency and Decay with Online Beta Update}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup}
source("lkt-vignette-setup.R")
knitr::opts_chunk$set(eval = run_expensive_vignette)
online_calibration_options <- list(maxit = 50, factr = 1e7)

print_online_parameters <- function(label, model) {
  cat("\nONLINE BETA UPDATE PARAMETERS:", label, "\n")
  cat("alpha:", model$model$alpha, "\n")
  if (!is.null(model$model$optimizer)) {
    cat("optimizer convergence:", model$model$optimizer$convergence, "\n")
    cat("optimizer value:", model$model$optimizer$value, "\n")
  }
  cat("coefficient rows:", nrow(model$coefs), "\n")
  print(head(model$coefs, 20))
  if (!is.null(model$optimizedpars) &&
      !(length(model$optimizedpars) == 1L && is.na(model$optimizedpars))) {
    cat("nonlinear optimized parameters:\n")
    print(model$optimizedpars$par)
  }
  invisible(model)
}
```

The full online-calibration comparison is intentionally not evaluated during
routine CRAN checks. Set `LKT_RUN_EXPENSIVE_VIGNETTES=true` to execute it when
rendering.

# Load data

```{r bundled-data}
val <- prepare_largeraw_sample()
```

# Recent Performance Factors Analysis

```{r rpfa-propdec2}
model_rpfa <- LKT(
  data = val,
  interc = TRUE,
  components = c(
    "Anon.Student.Id",
    "KC..Default.",
    "KC..Default.",
    "KC..Default."
  ),
  features = c("intercept", "intercept", "propdec2", "linefail"),
  fixedpars = c(0.372666739924378),
  model = "online_calibration",
  model_options = online_calibration_options
)
print_online_parameters("Recent Performance Factors Analysis", model_rpfa)
check_true("fixed propdec2 parameter used", identical(model_rpfa$model_name, "OnlineCalibration"))
```

# Recency tracing with logitdec

```{r logitdec-recency}
model_logitdec_recency <- LKT(
  data = val,
  interc = TRUE,
  components = c(
    "Anon.Student.Id",
    "KC..Default.",
    "KC..Default.",
    "KC..Default."
  ),
  features = c("intercept", "intercept", "logitdec", "recency"),
  fixedpars = c(.9, .5),
  model = "online_calibration",
  model_options = online_calibration_options
)
print_online_parameters("Recency tracing with logitdec", model_logitdec_recency)
check_has_coefficients(model_logitdec_recency, c("logitdecKC..Default.", "recencyKC..Default."))
```

# Recency tracing with transfer from cluster

```{r logitdec-recency-transfer}
model_logitdec_transfer <- LKT(
  data = val,
  interc = TRUE,
  components = c(
    "Anon.Student.Id",
    "KC..Default.",
    "KC..Default.",
    "KC..Default.",
    "KC..Cluster."
  ),
  features = c("intercept", "intercept", "logitdec", "recency", "logitdec"),
  fixedpars = c(.9, .5, .5),
  model = "online_calibration",
  model_options = online_calibration_options
)
print_online_parameters("Recency tracing with transfer from cluster", model_logitdec_transfer)
check_has_coefficients(model_logitdec_transfer, c("logitdecKC..Cluster."))
```

# Performance Prediction Equation

```{r ppe}
model_ppe <- LKT(
  data = val,
  interc = TRUE,
  components = c(
    "Anon.Student.Id",
    "KC..Default.",
    "KC..Default.",
    "KC..Default."
  ),
  features = c("intercept", "intercept", "ppe", "logitdec"),
  fixedpars = c(0.3491901, 0.2045801, 1e-05, 0.9734477, 0.4443027),
  model = "online_calibration",
  model_options = online_calibration_options
)
print_online_parameters("Performance Prediction Equation", model_ppe)
check_has_coefficients(model_ppe, c("ppeKC..Default.", "logitdecKC..Default."))
```

# base4

```{r base4}
model_base4 <- LKT(
  data = val,
  interc = TRUE,
  components = c(
    "Anon.Student.Id",
    "KC..Default.",
    "KC..Default.",
    "KC..Default."
  ),
  features = c("intercept", "intercept", "base4", "logitdec"),
  fixedpars = c(0.1890747, 0.6309054, 0.05471752, .5, 0.2160748),
  model = "online_calibration",
  model_options = online_calibration_options
)
print_online_parameters("base4", model_base4)
check_has_coefficients(model_base4, c("base4KC..Default.", "logitdecKC..Default."))
```
