---
title: "Transition Networks and Higher-Order Models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Transition Networks and Higher-Order Models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(gp3sequences)
```

## Synthetic navigation sequences

```{r data}
paths <- list(
  s1 = c("home", "search", "product", "checkout"),
  s2 = c("home", "search", "product", "home"),
  s3 = c("home", "category", "product", "checkout"),
  s4 = c("home", "category", "home", "search")
)
sequence_data <- do.call(rbind, lapply(seq_along(paths), function(i) {
  data.frame(sequence_id = names(paths)[i],
             sequence_order = seq_along(paths[[i]]),
             state = paths[[i]], stringsAsFactors = FALSE)
}))
```

## First-order transition network

```{r network}
network <- create_transition_network(
  sequence_data,
  normalise = "from",
  include_self = TRUE
)
network
summarise_transition_centrality(network)
detect_transition_communities(network)
```

Centrality values are graph-structural descriptors. They do not independently
measure attention, importance, intent, or influence.

## Higher-order transition model

```{r higher-order}
model <- fit_higher_order_transition_model(
  sequence_data,
  order = 2L,
  smoothing = 0.5,
  backoff = TRUE
)
predict_next_state(model, c("home", "search"))
predict_next_state(model, c("unseen"))
```

## Whole-sequence bootstrap

```{r bootstrap}
boot <- bootstrap_transition_network(
  sequence_data,
  n_boot = 20L,
  level = 0.95,
  seed = 8L
)
head(boot)
```

Bootstrap samples are drawn at the sequence level, preserving within-sequence
transition dependence.
