---
title: "Introduction to denstest"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to denstest}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  message = FALSE,
  warning = FALSE,
  fig.width = 7,
  fig.height = 5
)
```

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

# Introduction

The `denstest` package provides tools for testing equality between groups of estimated density functions. It implements FDET (Fourier-based Density Equality Testing), a new method proposed by the author for comparing groups of density functions based on their global shape using Fourier transforms. FDET extends the idea of an earlier method by Delicado, which compares densities directly but does not rely on spectral representations. For clarity and reference, this earlier approach is referred to as DET (Density Equality Testing) within the author's submitted paper.

In addition to FDET and DET, the package also includes MDET (Moment-based Density Equality Testing), another new method introduced by the author for detecting group differences in specific distributional features: expectation, variance, skewness, and kurtosis, as well as in arbitrary combinations of these four moments.

FDET and MDET are described in detail in the submitted paper, including their mathematical foundations, derivation, and use cases. DET is also presented in the same paper to contextualize the methodological development. For full theoretical background and illustrative examples, we recommend referring to the paper.


# Overview of Functions

The main functions included in the `denstest` package are:

- `denscomp()`: This is the primary function of the package, which performs statistical tests to assess whether groups of estimated density functions differ significantly. It supports various methods, including FDET and DET for global shape comparisons, and MDET for moment-based comparisons. The function returns a $p$-value indicating the significance of the observed group differences.

- `compute_B()`: This function computes the values of the test statistics underlying the different methods implemented in the package, without performing any permutation-based significance testing or calculating $p$-values. It is intended for users who wish to inspect or further process the raw test statistics directly.

Below, we provide a brief example of how to use the `denscomp()` function.

```{r example setup}
set.seed(123)

n1 <- 5
n2 <- 5
n3 <- 5
group_sizes = c(n1, n2, n3)
sample_size <- 500

densities_group1 <- lapply(1:n1, function(i) {
  data <- rnorm(sample_size, 0, 0.3)
  d <- density(data)
  list(x = d$x, y = d$y)
})

densities_group2 <- lapply(1:n2, function(i) {
  data <- rnorm(sample_size, 0, 0.32)
  d <- density(data)
  list(x = d$x, y = d$y)
})

densities_group3 <- lapply(1:n3, function(i) {
  data <- rnorm(sample_size, 0.02, 0.28)
  d <- density(data)
  list(x = d$x, y = d$y)
})
      
L <- c(densities_group1, densities_group2, densities_group3)
      
cat("p-value:", denscomp(L, group_sizes, ft.lp.weight = "AbsRoot", seed = 1234, plot = TRUE))

```


# References

For more detailed information on the methods used in this package, please refer to the following publications:

Anarat A., Krutmann, J., and Schwender, H. (2025). Testing for Differences in Extrinsic Skin Aging Based on Density Functions. Submitted.

Delicado, P. (2007). Functional k-sample problem when data are density functions. Computational Statistics, 22, 391–410. https://doi.org/10.1007/s00180-007-0047-y

