# Hotelling's T^2-test

## Install required packages

wants <- c("DescTools", "mvtnorm")
has   <- wants %in% rownames(installed.packages())
if(any(!has)) install.packages(wants[!has])

## One-sample Hotelling’s $$T^{2}$$-test

### Simulate data

set.seed(123)
library(mvtnorm)
Nj    <- c(15, 25)
Sigma <- matrix(c(16,-2, -2,9), byrow=TRUE, ncol=2)
mu1   <- c(-4, 4)
Y1    <- round(rmvnorm(Nj[1], mean=mu1, sigma=Sigma))

### Using HotellingsT2() from package DescTools

muH0 <- c(-1, 2)
library(DescTools)
HotellingsT2Test(Y1, mu=muH0)

Hotelling's one sample T2-test

data:  Y1
T.2 = 5.3252, df1 = 2, df2 = 13, p-value = 0.02045
alternative hypothesis: true location is not equal to c(-1,2)

### Using anova.mlm()

Y1ctr  <- sweep(Y1, 2, muH0, "-")
(anRes <- anova(lm(Y1ctr ~ 1), test="Hotelling-Lawley"))
Analysis of Variance Table

Df Hotelling-Lawley approx F num Df den Df  Pr(>F)
(Intercept)  1          0.81925   5.3252      2     13 0.02045 *
Residuals   14
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

## Hotelling’s $$T^{2}$$-test for two independent samples

### Using HotellingsT2() from package DescTools

mu2 <- c(3, 3)
Y2  <- round(rmvnorm(Nj[2], mean=mu2, sigma=Sigma))
Y12 <- rbind(Y1, Y2)
IV  <- factor(rep(1:2, Nj))
library(DescTools)
HotellingsT2Test(Y12 ~ IV)

Hotelling's two sample T2-test

data:  Y12 by IV
T.2 = 23.24, df1 = 2, df2 = 37, p-value = 2.901e-07
alternative hypothesis: true location difference is not equal to c(0,0)

### Using anova.mlm() or manova()

anova(lm(Y12 ~ IV), test="Hotelling-Lawley")
Analysis of Variance Table

Df Hotelling-Lawley approx F num Df den Df    Pr(>F)
(Intercept)  1           1.3742   25.423      2     37 1.130e-07 ***
IV           1           1.2562   23.240      2     37 2.901e-07 ***
Residuals   38
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(manova(Y12 ~ IV), test="Hotelling-Lawley")
          Df Hotelling-Lawley approx F num Df den Df    Pr(>F)
IV         1           1.2562    23.24      2     37 2.901e-07 ***
Residuals 38
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

## Hotelling’s $$T^{2}$$-test for two dependent samples

### Simulate data

N    <- 20
P    <- 2
muJK <- c(90, 100, 85, 105)
Sig  <- 15
Y1t0 <- rnorm(N, mean=muJK[1], sd=Sig)
Y1t1 <- rnorm(N, mean=muJK[2], sd=Sig)
Y2t0 <- rnorm(N, mean=muJK[3], sd=Sig)
Y2t1 <- rnorm(N, mean=muJK[4], sd=Sig)
Ydf  <- data.frame(id=factor(rep(1:N, times=P)),
Y1=c(Y1t0, Y1t1),
Y2=c(Y2t0, Y2t1),
IV=factor(rep(1:P, each=N), labels=c("t0", "t1")))
dfDiff <- aggregate(cbind(Y1, Y2) ~ id, data=Ydf, FUN=diff)
DVdiff <- data.matrix(dfDiff[ , -1])
muH0   <- c(0, 0)

### Using HotellingsT2() from package DescTools

library(DescTools)
HotellingsT2Test(DVdiff, mu=muH0)

Hotelling's one sample T2-test

data:  DVdiff
T.2 = 6.0014, df1 = 2, df2 = 18, p-value = 0.01007
alternative hypothesis: true location is not equal to c(0,0)

### Using anova.mlm()

anova(lm(DVdiff ~ 1), test="Hotelling-Lawley")
Analysis of Variance Table

Df Hotelling-Lawley approx F num Df den Df  Pr(>F)
(Intercept)  1          0.66682   6.0014      2     18 0.01007 *
Residuals   19
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

## Detach (automatically) loaded packages (if possible)

try(detach(package:DescTools))
try(detach(package:mvtnorm))