---
title: "Model-selection-MRHLP"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Model-selection-MRHLP}
  %\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

In this package, it is possible to select models based on information criteria 
such as **BIC**, **AIC** and **ICL**.

The selection is done on two parameters which are:

 * $K$: The number of regimes;
 * $p$: The order of the polyniomial regression.

# Data

Let's select a MRHLP model for the following multivariate time series $Y$:

```{r}
data("multivtoydataset")
x <- multivtoydataset$x
y <- multivtoydataset[, c("y1", "y2", "y3")]
matplot(x, y, type = "l", xlab = "x", ylab = "Y", lty = 1)
```

# Model selection with BIC

```{r}
selectedmrhlp <- selectMRHLP(X = x, Y = y, Kmin = 2, Kmax = 6, pmin = 0, pmax = 3)
```

The selected model has $K = 5$ regimes and the order of the polynomial 
regression is $p = 0$. According to the way $Y$ has been generated, these 
parameters are what we expected.

Let's summarize the selected model:

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

```{r}
selectedmrhlp$plot(what = "estimatedsignal")
```
