# t-test

Koristila se literatura [@Navarro2015], ...

**PDF od t-distribucije**

```{r, warning = F, fig.width = 9, fig.height = 6, fig.cap="t-distribucija s različitim stupnjevima slobode i standardna normalna distribucija"}
tablica <- tibble(sn =c(2,3,4,5,6,7,10,15,20,30,40,50)) %>% group_by(sn) %>%
  summarize(x = seq(-4,4,0.01), fun = dt(x, df = sn), 
            gauss = dnorm(x), .groups = "drop")
tablica %>%
  ggplot(aes(x = x)) + 
  geom_line(aes(y=fun), color = "blue") +
  geom_line(aes(y=gauss), linetype="dashed") +
  ylab("PDF(x)") + facet_wrap(~sn)
```

**Primjer**  
The response is the length of odontoblasts (cells responsible for tooth growth) in 60 guinea pigs. 
Each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two 
delivery methods, orange juice or ascorbic acid (a form of vitamin C and coded as VC).

```{r}
options(pillar.sigfig=5)
data("ToothGrowth")
glimpse(ToothGrowth)
```

## One sample t-test for a hypothesized mean

**Question:** Is the mean of a sample significantly different from a hypothesized mean?

**When to use the test?**
You want to compare the sample mean to a hypothesized value. The test
assumes the sample observations are normally distributed and the
population standard deviation is unknown.

**Dvosmjerni t-test**  
Početna hipoteza: $\mu=19$  
Alternativna hipoteza: $\mu\neq19$

```{r}
rez0 <- t.test(x=ToothGrowth$len, mu = 19)
rez0
```

**Ručni izračun**  

```{r}
n <- nrow(ToothGrowth)
Xbar <- mean(ToothGrowth$len)
mu <- 19
s <- sd(ToothGrowth$len)
sbar <- s / sqrt(n)
t = (Xbar - mu) / sbar
list(Xbar = Xbar,
     mu = mu,
     s = s,
     sbar = sbar,
     t = t,
     pvalue = ifelse(t < 0, 2*pt(t, n-1), 2*pt(t, n-1, lower.tail = FALSE)),
     lower = Xbar + qt(0.025, df=n-1) * sbar,
     upper = Xbar - qt(0.025, df=n-1) * sbar)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu =19$ i odbacujemo alternativnu hipotezu $\\mu\\neq19$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_vline(xintercept = t, linetype = "dotted") +
  geom_text(aes(x=t, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(-4, qt(0.025, df = n-1)), alpha=0.75) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(qt(0.975, df = n-1), 4), alpha=0.75) +
  stat_function(fun=dt, args = list(df = n-1), color="blue") + 
  labs(title = "one sample two sided t-test: \u03BC = 19")
```

**Jednosmjerni (less) t-test**  
Početna hipoteza: $\mu\geqslant21$  
Alternativna hipoteza: $\mu<21$

```{r}
rez1 <- t.test(x=ToothGrowth$len, mu = 21, alternative = "less")
rez1
```

**Ručni izračun**  

```{r}
mu <- 21
t = (Xbar - mu) / sbar
list(Xbar = Xbar,
     mu = mu,
     s = s,
     sbar = sbar,
     t = t,
     pvalue = pt(t, n-1),
     lower = -Inf,
     upper = Xbar - qt(0.05, df=n-1) * sbar)
```

```{r, warning = F, fig.cap = "Odbacujemo početnu hipotezu $\\mu\\geqslant21$ i prihvaćamo alternativnu hipotezu $\\mu<21$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez1$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(-4, qt(0.05, df = n-1)), alpha=0.75) +
  geom_vline(xintercept = rez1$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = n-1), color="blue") + 
  labs(title = "one sample less t-test: \u03BC \u2265 21")
```

**Jednosmjerni (greater) t-test**  
Početna hipoteza: $\mu\leqslant21$  
Alternativna hipoteza: $\mu>21$

```{r}
rez2 <- t.test(x=ToothGrowth$len, mu = 21, alternative = "greater")
rez2
```

**Ručni izračun**  

