---
title: "Selection of calendar td regressors"
vignette: >
  %\VignetteIndexEntry{Selection of calendar td regressors}
  %\VignetteEngine{quarto::html}
  %\VignetteEncoding{UTF-8}
  knitr:
  opts_chunk:
    collapse: true
    comment: '#>'
---

```{r}
#| label: setup
#| eval: true
#| echo: false

cond_run_vignette <- rjd3toolkit::get_java_version() >= rjd3toolkit::minimal_java_version

knitr::opts_chunk$set(
    collapse = TRUE,
    echo = TRUE,
    eval = cond_run_vignette,
    comment = "#>"
)
```

```{r}
#| echo: true
#| warning: false
#| label: setup-rjd3production

library("rjd3production")
```

### Regressor selection

To select a set of regressors, use the `select_td()` function:

```{r}
#| echo: true
#| label: "select-td"

library("rjd3toolkit")
td_table <- select_td(ABS[, seq_len(3L)])
print(td_table)
```

### Import/Export

Once the regressors have been selected, you can export the table in `.yaml` format and reimport it later:

```{r}
#| echo: true
#| label: "io-td"

path_td <- tempfile(pattern = "td-table", fileext = ".yaml")
export_td(td_table, path_td)
td_table2 <- import_td(path = path_td)
waldo::compare(td_table, td_table2)
```

### Assignment

Finally, to assign calendar regressors to a workspace, you can use the `assign_td()` function:

```{r}
#| echo: true
#| eval: false
#| label: "assign-td"

library("rjd3workspace")
my_ws <- jws_open("my_workspace")
assign_td(td_table, my_ws)
```

### Advanced selection

By default, the regressors proposed are those used by INSEE (and in the SSP). To use custom regressor sets, simply create a modelling context](https://rjdverse.github.io/rjd3toolkit/reference/modelling_context.html) containing as many variables as you want regressor sets and one regressor set per variable.

For example, for the INSEE regressor sets, here is the structure of the corresponding modelling context:

```{r}
#| echo: true
#| label: "structure-of-context"


str(create_insee_context(), max.level = 2L)
```

Let's imagine that I only want to test three types of sets:

- a first regressor set (`TD2_TB`) containing two regressors: Thursday, week (and weekend in contrast)
- a second set (`TD3_TB`) containing three regressors: Thursday, week (and weekend in contrast) and leap-year regressor
- a third set (`TD6_TB`) that differentiates between all days of the week

We then create our sets of regressors and create the modelling context:

```{r}
#| echo: true
#| label: "custom-td-creation"

series_example <- ABS[, 1L]

TD2_TB <- calendar_td(
    s = series_example,
    groups = c(1L, 1L, 1L, 2L, 1L, 0L, 0L)
)

TD3_TB <- cbind(
    calendar_td(
        s = series_example,
        groups = c(1L, 1L, 1L, 2L, 1L, 0L, 0L)
    ),
    lp_variable(s = series_example)
)
colnames(TD3_TB) <- c("group_1", "group_2", "ly")

TD6_TB <- calendar_td(
    s = series_example,
    groups = c(1L, 2L, 3L, 4L, 5L, 6L, 0L)
)

my_regressors_sets <- list(
    TD2_TB = TD2_TB,
    TD3_TB = TD3_TB,
    TD6_TB = TD6_TB
)
my_context <- modelling_context(variables = my_regressors_sets)
```

We can now use our context to select the calendar regressors:

```{r}
#| echo: true
#| label: "select-td-advanced"

my_td_table <- select_td(ABS[, seq_len(3L)], context = my_context)
print(my_td_table)
```
