---
title: "Anova with Fixed Intercepts"
output: html_document
---

```{r setup, include=TRUE, message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
require(Sleuth3)
require(ggplot2)
require(tigerstats)
require(xtable)

fish = case0602
```

Let's run the ANOVA without a fixed intercept
```{r}
anova(lm(Percentage~Pair, data=fish))
```


And now let's fix the intercept at 0
```{r}
fit0 <- lm(Percentage~0+Pair, data=fish)
anova(fit0)
```

Let's now compute the right SSE (which will be distributed with a $\chi^2$ distribution if all the means are equal) by hand

```{r}
SSR <- sum( (fit0$fitted.values-mean(fish$Percentage))^2)
SSE <- anova(fit0)$`Sum Sq`[2]   #Sum Squared Residuals

SSR_df <- fit0$rank-1     #degrees of freedom for SSE: the number of groups minus 1, since we constrained things 
                          #by subtracting the mean
F <- (SSR/(SSR_df))/(SSE/anova(fit0)$Df[2])
```

Now, we need the p-value. We need the probability that the F-statistic would be larger than F. To get that, we use

```{r}
1-pf(F, df1=SSR_df, df2=fit0$df.residual)
```

We got the p-value we wanted back!