4
$\begingroup$

I'm learning R and trying to understand how lm() handles factor variables & how to make sense of the ANOVA table. I'm fairly new to statistics, so please be gentle with me.

Here's some movie data from Rotten Tomatoes. I'm trying to model the score of each movie based on the mean scores for all of the movies in 4 groups: those rated G, PG, PG-13, and R.

download.file("http://www.rossmanchance.com/iscam2/data/movies03RT.txt", destfile = "./movies.txt")
movies <- read.table("./movies.txt", sep = "\t", header = T, quote = "")
lm1 <- lm(movies$score ~ as.factor(movies$rating))
anova(lm1)

and the ANOVA output:

## Analysis of Variance Table
## 
## Response: movies$score
##                           Df Sum Sq Mean Sq F value Pr(>F)
## as.factor(movies$rating)   3    570     190    0.92   0.43
## Residuals                136  28149     207

I understand how to get all the numbers in this table, EXCEPT Sum Sq and Mean Sq for as.factor(movies$rating). Can someone please explain how that Sum Sq is calculated from my data? I know that Mean Sqis just Sum Sq divided by Df.

$\endgroup$
  • $\begingroup$ Depends on the row, but in general it is the sum of the squared differences between each value and its expected value (usually some mean), so you can get a measure of variation. For the row you asked, is the sum of the (4) differences between each group mean and the general mean. I recommend you to start reading a basic stats book (the ANOVA section to go straight into it) or, e.g., something like this. There are many pitfalls (e.g., invalid assumptions) you may want to avoid. $\endgroup$ – FairMiles Feb 13 '13 at 19:18
9
$\begingroup$

You must be in the Coursera Data Analysis class. The topic of regression is much more involved than that professor is leading his students to believe. This is one very good example where he just waved his hands without explaining.

The idea behind analysis of variance (ANOVA) as it applies to linear regression is to partition the variances. So the column "Sum sq" in the ANOVA table you give is actually two different sum of square values.

The one in the "as.factor(movies$rating)" is what is called the Model sum of squares (aka regression sum of squares). This is the variability of the model's predicted values. In other words, it is the variability of the response due to its relationship with the predictor. In your case, the response is score and the predictor is rating.

It is calculated as the sum of the squares of the difference in the predicted values from the overall mean.

Your model:

$$ \hat{Y_i} = \hat{\beta}_0 + \hat{\beta}_1 x_i $$

where $\hat{Y_i}$ is the predicted value of score for the $i$th movie and $x_i$ is the rating for the $i$th movie.

The regression sum of squares (the 570 in your table) is calculated as the square of the difference of each predicted value from the overall mean--the overall mean score of all the movies.

$$ RSS = \sum_{i=1}^n \left(\hat{Y}_i - \bar{Y}\right)^2 $$

Here, $\bar{Y}$ is the overall mean score of all the movies. If you run the following R code you'll see how it's calculated.

sum((predict(lm1) - mean(movies$score))^2) 

The sum of squares for the residuals is called the sum of square errors and is calculated from the formula

$$ SSE = \sum_{i=1}^n \left(Y_i - \hat{Y}_i\right)^2 $$

This is the sum of the squared difference of each actual score from the predicted score. This is the value that is minimized by the least squares procedure. You can see this using the following R code

sum(lm1$residuals^2)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.