---
title: "Model fitting for variable domain functional data"
output:
  rmarkdown::html_vignette:
    toc: yes
    fig_width: 6
vignette: >
  %\VignetteIndexEntry{Model fitting for variable domain functional data}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---



## Introduction

The VDPO package provides, among other tools, methods for analyzing variable domain functional data. This vignette demonstrates how to fit variable domain functional regression models using the `vd_fit` function, which is designed to handle various types of functional and non-functional covariates in a flexible framework.

## Data Generation


``` r
library(VDPO)
```

We'll start by generating sample data using the `data_generator_vd` function. This function creates simulated data with variable domain functional covariates and additional non-functional covariates if specified.


``` r
# Generate data with functional and non-functional covariates
data <- data_generator_vd(beta_index = 1, use_x = TRUE, use_f = TRUE)
```

## Model Fitting

The `vd_fit` function is the main tool for fitting variable domain functional regression models. It supports various model specifications through a formula interface.

### Basic Model with Single Functional Covariate

Let's start with a basic model using only the functional covariate:


``` r
data <- data_generator_vd(beta_index = 1, use_x = FALSE, use_f = FALSE)
formula <- y ~ ffvd(X_se, nbasis = c(10, 10, 10))
res <- vd_fit(formula = formula, data = data)
```

### Model with Multiple Functional Covariates

If your data contains multiple functional covariates, you can include them in the model:


``` r
data <- data_generator_vd(
  beta_index = 1,
  use_x = FALSE,
  use_f = FALSE,
  multivariate = TRUE
)
formula <- y ~ ffvd(X_se, nbasis = c(10, 10, 10)) + ffvd(Y_se, nbasis = c(10, 20, 10))
res_multi <- vd_fit(formula = formula, data = data)
```

### Model with Functional and Non-Functional Covariates

The `vd_fit` function also supports including non-functional covariates, both linear and smooth terms:


``` r
data <- data_generator_vd(beta_index = 1, use_x = TRUE, use_f = TRUE)
formula <- y ~ ffvd(X_se, nbasis = c(10, 10, 10)) + f(x2, nseg = 30, pord = 2, degree = 3) + x1
res_complex <- vd_fit(formula = formula, data = data)
```

In this model:

-   `ffvd(X_se, nbasis = c(10, 10, 10))` specifies the functional covariate
-   `f(x2, nseg = 30, pord = 2, degree = 3)` adds a smooth effect for `x2`
-   `x1` is included as a linear term

## Model Summary

You can obtain a summary of the fitted model using the `summary` function:


``` r
summary(res_complex)
#> 
#> Family: gaussian 
#> Link function: identity 
#> 
#> 
#> Formula:
#> NULL
#> 
#> 
#> Fixed terms: 
#>                                  x2                                                 
#>   1.4677659   0.9740737  -0.1436006   4.9290908   3.7791431 -11.9600960  -2.3722837 
#> 
#> 
#> Estimated degrees of freedom:
#> Total edf     Total      <NA>      <NA>      <NA> 
#>    4.9384    4.5431    0.0001    9.4815   16.4815 
#> 
#> R-sq.(adj) =  0.958   Deviance explained = 97.5%  n = 100
#> 
#> Number of iterations: 1
```

## Working with Non-Aligned Data

The `vd_fit` function can handle both aligned and non-aligned functional data. Here's an example with non-aligned data:


``` r
data_not_aligned <- data_generator_vd(aligned = FALSE, beta_index = 1)
formula <- y ~ ffvd(X_se, nbasis = c(10, 10, 10))
res_not_aligned <- vd_fit(formula = formula, data = data_not_aligned)
```

## Additional functionality

If you need to include an offset in your model, you can use the `offset` argument:


``` r
offset <- rnorm(nrow(data$X_se))
res_with_offset <- vd_fit(formula = formula, data = data, offset = offset)
```

### Plotting the betas

A heatmap for a specific beta of the model can be obtained by using the plot function:


``` r
plot(res)
```

![](includes/VDPO-02-unnamed-chunk-9-1.png)

## Final remarks

The `vd_fit` function in the VDPO package provides a flexible and powerful tool for fitting variable domain functional regression models. It supports a wide range of model specifications, including multiple functional covariates, non-functional covariates, and various distribution families. By leveraging the formula interface, users can easily specify complex models tailored to their specific analysis needs.
