# In R, why are the results of summary(lm(y~-1))$sigma^2 and mean(y^2) same? Consider the following data and the model: y <- c(9,3,-2,7,6,12) lm(y~-1) summary(lm(y~-1))$sigma^2
[1] 53.83333


I expected var(y) will give the same result of summary(lm(y~-1))$sigma^2. But, it did not give, rather mean(y^2) gave same result? Isn't formula of mean(y^2) is $$\frac{\sum_{i=1}^{n}y_i^2}{n}$$? Why are the results of summary(lm(y~-1))$sigma^2 and mean(y^2) same?

var(y)
[1] 23.76667

mean(y^2)
[1] 53.83333


If you define the model as lm(y~1), you will get a value of 23.76667 for sigma^2.
Your model does not have an intercept (that's the -1 in the formula), which is equivalent to stating that the y values come from a population with mean zero. Your estimate of the population variance is, thus, $$\frac{\sum_{i=1}^{n}y_i^2}{n}$$.
summary(lm(y~-1))\$sigma^2 gives the variance of the residuals from your model, but your model is one that doesn't fit any parameters and just predicts $$0$$ every time: $$\hat y_i = 0\space\space\forall\space\space i$$. There are no parameters estimated from the data. Therefore, the residuals are the $$y$$-values themselves, and their variance is calculated assuming a degrees of freedom equal to the sample size itself (you don't lose a degree of freedom estimating the mean).
$$\text{var}\left(\hat\varepsilon\right) = \dfrac{\sum_i y_i}{N}$$