How to find residual variance from fitlm

7 visualizzazioni (ultimi 30 giorni)
Dylan
Dylan il 29 Ago 2022
Commentato: Dylan il 30 Ago 2022
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

Risposta accettata

Torsten
Torsten il 29 Ago 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)
x = 1×60
353.0900 353.6500 353.8100 354.2800 362.6900 373.4600 373.9500 373.9900 375.0700 375.2900 380.5100 385.8300 386.0800 387.7100 387.8500 388.6500 389.7600 412.4700 414.9200 419.4500 421.9400 427.5300 428.0300 429.7400 429.9400 438.4600 439.1800 443.0900 443.2700 444.6800
ix = 1×60
14 1 37 6 18 45 58 48 54 8 39 3 11 15 35 52 2 38 22 4 20 5 29 28 33 30 36 19 60 10
y = y(ix)
y = 1×60
0.4596 0.3243 0.3652 0.4091 0.2438 0.1557 0.0208 0.0815 0.4106 0.2414 0.2840 0.1632 0.3978 0.3441 0.0638 0.1689 0.0518 0.3190 0.1775 0.4561 0.2630 0.3791 0.4128 0.2592 0.3112 0.1562 0.2652 0.3008 0.3574 0.3263
plot(x,y)
A = [ones(numel(x),1),x.'];
b = y.';
c = A\b;
ans = 60
format long
%Residual variance for linear regression model
var(y-(c(1)+c(2)*x))*(numel(x)-1)/(numel(x)-2)
ans =
0.019752198890146
%% fit linear model
xy_lm=fitlm(x,y)
xy_lm =
Linear regression model: y ~ 1 + x1 Estimated Coefficients: Estimate SE tStat pValue ___________________ ____________________ _________________ ____________________ (Intercept) -0.15828398677887 0.10942366710812 -1.44652423887853 0.153413107689524 x1 0.00114045590675123 0.000230521588155199 4.94728461606562 6.79762178610595e-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
%Residual variance = MeanSq of Residual
anova(xy_lm,'summary')
ans = 3×5 table
SumSq DF MeanSq F pValue _________________ __ __________________ ________________ ___________________ Total 1.62907495001833 59 0.0276114398308192 Model 0.483447414389884 1 0.483447414389884 24.4756250723596 6.7976217861058e-06 Residual 1.14562753562845 58 0.0197521988901457
  3 Commenti
Torsten
Torsten il 29 Ago 2022
Modificato: Torsten il 29 Ago 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')
Dylan
Dylan il 30 Ago 2022
Ohhh thankyou! I didnt realise that the annova table gives you residual

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by