```{r}
list(Xbar = Xbar,
     mu = mu,
     s = s,
     sbar = sbar,
     t = t,
     pvalue = pt(t, n-1, lower.tail = FALSE),
     lower = Xbar + qt(0.05, df=n-1) * sbar,
     upper = Inf)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu\\leqslant21$ i odbacujemo alternativnu hipotezu $\\mu>21$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez2$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(qt(0.95, df = n-1), 4), alpha=0.75) +
  geom_vline(xintercept = rez2$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = n-1), color="blue") + 
  labs(title = "one sample greater t-test: \u03BC \u2264 21")
```

## Two sample t-test for the difference in sample means

**Question:** Is the difference between the mean of two samples significantly different from zero?

**When to use the test?**
You want to assess the extent to which the mean of two independent
samples are different from each other. The test assumes the sample
observations are normally distributed, and the sample variances are equal.

**Dvosmjerni t-test**  
Početna hipoteza: $\mu_1=\mu_2$  
Alternativna hipoteza: $\mu_1\neq\mu_2$

```{r}
rez3 <- t.test(len ~ supp, data = ToothGrowth, var.equal = TRUE)
rez3
```

**Ručni izračun**  

```{r}
podaci <- ToothGrowth %>% group_by(supp) %>% 
  summarize(Xbar = mean(len), v = var(len), n = n())
s2_pool <- podaci %>% summarize(s2_pool = sum((n - 1) * v) / (sum(n) - 2)) %>% 
  pull(s2_pool)
Xbar <- podaci %>% pull(Xbar)
n <- podaci %>% pull(n)
SE <- sqrt(s2_pool * (1 / n[1] + 1 / n[2]))
t <- (Xbar[1] - Xbar[2]) / SE
list(podaci = podaci,
     pooled_variance = s2_pool,
     standard_error = SE,
     t = t,
     pvalue = ifelse(t < 0, 2*pt(t, sum(n)-2), 2*pt(t, sum(n)-2, lower.tail = FALSE)),
     lower = Xbar[1]-Xbar[2] + qt(0.025, df=sum(n)-2) * SE,
     upper = Xbar[1]-Xbar[2] - qt(0.025, df=sum(n)-2) * SE)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu_1 =\\mu_2$ i odbacujemo alternativnu hipotezu $\\mu_1\\neq\\mu_2$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez3$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = sum(n)-2), fill = "pink", 
            xlim = c(-4, qt(0.025, df = sum(n)-2)), alpha=0.75) +
  geom_area(stat = "function", fun = dt, args = list(df = sum(n)-2), fill = "pink", 
            xlim = c(qt(0.975, df = sum(n)-2), 4), alpha=0.75) +
  geom_vline(xintercept = rez3$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = sum(n)-2), color="blue") + 
  labs(title = "two sample two sided t-test: \u03BC\u2081 = \u03BC\u2082")
```

**Jednosmjerni (less) t-test**  
Početna hipoteza: $\mu_1\geqslant\mu_2$  
Alternativna hipoteza: $\mu_1<\mu_2$

```{r}
rez4 <- t.test(len ~ supp, data = ToothGrowth, var.equal = TRUE, alternative = "less")
rez4
```

**Ručni izračun**  

```{r}
list(podaci = podaci,
     pooled_variance = s2_pool,
     standard_error = SE,
     t = t,
     pvalue = pt(t, sum(n)-2),
     lower = -Inf,
     upper = Xbar[1]-Xbar[2] - qt(0.05, df=sum(n)-2) * SE)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu_1\\geqslant\\mu_2$ i odbacujemo alternativnu hipotezu $\\mu_1<\\mu_2$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez4$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = sum(n)-2), fill = "pink", 
            xlim = c(-4, qt(0.05, df = sum(n)-2)), alpha=0.75) +
  geom_vline(xintercept = rez3$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = sum(n)-2), color="blue") + 
  labs(title = "two sample less t-test: \u03BC\u2081 \u2265 \u03BC\u2082")

```

**Jednosmjerni (greater) t-test**  
Početna hipoteza: $\mu_1\leqslant\mu_2$  
Alternativna hipoteza: $\mu_1>\mu_2$

```{r}
rez5 <- t.test(len ~ supp, data = ToothGrowth, var.equal = TRUE, alternative = "greater")
rez5
```

**Ručni izračun**  

