How does MATLAB calculate standard error in 'fitlm'?
16 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2017 年 12 月 13 日
回答済み: MathWorks Support Team
2017 年 12 月 20 日
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.
採用された回答
MathWorks Support Team
2017 年 12 月 13 日
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 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Analysis of Variance and Covariance についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!