I'm doing a WLS estimation and actually the RMSE (Root mean square error) given by the "fitlm" function is different by the one calculated by hand.
Here there is the MATLAB code
%% Generating data
x=unifrnd(0,30,100,1);
y=sin(x).*x;
wi=x; % weights
m_hat=fitlm(x,y,'Weights',wi); % the model
b_hat = m_hat.Coefficients.Estimate; % coefficients
y_hat = b_hat(1)+b_hat(2)*x; % model estimation
% RMSE extracted
rmse=m_hat.RMSE; % rmse "extracted" from fitlm
% RMSE by hand
RMSE_with_formula=sqrt(sum((y-y_hat).^2)/m_hat.DFE))
Can somebody explain why? Which is the correct one? Thanks.