```{r}
list(podaci = podaci,
     pooled_variance = s2_pool,
     standard_error = SE,
     t = t,
     pvalue = pt(t, sum(n)-2, lower.tail = FALSE),
     lower = Xbar[1]-Xbar[2] + qt(0.05, df=sum(n)-2) * SE,
     upper = Inf)
```

```{r, warning = F, fig.cap = "Odbacujemo početnu hipotezu $\\mu_1\\leqslant\\mu_2$ i prihvaćamo alternativnu hipotezu $\\mu_1>\\mu_2$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez5$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = sum(n)-2), fill = "pink", 
            xlim = c(qt(0.95, df = sum(n)-2), 4), alpha=0.75) +
  geom_vline(xintercept = rez5$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = sum(n)-2), color="blue") + 
  labs(title = "two sample greater t-test: \u03BC\u2081 \u2264 \u03BC\u2082")

```

## Welch t-test for the difference in sample means

**Question:** Is the difference between the mean of two samples significantly different from zero?

**When to use the test?**
You want to assess the extent to which the mean of two independent
samples are different from each other. The test assumes the sample
observations are normally distributed, and the sample variances are not equal.

**Dvosmjerni t-test**  
Početna hipoteza: $\mu_1=\mu_2$  
Alternativna hipoteza: $\mu_1\neq\mu_2$

```{r}
rez6 <- t.test(len ~ supp, data = ToothGrowth)
rez6
```

**Ručni izračun**  

```{r}
podaci <- ToothGrowth %>% group_by(supp) %>% 
  summarize(Xbar = mean(len), v = var(len), n = n())
Xbar <- podaci %>% pull(Xbar)
SE <- podaci %>% summarize(SE = sqrt(sum(v / n))) %>% pull(SE)
t <- (Xbar[1] - Xbar[2]) / SE
degfr <- podaci %>% 
  summarize(degfr = sum(v / n)^2 / sum((v / n)^2 / (n - 1))) %>% 
  pull(degfr)
list(podaci = podaci,
     standard_error = SE,
     t = t,
     df = degfr,
     pvalue = ifelse(t < 0, 2*pt(t, degfr), 2*pt(t, degfr, lower.tail = FALSE)),
     lower = Xbar[1]-Xbar[2] + qt(0.025, df=degfr) * SE,
     upper = Xbar[1]-Xbar[2] - qt(0.025, df=degfr) * SE)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu_1 =\\mu_2$ i odbacujemo alternativnu hipotezu $\\mu_1\\neq\\mu_2$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez6$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = degfr), fill = "pink", 
            xlim = c(-4, qt(0.025, df = degfr)), alpha=0.75) +
  geom_area(stat = "function", fun = dt, args = list(df = degfr), fill = "pink", 
            xlim = c(qt(0.975, df = degfr), 4), alpha=0.75) +
  geom_vline(xintercept = rez6$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = degfr), color="blue") + 
  labs(title = "Welch two sample two sided t-test: \u03BC\u2081 = \u03BC\u2082")
```

**Jednosmjerni (less) t-test**  
Početna hipoteza: $\mu_1\geqslant\mu_2$  
Alternativna hipoteza: $\mu_1<\mu_2$

```{r}
rez7 <- t.test(len ~ supp, data = ToothGrowth, alternative = "less")
rez7
```

**Ručni izračun**  

```{r}
list(podaci = podaci,
     standard_error = SE,
     t = t,
     df = degfr,
     pvalue = pt(t, degfr),
     lower = -Inf,
     upper = Xbar[1]-Xbar[2] - qt(0.05, df=degfr) * SE)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu_1 \\geqslant\\mu_2$ i odbacujemo alternativnu hipotezu $\\mu_1<\\mu_2$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez7$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = degfr), fill = "pink", 
            xlim = c(-4, qt(0.05, df = degfr)), alpha=0.75) +
  geom_vline(xintercept = rez7$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = degfr), color="blue") + 
  labs(title = "Welch two sample less t-test: \u03BC\u2081 \u2265 \u03BC\u2082")
```

**Jednosmjerni (greater) t-test**  
Početna hipoteza: $\mu_1\leqslant\mu_2$  
Alternativna hipoteza: $\mu_1>\mu_2$

```{r}
rez8 <- t.test(len ~ supp, data = ToothGrowth, alternative = "greater")
rez8
```

**Ručni izračun**  

