MATLAB:存储在矩阵中的数据的线性回归(用于循环?)

发布于 2025-02-06 14:29:44 字数 1107 浏览 2 评论 0原文

我尝试对存储在多个矩阵(MATLAB)中的数据进行线性回归。对于向量,线性回归非常简单:

%x-values
x = [1.5;2.5;5];                                        %x-vector
          
%y-values
y = [9;11.1;17];                                        %y-vector

%Regression:
X = [ones(length(x),1) x]                               %Matrix to do the backslash calculation--> how can I set that up if I have a matrix for x and not a vector?
m2 = X\y;                                               %backslash calculation --> first value: intercept, second value: slope
yCalc = X*m2;                                           %linear regression
Rsq2 = 1 - sum((y - yCalc).^2)/sum((y - mean(y)).^2);   %R² value

在我的情况下,x-和y-data存储在矩阵中:

X = [a1 b1 c1;
     a2 b2 c2;
     a3 b3 c3]

Y = [A1 B1 C1;
     A2 B2 C2;
     A3 B3 C3]

必须在矩阵x(x-values:a1 a2 a3)和矩阵y的第一列(相应的y值:A1 A2 A3),然后以所有其他列的方式(2 ... i )。我已经尝试了几种不同的方法(主要是横面)。最后,我总是弄乱设置1 ... i 矩阵,其中包含第一列中的矩阵和第1列的x-data ... i (矩阵x)在第二列中运行后斜线操作,这是获得回归的截距和斜率所需的。最后,我想为我所做的每一次回归存储变量M2,YCALC和RSQ2。谁能帮忙?那将非常有帮助!非常感谢!

I try to do a linear regression for data that is stored in several matrices (Matlab). For vectors, the linear regression is pretty straight forward:

%x-values
x = [1.5;2.5;5];                                        %x-vector
          
%y-values
y = [9;11.1;17];                                        %y-vector

%Regression:
X = [ones(length(x),1) x]                               %Matrix to do the backslash calculation--> how can I set that up if I have a matrix for x and not a vector?
m2 = X\y;                                               %backslash calculation --> first value: intercept, second value: slope
yCalc = X*m2;                                           %linear regression
Rsq2 = 1 - sum((y - yCalc).^2)/sum((y - mean(y)).^2);   %R² value

In my case the x- and y-data is stored in matrices:

X = [a1 b1 c1;
     a2 b2 c2;
     a3 b3 c3]

Y = [A1 B1 C1;
     A2 B2 C2;
     A3 B3 C3]

The linear regression has to be done between the first column in matrix X (x-values: a1 a2 a3) and the first column of matrix Y (respective y-values: A1 A2 A3) and then for all the other columns in the same way (2...i). I already tried a couple of different approaches (mainly for-loops). In the end, I always mess up with setting up 1...i matrices that contain ones in the first column and the x-data of column 1...i (Matrix X) in the second column to run the backslash operation which is needed to obtain the intercept and slope for the regression. IN the end, I would like to store the variables m2,yCalc and Rsq2 for every regression that I did. Can anyone help? That would be very helpful! Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十年不长 2025-02-13 14:29:44

使用函数文件以组织您的程序。放入

function [m2 yCalc Rsq2] = lin_reg(x, y)
  X = [ones(length(x),1) x];
  m2 = X\y;
  yCalc = X*m2;
  Rsq2 = 1 - sum((y - yCalc).^2)/sum((y - mean(y)).^2);
end

文件lin_reg.m中。所有lin_reg从脚本

X = [1 10 100;
     2 20 200;
     3 30 300];

Y = [4 40 400;
     5 50 500;
     7 70 600];

[m, n] = size(X);
m2 = zeros(2, n);
yCalc = zeros(m, n);
Rsq2 = zeros(1, n);
for k=1:n
  [m2(:, k) yCalc(:, k) Rsq2(k)] = lin_reg(X(:, k), Y(:, k));
end

m2
yCalc
Rsq2

输出

m2 =

    2.3333   23.3333  300.0000
    1.5000    1.5000    1.0000


yCalc =

    3.8333   38.3333  400.0000
    5.3333   53.3333  500.0000
    6.8333   68.3333  600.0000


Rsq2 =

    0.9643    0.9643    1.0000

Use a function file to organize your program. Put

function [m2 yCalc Rsq2] = lin_reg(x, y)
  X = [ones(length(x),1) x];
  m2 = X\y;
  yCalc = X*m2;
  Rsq2 = 1 - sum((y - yCalc).^2)/sum((y - mean(y)).^2);
end

in a file lin_reg.m. Сall lin_reg from a script

X = [1 10 100;
     2 20 200;
     3 30 300];

Y = [4 40 400;
     5 50 500;
     7 70 600];

[m, n] = size(X);
m2 = zeros(2, n);
yCalc = zeros(m, n);
Rsq2 = zeros(1, n);
for k=1:n
  [m2(:, k) yCalc(:, k) Rsq2(k)] = lin_reg(X(:, k), Y(:, k));
end

m2
yCalc
Rsq2

outputs

m2 =

    2.3333   23.3333  300.0000
    1.5000    1.5000    1.0000


yCalc =

    3.8333   38.3333  400.0000
    5.3333   53.3333  500.0000
    6.8333   68.3333  600.0000


Rsq2 =

    0.9643    0.9643    1.0000
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文