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

anova(lm(Percentage~Pair, data=fish))
## Analysis of Variance Table
## 
## Response: Percentage
##           Df  Sum Sq Mean Sq F value Pr(>F)
## Pair       5   938.7  187.75  0.7858  0.563
## Residuals 78 18636.7  238.93

And now let’s fix the intercept at 0

fit0 <- lm(Percentage~0+Pair, data=fish)
anova(fit0)
## Analysis of Variance Table
## 
## Response: Percentage
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## Pair       6 325175   54196  226.83 < 2.2e-16 ***
## Residuals 78  18637     239                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

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

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

1-pf(F, df1=SSR_df, df2=fit0$df.residual)
## [1] 0.5630187

We got the p-value we wanted back!