```{r}
list(podaci = podaci,
     standard_error = SE,
     t = t,
     df = degfr,
     pvalue = pt(t, degfr, lower.tail = FALSE),
     lower = Xbar[1]-Xbar[2] + qt(0.05, df=degfr) * SE,
     upper = Inf)
```

```{r, warning = F, fig.cap = "Odbacujemo početnu hipotezu $\\mu_1 \\leqslant\\mu_2$ i prihvaćamo alternativnu hipotezu $\\mu_1>\\mu_2$"}
tibble(x = seq(-4,4,0.01)) %>%
  ggplot(aes(x)) + 
  geom_text(aes(x=rez8$statistic, label="t", y=0.1), nudge_x = 0.08, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = degfr), fill = "pink", 
            xlim = c(qt(0.95, df = degfr), 4), alpha=0.75) +
  geom_vline(xintercept = rez8$statistic, linetype = "dotted") +
  stat_function(fun=dt, args = list(df = degfr), color="blue") + 
  labs(title = "Welch two sample greater t-test: \u03BC\u2081 \u2264 \u03BC\u2082")
```

## Paired t-test

**Question:** Is the difference between the mean of two samples significantly different 
from zero?

**When to use the test?**
This test is used when each subject in a study is measured twice, before
and after a treatment. Alternatively, in a matched pairs experimental
design, where subjects are matched in pairs and different treatments are
given to each subject pair. Subjects are assumed to be drawn from a
population with a normal distribution.

**Primjer**

```{r}
testovi <- tibble(test1 = c(16,20,21,22,23,22,27,25,27,28), 
                  test2 = c(19,22,24,24,25,25,26,26,28,32))
```

**Dvosmjerni t-test**  
Početna hipoteza: $\mu=0$  
Alternativna hipoteza: $\mu\neq0$

```{r}
t.test(testovi$test2, testovi$test1, paired = TRUE)
```

**Ručni izračun**  

```{r}
n <- nrow(testovi)
razlika <- testovi$test2 - testovi$test1
Xbar <- mean(razlika)
s <- sd(razlika)
sbar <- s / sqrt(n)
t = Xbar / sbar
list(Xbar = Xbar,
     s = s,
     sbar = sbar,
     t = t,
     pvalue = ifelse(t < 0, 2*pt(t, n-1), 2*pt(t, n-1, lower.tail = FALSE)),
     lower = Xbar + qt(0.025, df=n-1) * sbar,
     upper = Xbar - qt(0.025, df=n-1) * sbar)
```

```{r, warning = F, fig.cap = "Odbacujemo početnu hipotezu $\\mu=0$ i prihvaćamo alternativnu hipotezu $\\mu\\neq0$"}
tibble(x = seq(-5,5,0.01)) %>%
  ggplot(aes(x)) + 
  geom_vline(xintercept = t, linetype = "dotted") +
  geom_text(aes(x=t, label="t", y=0.1), nudge_x = 0.1, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(-5, qt(0.025, df = n-1)), alpha=0.75) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(qt(0.975, df = n-1), 5), alpha=0.75) +
  stat_function(fun=dt, args = list(df = n-1), color="blue") + 
  labs(title = "paired two sided t-test: \u03BC = 0")
```

**Jednosmjerni (less) t-test**  
Početna hipoteza: $\mu\geqslant0$  
Alternativna hipoteza: $\mu<0$

```{r}
t.test(testovi$test2, testovi$test1, alternative = "less", paired = TRUE)
```

**Ručni izračun**  

```{r}
list(Xbar = Xbar,
     s = s,
     sbar = sbar,
     t = t,
     pvalue = pt(t, n-1),
     lower = -Inf,
     upper = Xbar - qt(0.05, df=n-1) * sbar)
```

```{r, warning = F, fig.cap = "Prihvaćamo početnu hipotezu $\\mu\\geqslant0$ i odbacujemo alternativnu hipotezu $\\mu<0$"}
tibble(x = seq(-5,5,0.01)) %>%
  ggplot(aes(x)) + 
  geom_vline(xintercept = t, linetype = "dotted") +
  geom_text(aes(x=t, label="t", y=0.1), nudge_x = 0.1, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(-5, qt(0.05, df = n-1)), alpha=0.75) +
  stat_function(fun=dt, args = list(df = n-1), color="blue") + 
  labs(title = "paired less t-test: \u03BC \u2265 0")
```

