# Explaining Anova of linear model for correlation

In the code below I'm trying to check for correlation between a categorical variable field2 and a continuous variable field1. Can someone please explain what the anova test is doing below? Like I understand that if the p value is below 0.05 I reject the null hypothesis that the two fields are linearly independent and that indicates a linear relation and a sign of correlation, but what is it that the anova test is doing below when it's applied to fit?

Code:

fit<-lm(field1~field2, data=NewDf[,c("field1","field2")])
anova(fit)

• You need to dig a little into R documentation enter ?anova.lm and follow the docs to the source Commented Jan 4, 2018 at 9:31
• @Patrik_P Thank you, so from the documentation it sounds like the p value from the anova test in this case compares the mean of the fitted values from the linear model to the mean of the residuals from the model. Is that correct? Commented Jan 4, 2018 at 17:30

## Statistical explanation of ANOVA

For ANOVA, where we are using a linear model between one numerical and one nominal variable the null and alternative hypothesis looks like this:

$H_0$: the group means of the numerical variable grouped by the nominal variable are the same for all groups (i.e. the numerical variable is independent from the nominal variable, they are not "correlated")

$H_1$: at least one group mean differs from the others (i.e. the variables not independent, they are "correlated")

For testing these hypotheses we have to calculate the F test statistic, for that calculation we are using the ANOVA table:

---------------------------------------------------------------------
| Source    | SS  | df        | MS              | F       | p-value |
|-----------|-----|-----------|-----------------|---------|---------|
| Treatment | SST | dfT = k-1 | MST = SST/(k-1) | MST/MSE |   ...   |
| Error     | SSE | dfE = N-k | MSE = SSE/(N-k) |    -    |    -    |
---------------------------------------------------------------------
| Total     | SS  | df = N-1  |        -        |    -    |    -    |
---------------------------------------------------------------------


where

• $SST$ is the sum of squared difference between the group means and the overall mean
• $SSE$ is the sum of squared difference between the individual values and the overall mean
• $k$ is the number of groups
• $N$ is the number of observations
• $MST$ is the mean squared difference between the group means and the overall mean
• $MSE$ is the mean squared difference between the individual values and the overall mean

So in this sense the F test statistic "compares" the $MST$ and the $MSE$ as the calculation of their ratio is a kind of comparison. For a given significance level alpha (mainly 5%) we can get the value of the F distribution ($F\left(\alpha, df_T, df_E\right)$), and if the $F$ value from the ANOVA table is more then the value of $F$ distribution ($F < F\left(\alpha, df_T, df_E\right)$), we reject $H_0$, otherwise accept it. The $p\text{-value}$ is where $F = F\left(p\text{-value}, df_T, df_E\right)$ (the point where we can't deicide), that's why we can use the decision rule if $p\text{-value} < \text{choosen significance level}$ then $H_0$ rejected.

## More statistical details

http://www.itl.nist.gov/div898/handbook/prc/section4/prc431.htm#SST%20and%20SSE

http://www.itl.nist.gov/div898/handbook/prc/section4/prc433.htm

## R code for the example in the linked page

lvl1 <- c(6.9, 5.4, 5.8, 4.6, 4.0)
lvl2 <- c(8.3, 6.8, 7.8, 9.2, 6.5)
lvl3 <- c(8.0, 10.5, 8.1, 6.9, 9.3)
group <- gl(3, 5, labels = c("Lvl1","Lvl2", "Lvl3"))
values <- c(lvl1, lvl2, lvl3)
anova(lm(values ~ group))