---
title: "Description of lcmm package"
output: 
  rmarkdown::html_vignette:
    toc: true # table of content true
    toc_depth : 2  
vignette: >
  %\VignetteIndexEntry{Introduction}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---




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

# Functionalities of the package  

<p align="justify"> The R package lcmm provides a series of functions to estimate statistical models based on the linear mixed model theory. It includes the estimation of:

- mixed models and latent class mixed models for Gaussian longitudinal outcomes (hlme),
- curvilinear and ordinal univariate longitudinal outcomes (lcmm)
- curvilinear multivariate outcomes (multlcmm)
- joint latent class mixed models (jlcmm and mpjlcmm) for (Gaussian and/or curvilinear) longitudinal outcomes and a time-to-event that can be possibly left-truncated right-censored and defined in a competing setting. 


<p align="justify">The package lcmm includes 5 main functions: lcmm, hlme, multlcmm, jlcmm and mpjlcmm. Each function is described in a different vignette. These functions apply to longitudinal markers, even those that have limited metrological properties but the functions can also be used in cross-sectional context.</p>


For each model, maximum likelihood estimators are obtained using a modified Marquardt algorithm with strict convergence criteria based on the parameters and likelihood stability, and on the negativity of the second derivatives. The algorithm is implemented in the R package marqLevAlg with parallel computation. </p>

The package also provides various post-fit functions including goodness-of-fit analyses, classification, plots, predicted trajectories, individual dynamic prediction of the event and predictive accuracy assessment. </p>

Any question question or issue should be addressed on the github page: https://github.com/CecileProust-Lima/lcmm/issues </p>


# Calls functions

Each function is precisely introduced in the companion paper (Proust-Lima, JSS 2017 - https://doi.org/10.18637/jss.v078.i02).

## hlme

This function is used for standard linear mixed models and their extension to multiple profiles of trajectory : the latent class linear mixed models 

```{r, eval=FALSE}
hlme(fixed, mixture, random, subject, classmb, ng = 1, idiag = FALSE, nwg = FALSE, cor = NULL, data, B, convB = 0.0001, convL = 0.0001, convG = 0.0001, prior, maxiter = 500, subset = NULL, na.action = 1, posfix = NULL)
```


## lcmm 

This function is used to estimate mixed models when the outcome does not have a Gaussian distribution. The method is called "latent process model". It currently handles curvilinear or non-Gaussian continuous markers using continuous link functions, and binary and ordinal markers using the probit framework with piecewise constant link functions. As hlme, the method also handles heterogeneous profiles of trajectories in a "latent process and latent class mixed model". 

```
lcmm(fixed, mixture, random, subject, classmb, ng = 1, idiag = FALSE, nwg = FALSE, link = "linear", intnodes = NULL, epsY = 0.5, cor = NULL, data, B, convB = 1e-04, convL = 1e-04, convG = 1e-04,  maxiter = 100, nsim = 100, prior, range = NULL, subset = NULL, na.action = 1, posfix = NULL,  partialH = FALSE)
``` 

## multlcmm

This function extends lcmm function when more than one marker measures the same underlying construct of interest. The function handles the same natures of markers as lcmm (possibly mixed into the same model): curvilinear or non-Gaussian continuous markers using continuous link functions, and binary and ordinal markers using the probit framework with piecewise constant link functions. With only binary/ordinal markers, multlcmm defines a continuous-time longitudinal IRT model. In addition, as other functions, multlcmm handles either homogeneous or heterogeneous profiles of trajectories.

```
multlcmm(fixed, mixture, random, subject, classmb, ng = 1, idiag = FALSE, nwg = FALSE, randomY = FALSE, link = "linear", intnodes = NULL, epsY = 0.5, cor = NULL, data, B, convB = 1e-04, convL = 1e-04, convG = 1e-04, maxiter = 100, nsim = 100, prior, range = NULL, subset = NULL, na.action = 1, posfix = NULL, partialH = FALSE)
``` 


## jlcmm 

This function extends hlme, lcmm and multlcmm to jointly analyze times to event (in a competing setting). The latent structure shared between the longitudinal outcome(s) and the time(s) to event is latent classes. 

```
Jointlcmm(fixed, mixture, random, subject, classmb, ng = 1, idiag = FALSE, nwg = FALSE, survival, hazard = "Weibull", hazardtype = "Specific", hazardnodes = NULL, TimeDepVar = NULL, link = NULL, intnodes = NULL, epsY = 0.5, range = NULL, cor = NULL, data, B, convB = 1e-4, convL = 1e-4, convG = 1e-4, maxiter = 100, nsim = 100, prior, logscale = FALSE, subset = NULL, na.action = 1, posfix = NULL, partialH = FALSE)
``` 



## mpjlcmm 

This function is a multivariate version of jlcmm: it models multivariate longitudinal markers (possibly regrouped in several latent processes), and may also jointly analyze times to event (in a competing setting). The latent structure shared between the latent processes and the time(s) to event is latent classes. 

```
mpjlcmm <- function(longitudinal,subject,classmb,ng,survival,
      hazard="Weibull",hazardtype="Specific",hazardnodes=NULL,TimeDepVar=NULL,
      data,B,convB=0.0001,convL=0.0001,convG=0.0001,maxiter=100,nsim=100,
      prior,logscale=FALSE,subset=NULL,na.action=1,posfix=NULL,
      partialH=FALSE,verbose=TRUE,nproc=1,clustertype=NULL)
``` 

# Dataset example

## paquid subsample

<p align="justify">

In the wikis, lcmm is illutrated with paquid dataset. This is a subsample of 500 subjects from the original French prospective study Paquid. This dataset can not be used for epidemiological purposes as the subsample is not representative of the original cohort (in particular, dementia cases have been over sampled).  

The data are in the longitudinal format including some variables such as 3 psychometric tests $MMSE$, $BVRT$, $IST$, the depressive symptomatology scale $CESD$ and the variables $age$, $agedem$ (age until dementia), $dem$ (boolean with 1 for dementia), $ageinit$, $CEP$ (education level) and $male$.</p>

&nbsp;

For visualizing the data (head of the table only):

&nbsp;

```{r,comment=''}
head(paquid)
```

The different markers are collected at different times. In the dataset the timescale is $age$.

&nbsp;

For obtaining a quick summary of the data:

&nbsp;

```{r,comment=''}
summary(paquid)
```

Some variables have missing values. This is not a problem when using lcmm package as missing values are systematically removed. 

&nbsp;

## MMSE outcome

In the lcmm examples, MMSE is usually considered as the outcome. MMSE is a very common neuropsychological test to measure global cognitive functioning in the elderly. It has a very asymmetric distribution so that it is usually normalized to be applied with methods for Gaussian variables. This is done using a pre-normalization function dedicated to MMSE provided in NormPsy package:   


```{r, results='hide',message=FALSE,warning=FALSE, echo=FALSE}
library(lcmm)
```

&nbsp;

```{r,comment='', results='hide'}
library(NormPsy)
``` 

```{r,comment='', fig.height=4, fig.width=6}
paquid$normMMSE <- normMMSE(paquid$MMSE)
par(mfrow=c(1,2))
hist(paquid$MMSE, cex.main=0.8, cex.lab=0.8)
hist(paquid$normMMSE, cex.main=0.8, cex.lab=0.8)
```

&nbsp;

The individual repeated measures of MMSE to be modelled are:

&nbsp;

```{r,comment='', fig.height=4, fig.width=6}
library(lattice)
color <- paquid$ID
xyplot(normMMSE ~ age, paquid, groups = ID, col=color, lwd=2, type="l")
```

&nbsp;