**Jednosmjerni (greater) t-test**  
Početna hipoteza: $\mu\leqslant0$  
Alternativna hipoteza: $\mu>0$

```{r}
t.test(testovi$test2, testovi$test1, alternative = "greater", paired = TRUE)
```

**Ručni izračun**  

```{r}
list(Xbar = Xbar,
     s = s,
     sbar = sbar,
     t = t,
     pvalue = pt(t, n-1, lower.tail = FALSE),
     lower = Xbar + qt(0.05, df=n-1) * sbar,
     upper = Inf)
```

```{r, warning = F, fig.cap = "Odbacujemo početnu hipotezu $\\mu\\leqslant0$ i prihvaćamo alternativnu hipotezu $\\mu>0$"}
tibble(x = seq(-5,5,0.01)) %>%
  ggplot(aes(x)) + 
  geom_vline(xintercept = t, linetype = "dotted") +
  geom_text(aes(x=t, label="t", y=0.1), nudge_x = 0.1, colour="red",
            text=element_text(size=11)) +
  geom_area(stat = "function", fun = dt, args = list(df = n-1), fill = "pink", 
            xlim = c(qt(0.95, df = n-1), 5), alpha=0.75) +
  stat_function(fun=dt, args = list(df = n-1), color="blue") + 
  labs(title = "paired less t-test: \u03BC \u2264 0")
```

## Two sample Wilcoxon test

Unlike the t-test, the Wilcoxon test doesn’t assume normality, which is nice. 
In fact, they don’t make any assumptions about what kind of distribution is involved: 
in statistical jargon, this makes them **nonparametric** tests. While avoiding 
the normality assumption is nice, there’s a drawback: the Wilcoxon test is 
usually less powerful than the t-test (i.e., higher Type II error rate).

**Linkovi**  
[Wilcoxon signed rank test, Wiki](https://en.wikipedia.org/wiki/Wilcoxon_signed-rank_test){target="_blank"}  
[Mann–Whitney U-test, Wiki](https://en.wikipedia.org/wiki/Mann%E2%80%93Whitney_U_test){target="_blank"}  
[Mann–Whitney U-test, statskingdom](https://www.statskingdom.com/test_mann_whitney.html){target="_blank"}

**Question:** Is the difference between the median of two samples significantly different
from zero?

**When to use the test?**
You want to assess the extent to which the median of two independent
samples are different from each other. The test is less sensitive to outliers
than the two sample t-test. Note, the test is some mes referred to as the
Mann-Whitney U test, or the Mann-Whitney-Wilcoxon test.

**Jedan primjer upotrebe gotove naredbe**

```{r}
wilcox.test(len ~ supp, data = ToothGrowth, alternative = "less", 
            exact = FALSE, correct = FALSE)
```

**Ručni izračun**

```{r}
ToothGrowth %>% mutate(lenRank = rank(len)) %>% group_by(supp) %>% 
  summarize(R = sum(lenRank), n =n(), U = R - n * (n + 1) / 2) 
```

```{r}
tied <- table(rank(ToothGrowth$len))
tk <- sum(sapply(tied, function(x) x^3 - x))
n1 <- 30
n2 <- 30
n <- n1 + n2
pnorm(575.5, n1*n2/2, sqrt(n1 * n2 / 12 * (n + 1 - tk / (n*(n+1) ))))
```

**Rangovi**

```{r}
tibble(len = sort(ToothGrowth$len), len.rank = rank(len)) %>%
  print(n=Inf)
```

## One sample Wilcoxon test

**Question:** Is the median of a sample significantly different from a hypothesized value?

**When to use the test?**
To test whether the median of sample is equal to a speciﬁed value. The
null hypothesis is that the median of observations is zero (or some other
speciﬁed value). It is a nonparametric test and therefore requires no
assumption for the sample distribution. It is an alternative to the one-sample 
t-test when the normal assumption is not satisfied.

```{r}
testovi <- tibble(test1 = c(16,20,21,22,23,22,27,25,27,28), 
                  test2 = c(19,22,24,24,25,25,26,26,28,32))
```

```{r}
wilcox.test(testovi$test2, testovi$test1, paired = TRUE, alternative = "less")
```
