How does MATLAB calculate standard error in 'fitlm'?
15 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MathWorks Support Team
el 13 de Dic. de 2017
Respondida: MathWorks Support Team
el 20 de Dic. de 2017
I would like to know how MATLAB calculates the standard error in the 'fitlm' function both when an intercept is set to 'false' and when an intercept is included in the model.
Respuesta aceptada
MathWorks Support Team
el 13 de Dic. de 2017
The following code can be used to calculate the standard error on mock data both in the case that the intercept is set to 'false' and when an intercept is present:
%%Sample Data Definition
X = [1 2 3 4 5 6 7 8 9 10];
Y = [1 1.5 2.1 4 5 6.3 7.3 7.8 9 9.9];
%%Calculation of Standard Error Without Intercept
n = length(X); % Number of observations
Slope = sum(X.*Y)/sum(X.^2); % Calculates slope
yfit=X*Slope; % Fitted response values based on the slope
r = Y - yfit; % r is the residuals, which is the observed minus fitted values
SSE = sum(r.^2); % SSE is the sum of squared errors
MSE=SSE/(n-1); % Mean Squared Error
Code_NoIntercept_SE=sqrt(MSE/sum(X.^2)) % Standard error of the slope
model1 = fitlm(X,Y,'Intercept',false); % Calls 'fitlm'
MATLAB_NoIntercept_SE=model1.Coefficients.SE % Queries the standard error of MATLAB's model
%%Clear Variables then Redefine Data
clear all
X = [1 2 3 4 5 6 7 8 9 10];
Y = [1 1.5 2.1 4 5 6.3 7.3 7.8 9 9.9];
%%Calculation of Standard Error With Intercept
n = length(X); % Number of observations
XBar=mean(X); % Calculates mean of X
YBar=mean(Y); % Calculates mean of Y
Sxx=sum((X-XBar).^2);
Sxy=sum((X-XBar).*(Y-YBar));
Slope = Sxy/Sxx; % Calculates Slope
Intercept= YBar-Slope*XBar; % Calculates Intercept
yfit=Intercept + X*Slope; % Fitted response values based on the slope
r = Y - yfit; % r is the residuals, which is the observed minus fitted values
SSE = sum(r.^2); % SSE is the sum of squared errors
MSE=SSE/(n-2); % Mean Squared Error
Code_Intercept_SE=[ % Standard Error of the regression coefficients
sqrt(MSE*sum(X.^2)/(n*Sxx)); % Standard Error of the intercept coefficient
sqrt(MSE/Sxx)] % Standard Error of the slope coefficient
model2 = fitlm(X,Y,'Intercept',true); % Calls 'fitlm'
MATLAB_Intercept_SE=model2.Coefficients.SE % Queries the standard error of MATLAB's model
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Analysis of Variance and Covariance en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!