---
title: "Getting started with segen"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting started with segen}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4)
library(segen)
```

`segen` forecasts numeric or categorical sequences by comparing recent
windows with historical windows and aggregating similar sequences.

## Numeric example

The package includes a small example dataset. The following deliberately uses
one validation window and one sampled model so that the vignette runs quickly.

```{r numeric}
set.seed(123)
fit <- segen(
  time_features[, "IBM.Close", drop = FALSE],
  seq_len = 12,
  similarity = 0.7,
  n_windows = 2,
  n_samp = 1,
  seed = 123
)

head(fit$history)
fit$best_model$predictions$IBM.Close
```

The result contains the model search history, the selected model's
predictions and testing errors, plots, and elapsed-time information.

```{r plot, fig.alt="Forecast with uncertainty interval"}
fit$best_model$plots$IBM.Close
```

## Reproducibility

Set `seed` whenever results need to be reproduced. This controls model
sampling and uncertainty draws. `segen` runs sequentially by default.

## Distance methods and parallel execution

All six distance methods, including DTW with the symmetric2 recurrence,
are implemented using standard R libraries. Parallel execution uses PSOCK
workers from the standard parallel package, with two workers by default.

```{r parallel, eval=FALSE}
fit_parallel <- segen(time_features[, "IBM.Close", drop = FALSE],
                     seq_len = 12, n_samp = 4,
                     use_parallel = TRUE, parallel_workers = 2)
```

## Changes in 2.0.1

There are no contributed runtime dependencies. Plot objects now have class
`segen_plot`; display them with `plot()` or `print()`, rather than adding
ggplot2 layers. Numeric gaps use linear interpolation with constant endpoint
extension; categorical gaps use the most frequent observed level. Smoothing
uses degree-one loess with span 0.75 rather than automatic span selection.
These preprocessing changes can change forecasts. Binary entropy now measures
the empirical distribution of zero/one outcomes in natural-log units.
Percentage metrics use percentages and denominators are bounded below by 1e-8.
Intervals use pooled rolling residuals and do not guarantee nominal coverage
under arbitrary temporal dependence.

## Input requirements

Input must be a data frame whose columns are all numeric or all categorical.
Dates, when supplied, must be a `Date` vector with one value per row. Missing
numeric values are imputed before forecasting.
