Is it possible to extract also R^2 value from linear fit between 2 vectors ?
98 ビュー (過去 30 日間)
古いコメントを表示
Galina Machavariani
2020 年 1 月 14 日
回答済み: Lola Davidson
2025 年 11 月 25 日 21:28
Hello,
I know that it is possible to find fit parameters using polyfit command.
F.e., linearCoefficients = polyfit(x, y, 1)
Is it possible to extract also R^2 value from linear fit between 2 vectors ?
Thank you !
0 件のコメント
採用された回答
Star Strider
2020 年 1 月 14 日
It is, however polyfit wil not do it for you.
Try this:
x = 1:10; % Create ‘x’
y = randn(size(x)) + 0.2*x; % Create ‘y’
linearCoefficients = polyfit(x, y, 1); % Coefficients
yfit = polyval(linearCoefficients, x); % Estimated Regression Line
SStot = sum((y-mean(y)).^2); % Total Sum-Of-Squares
SSres = sum((y-yfit).^2); % Residual Sum-Of-Squares
Rsq = 1-SSres/SStot; % R^2
4 件のコメント
Star Strider
2020 年 1 月 14 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
その他の回答 (1 件)
Lola Davidson
2025 年 11 月 25 日 21:28
As of R2024a, polyfit will actually do this for you.
x = 1:10;
y = randn(size(x)) + 0.2*x;
[linearCoefficients,structOutput] = polyfit(x, y, 1);
Rsq = structOutput.rsquared;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Linear and Nonlinear Regression についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!