---
title: "Generate lavaan syntax for latent change score models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Generate lavaan syntax for latent change score models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(lcsm)
```

# Univariate LCSM syntax

The example below shows how to specify a generic univariate latent change score model using the function `specify_uni_lcsm()`.

```{r}
uni_lcsm_syntax <- specify_uni_lcsm(timepoints = 5,
                                    var = "x",  
                                    change_letter = "g",
                                    model = list(alpha_constant = TRUE, 
                                                 beta = TRUE, 
                                                 phi = TRUE))
```


```{r}
# To get a readable output use cat() function
cat(uni_lcsm_syntax)
```

## Add lavaan syntax

```{r}
uni_lcsm_syntax_add <- specify_uni_lcsm(timepoints = 3,
                                    var = "x",  
                                    change_letter = "g",
                                    add = "# JUST ANOTHER COMMENT",
                                    model = list(alpha_constant = TRUE, 
                                                 beta = TRUE, 
                                                 phi = TRUE))
```


```{r}
# To get a readable output use cat() function
cat(uni_lcsm_syntax_add)
```

# Bivariate LCSM syntax

## True Score to Change Score

```{r}
# Specify bivariate LCSM
lavaan_bi_lcsm_delta_01 <- specify_bi_lcsm(timepoints = 5, 
                                           var_x = "x",
                                           model_x = list(alpha_constant = TRUE, 
                                                          beta = TRUE, 
                                                          phi = TRUE),
                                           var_y = "y",  
                                           model_y = list(alpha_constant = TRUE, 
                                                          beta = TRUE, 
                                                          phi = TRUE),  
                                           coupling = list(delta_lag_xy = TRUE, 
                                                           delta_lag_yx = TRUE),
                                           change_letter_x = "g",
                                           change_letter_y = "j")
```


```{r}
# To get a readable output use cat() function
cat(lavaan_bi_lcsm_delta_01)
```


## Change Score to Change Score

```{r}
# Specify bivariate LCSM
lavaan_bi_lcsm_xi_01 <- specify_bi_lcsm(timepoints = 5, 
                                        var_x = "x",
                                        model_x = list(alpha_constant = TRUE, 
                                                       beta = TRUE, 
                                                       phi = TRUE),
                                        var_y = "y",  
                                        model_y = list(alpha_constant = TRUE, 
                                                       beta = TRUE, 
                                                       phi = TRUE),  
                                        coupling = list(xi_lag_xy = TRUE, 
                                                        xi_lag_yx = TRUE),
                                        change_letter_x = "g",
                                        change_letter_y = "j")
```


```{r}
# To get a readable output use cat() function
cat(lavaan_bi_lcsm_xi_01)
```

## Add model specifications to lavaan code

```{r}
# Specify bivariate LCSM
lavaan_bi_lcsm_xi_add <- specify_bi_lcsm(timepoints = 3, 
                                        var_x = "x",
                                        model_x = list(alpha_constant = TRUE, 
                                                       beta = TRUE, 
                                                       phi = TRUE),
                                        var_y = "y",  
                                        model_y = list(alpha_constant = TRUE, 
                                                       beta = TRUE, 
                                                       phi = TRUE),  
                                        coupling = list(xi_lag_xy = TRUE, 
                                                        xi_lag_yx = TRUE),
                                        add = "age ~~ age_j2 * j2",
                                        change_letter_x = "g",
                                        change_letter_y = "j")
```


```{r}
# To get a readable output use cat() function
cat(lavaan_bi_lcsm_xi_add)
```

# TODOs

- show different examples
- highlight the comments that explain the different sections of the code 
- show how this syntax can be adapted and used outside of this package in lavaan