---
title: "k-sample test"
author: "Giovanni Saraceno"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{k-sample test}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---
```{r srr-tags, eval = FALSE, echo = FALSE}
#' srr tags
#' 
#' 
#' @srrstats {G1.5} k-sample test example in the associated paper
```
### $k$-sample test

We generated three samples, with $n=200$ observations each, from a 2-dimensional Gaussian distributions with mean vectors $\mu_1 = (0, \frac{\sqrt{3}}{3})$, ${\mu}_2 = (-\frac{1}{2}, -\frac{\sqrt{3}}{6})$ and $\mu_3 = (\frac{1}{2}, -\frac{\sqrt{3}}{6})$, and the Identity matrix as covariance matrix. In this situation, the generated samples are well separated, following different Gaussian distributions, i.e. $X_1 \sim N_2(\mu_1, I)$, $X_2 \sim N_2(\mu_2, I)$ and $X_3 \sim N_2(\mu_3, I)$}. The vector ``y`` indicates the membership to groups.  
```{r}
library(mvtnorm)
library(QuadratiK)
sizes <- rep(200,3)
eps <- 1
set.seed(2468)
x1 <- rmvnorm(sizes[1], mean = c(0,sqrt(3)*eps/3))
x2 <- rmvnorm(sizes[2], mean = c(-eps/2,-sqrt(3)*eps/6))
x3 <- rmvnorm(sizes[3], mean = c(eps/2,-sqrt(3)*eps/6))
x <- rbind(x1, x2, x3)
y <- as.factor(rep(c(1,2,3), times=sizes))
```
Recall that the computed test statistics correspond to the omnibus tests.
```{r}
h <- 1.5
set.seed(2468)
k_test <- kb.test(x=x, y=y, h=h)
k_test
```

When the $k$-sample test is performed, the ``summary`` method on the ``kb.test`` object returns the results of the tests together with the standard descriptive statistics for each variable computed, overall and with respect to the provided groups. 
```{r}
summary_ktest <- summary(k_test)
summary_ktest$summary_tables
```

#### Selection of h

If a value of $h$ is not provided, the function automatically perform the function ``select_h``.
```{r}
#k_test_h <- kb.test(x=x, y=y)
```

For a more accurate search of the tuning parameter, the function ``select_h`` can be used.This function needs the input ``x`` and ``y`` as the function ``kb.test`` for the $k$-sample problem. 
```{r eval=FALSE, fig.width=6, fig.height=4}
set.seed(2468)
h_k <- select_h(x=x, y=y, alternative="skewness")
```
```{r eval=FALSE}
h_k$h_sel
```
The figure generated by the ``select_h`` function on the result of the selection of $h$ algorithm for the $k$-sample data set displays the obtained power versus the considered $h$, for each value of skewness alternative $\delta$ considered.

As it is possible to see from the figure, when the alternative distribution $F_\delta$ with $\delta=0.2$ is considered, there are no values of $h$ which achieve power greater than or equal to 0.5. Then, the second value of $\delta=0.3$ is take into account and $h=1.6$ is chosen as optimal value since it is the smallest value with power greater than 0.5. Additionally, it gives a possible set of values with high power performance.