How to find residual variance from fitlm
7 views (last 30 days)
Show older comments
Hi, I was wondering how you read the fitlm output after using data to get the residual variance. I thought that it might be the R-squared output or the Adjusted R-squared but apparently that is not the case. here is an example with the output
%% data
x=[353.65 389.76 385.83 419.45 427.53 354.28 570.05 375.29 530.73 444.68 386.08 490.73 595.65 353.09 387.71 565.55 548.73 362.69 443.09 421.94 510.90 414.92 471.25 530.13 566.98 514.40 526.64 429.74 428.03 438.46 550.17 505.43 429.94 594.41 387.85 439.18 353.81 412.47 380.51 581.10 445.24 528.21 534.49 551.51 373.46 448.30 564.95 373.99 595.85 509.58 589.94 388.65 508.03 375.07 596.75 563.80 502.94 373.95 545.65 443.27];
y=[0.32432 0.05176 0.16317 0.45606 0.37908 0.40913 0.52598 0.24144 0.31293 0.32629 0.39778 0.21505 0.31383 0.45964 0.34407 0.55298 0.40881 0.24382 0.30078 0.26296 0.70163 0.17748 0.38921 0.41283 0.47194 0.64746 0.64146 0.25919 0.41283 0.15618 0.29238 0.61447 0.31118 0.53470 0.06382 0.26518 0.36523 0.31896 0.28402 0.68385 0.69655 0.45083 0.41407 0.45839 0.15570 0.59098 0.48110 0.08150 0.51529 0.29448 0.34008 0.16886 0.31172 0.41063 0.41504 0.54690 0.44654 0.02075 0.68367 0.35737];
%% fit lenear models
xy_lm=fitlm(x,y)
output:
xy_lm =
Linear regression model:
y ~ 1 + x1
Estimated Coefficients:
Estimate SE tStat pValue
_________ __________ _______ __________
(Intercept) -0.15828 0.10942 -1.4465 0.15341
x1 0.0011405 0.00023052 4.9473 6.7976e-06
Number of observations: 60, Error degrees of freedom: 58
Root Mean Squared Error: 0.141
R-squared: 0.297, Adjusted R-Squared: 0.285
F-statistic vs. constant model: 24.5, p-value = 6.8e-06
0 Comments
Accepted Answer
Torsten
on 29 Aug 2022
%% data
x = [353.65 389.76 385.83 419.45 427.53 354.28 570.05 375.29 530.73 444.68 386.08 490.73 595.65 353.09 387.71 565.55 548.73 362.69 443.09 421.94 510.90 414.92 471.25 530.13 566.98 514.40 526.64 429.74 428.03 438.46 550.17 505.43 429.94 594.41 387.85 439.18 353.81 412.47 380.51 581.10 445.24 528.21 534.49 551.51 373.46 448.30 564.95 373.99 595.85 509.58 589.94 388.65 508.03 375.07 596.75 563.80 502.94 373.95 545.65 443.27];
y = [0.32432 0.05176 0.16317 0.45606 0.37908 0.40913 0.52598 0.24144 0.31293 0.32629 0.39778 0.21505 0.31383 0.45964 0.34407 0.55298 0.40881 0.24382 0.30078 0.26296 0.70163 0.17748 0.38921 0.41283 0.47194 0.64746 0.64146 0.25919 0.41283 0.15618 0.29238 0.61447 0.31118 0.53470 0.06382 0.26518 0.36523 0.31896 0.28402 0.68385 0.69655 0.45083 0.41407 0.45839 0.15570 0.59098 0.48110 0.08150 0.51529 0.29448 0.34008 0.16886 0.31172 0.41063 0.41504 0.54690 0.44654 0.02075 0.68367 0.35737];
[x,ix] = sort(x)
y = y(ix)
plot(x,y)
A = [ones(numel(x),1),x.'];
b = y.';
c = A\b;
format long
%Residual variance for linear regression model
var(y-(c(1)+c(2)*x))*(numel(x)-1)/(numel(x)-2)
%% fit linear model
xy_lm=fitlm(x,y)
%Residual variance = MeanSq of Residual
anova(xy_lm,'summary')
3 Comments
Torsten
on 29 Aug 2022
Edited: Torsten
on 29 Aug 2022
You wanted the residual variance - I gave you two ways to calculate it:
A = [ones(numel(x),1),x.'];
b = y.';
c = A\b;
format long
%Residual variance for linear regression model
var(y-(c(1)+c(2)*x))*(numel(x)-1)/(numel(x)-2)
or with fitlm
%% fit linear model
xy_lm=fitlm(x,y);
%Residual variance = MeanSq of Residual
anova(xy_lm,'summary')
More Answers (0)
See Also
Categories
Find more on Linear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!