This problem came up because I was trying to replicate some results I was getting in Stata with R, and I was able to replicate everything except for the root mean squared error.
When I run a regression in Stata, I am able to perfectly replicate the calculated RMSE using a manual calculation.
However, when I include any kind of weight, I'm unable to replicate this.
Here's code to create some example data.
clear
set seed 12345
drawnorm x e, n(1000)
gen y = 5+(2*x)+e
gen w = x*runiform()
Here is code for running a basic regression and calculating the RMSE.
reg y x
local df = `e(df_r)'
predict yhat
gen sqerr = (yhat-y)^2
egen mse = total(sqerr)
replace mse = mse/`df'
gen rmse = sqrt(mse)
mean rmse
Here, I get an output RMSE of 1.0151, and a manually-calculated RMSE of 1.0151.
Here is the code for doing it with weights.
drop yhat sqerr mse rmse
reg y x [aweight=w]
local df = `e(df_r)'
predict yhat
gen sqerr = (yhat-y)^2
egen mse = total(sqerr)
replace mse = mse/`df'
gen rmse = sqrt(mse)
mean rmse
Here, I get an output RMSE of 0.985 and a manually-calculated RMSE of 1.442.
Any ideas? I've searched the web, and everyone seems to agree the RMSE should be calculated with $\sqrt{\frac{\sum{(predict-actual)^2}}{df}}$. Any ideas of how this formula is different when weights are included?