# Hotelling's T^2-test

## Install required packages

wants <- c("ICSNP", "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, mean=mu1, sigma=Sigma))

### Using HotellingsT2() from package ICSNP

muH0 <- c(-1, 2)
library(ICSNP)
HotellingsT2(Y1, mu=muH0)

Hotelling's one sample T2-test

data:  Y1
T.2 = 5.325, 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.819     5.33      2     13   0.02 *
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 ICSNP

mu2 <- c(3, 3)
Y2  <- round(rmvnorm(Nj, mean=mu2, sigma=Sigma))
Y12 <- rbind(Y1, Y2)
IV  <- factor(rep(1:2, Nj))
library(ICSNP)
HotellingsT2(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.37     25.4      2     37 1.1e-07 ***
IV           1             1.26     23.2      2     37 2.9e-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.26     23.2      2     37 2.9e-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, sd=Sig)
Y1t1 <- rnorm(N, mean=muJK, sd=Sig)
Y2t0 <- rnorm(N, mean=muJK, sd=Sig)
Y2t1 <- rnorm(N, mean=muJK, 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 ICSNP

library(ICSNP)
HotellingsT2(DVdiff, mu=muH0)

Hotelling's one sample T2-test

data:  DVdiff
T.2 = 6.001, 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.667        6      2     18   0.01 *
Residuals   19
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 

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

try(detach(package:ICSNP))
try(detach(package:ICS))
try(detach(package:survey))
try(detach(package:mvtnorm))