---
title: "A-quick-tour-of-MHMMR"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{A-quick-tour-of-MHMMR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(
	fig.align = "center",
	fig.height = 5.5,
	fig.width = 6,
	warning = FALSE,
	collapse = TRUE,
	dev.args = list(pointsize = 10),
	out.width = "90%",
	par = TRUE
)
knit_hooks$set(par = function(before, options, envir)
  { if (before && options$fig.show != "none") 
       par(family = "sans", mar = c(4.1,4.1,1.1,1.1), mgp = c(3,1,0), tcl = -0.5)
})
```

```{r, message = FALSE, echo = FALSE}
library(samurais)
```

# Introduction

**MHMMR**: Flexible and user-friendly probabilistic joint segmentation of 
multivariate time series (or multivariate structured longitudinal data) with 
regime changes by a multiple regression model governed by a hidden Markov 
process, fitted by the EM (Baum-Welch) algorithm.

It was written in R Markdown, using the [knitr](https://cran.r-project.org/package=knitr) package for production.

See `help(package="samurais")` for further details and references provided by `citation("samurais")`.

# Load data

```{r}
data("multivtoydataset")
```

# Set up MHMMR model parameters

```{r}
K <- 5 # Number of regimes (states)
p <- 3 # Dimension of beta (order of the polynomial regressors)
variance_type <- "heteroskedastic" # "heteroskedastic" or "homoskedastic" model
```

# Set up EM parameters

```{r}
n_tries <- 1
max_iter <- 1500
threshold <- 1e-6
verbose <- TRUE
```

# Estimation

```{r}
mhmmr <- emMHMMR(multivtoydataset$x, multivtoydataset[,c("y1", "y2", "y3")], 
                 K, p, variance_type, n_tries, max_iter, threshold, verbose)
```

# Summary

```{r}
mhmmr$summary()
```

# Plots

## Predicted time series and predicted regime probabilities

```{r}
mhmmr$plot(what = "predicted")
```

## Filtered time series and filtering regime probabilities

```{r}
mhmmr$plot(what = "filtered")
```

## Fitted regressors

```{r}
mhmmr$plot(what = "regressors")
```

## Smoothed time series and segmentation

```{r}
mhmmr$plot(what = "smoothed")
```

## Log-likelihood

```{r}
mhmmr$plot(what = "loglikelihood")
```
