For a given dataset wm_t, the example for calculating standard deviation of the "gain" column is represented as:
sd_diff <- sqrt((sum(wm_t$gain^2) - ((sum(wm_t$gain))^2/n))/(n-1))
My problem is with the calculation in the numerator. Let's take away everything except for the numerator, leaving:
sum(wm_t$gain^2) - ((sum(wm_t$gain))^2/n)
So the definition for standard deviation of a sample is:
Sum(X-M)^2 / (n-1)
where X a given value in a dataset, M is the mean, and n is the sample size. Correct?
What they seem to be saying here in that above R excerpt, though, is that the sum(X-M)^2 can be expanded into:
X^2 - (sum(X)^2 / n)
How can you expand the (X-M)^2? I'm not understanding how this is possible, and especially cannot wrap my head around why you are only distributing the square to the numerator of the mean and not the denominator.
To explain via R, on the original definition of standard deviation above, why is the following code not correct?
mean_diff <- sum(wm_t$gain) / n sd <- sqrt(sum(wm_t$gain - mean_diff)^2 / (n-1))
This follows the original equation, whereas the example that they give (which is correct) seems to expand (X-M)^2. I cannot understand what is wrong with my approach.
I hope this is clear. Thank you!