---
title: "A-quick-tour-of-mixRHLP"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{A-quick-tour-of-mixRHLP}
  %\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(flamingos)
```

# Introduction

**mixRHLP**: Simultaneous model-based clustering and segmentation of 
heterogeneous and dynamical functional data (curves/times series) with smooth 
and/or abrupt changes in regime by a mixture of regression models with hidden 
logistic processes, fitted by the EM algorithm and extensions.

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

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

# Load data

```{r}
data("toydataset")
x <- toydataset$x
Y <- t(toydataset[,2:ncol(toydataset)])
```

# Set up mixRHLP model parameters

```{r}
K <- 3 # Number of clusters
R <- 3 # Number of regimes (polynomial regression components)
p <- 1 # Degree of the polynomials
q <- 1 # Order of the logistic regression (by default 1 for contiguous segmentation)
variance_type <- "heteroskedastic" # "heteroskedastic" or "homoskedastic" model
```

# Set up EM parameters

```{r}
n_tries <- 1
max_iter <- 1000
threshold <- 1e-5
verbose <- TRUE
verbose_IRLS <- FALSE
init_kmeans <- TRUE
```

# Estimation

```{r, echo=TRUE}
mixrhlp <- emMixRHLP(X = x, Y = Y, K, R, p, q, variance_type, init_kmeans, 
                     n_tries, max_iter, threshold, verbose, verbose_IRLS)
```

# Summary

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

# Plots

## Estimated signal

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

## Regressors

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

## Log-likelihood

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