MATLAB:线性回归

发布于 2025-01-06 02:38:52 字数 738 浏览 0 评论 0原文

我正在尝试找出最有效的方法来查找数据集的线性回归方程 (y = mx + c),给定 2 × n 数组。

基本上我想知道当 X 为 50 时 Y 的值是多少。

我当前的方法还有很多不足之处:

inputData 是我的 2 × n 数组,X 在第一列,Y 在第二列。

x = 50

for i = 1 : size(inputData,1) % for every line in the inputData array
    if (inputData(i,1) < x + 5) | (inputData(i,1) > x - 5) % if we're within 5 of the specified X value
         arrayOfCloseYValues(i) = inputData(i, 2); % add the other position to the array
    end
end
y = mean(arrayOfCloseYValues) % take the mean to find Y

正如您所看到的,我的上述方法只是尝试查找给定 X 值 5 以内的 Y 值并获取平均值。这是一种糟糕的方法,而且需要很长时间才能处理。

我真正需要的是一种强大的方法来计算 X 和 Y 的线性回归,以便我可以通过方程 y = mx + c...

PS 找到该值。在我上面的方法中,我实际上预先分配了内存并删除了末尾的尾随零,但为了简单起见,我删除了这部分。

I'm trying to work out the most efficient method to find the linear regression equation (y = mx + c) for a dataset, given a 2 by n array.

Basically I want to know what the value of Y is when X is, for example, 50.

My current method leaves a lot to be desired:

inputData is my 2 by n array, with X in the first column and Y in the second.

x = 50

for i = 1 : size(inputData,1) % for every line in the inputData array
    if (inputData(i,1) < x + 5) | (inputData(i,1) > x - 5) % if we're within 5 of the specified X value
         arrayOfCloseYValues(i) = inputData(i, 2); % add the other position to the array
    end
end
y = mean(arrayOfCloseYValues) % take the mean to find Y

As you can see, my above method simply tries to find values of Y that are within 5 of the given X value and gets the mean. This is a terrible method, plus it takes absolutely ages to process.

What I really need is a robust method for calculating the linear regression for X and Y, so that I can find the value through the equation y = mx + c...

PS. In my above method I do actually pre-allocate memory and remove trailing zeros at the end, but I have removed this part for simplicity.

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

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

发布评论

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

评论(1

温折酒 2025-01-13 02:38:52

Polyfit 很好,但我认为你的问题更简单一些。你有一个 2 xn 的数据数组。假设第 1 列是 y,第 2 列是 x,那么:

y = inputData(:,1);
x = inputData(:,2);
b = ones(size(inputData));
A = [x b];
c = A\y

应该为您提供斜率和偏移的最小二乘回归。

这是测试它的另一种方法:

x = transpose(0:10);
y = 0.5*x + 1 + 0.1*randn(size(x)); % as a test, m = 0.5, b=1, and add some noise
A = [x ones(size(x))];
c = A\y;
yest = c(1)*x + c(2);
plot(x,yest,x,y)
legend('y_{est}','y')

应该让你:
估计 Y 与实际 Y

Polyfit is fine, but I think you're problem is a bit simpler. You have a 2 x n array of data. Let's say column 1 is y and column 2 is x, then:

y = inputData(:,1);
x = inputData(:,2);
b = ones(size(inputData));
A = [x b];
c = A\y

Should give you a least squares regression for the slope and offset.

Here's another way to test it:

x = transpose(0:10);
y = 0.5*x + 1 + 0.1*randn(size(x)); % as a test, m = 0.5, b=1, and add some noise
A = [x ones(size(x))];
c = A\y;
yest = c(1)*x + c(2);
plot(x,yest,x,y)
legend('y_{est}','y')

Should get you:
Estimated Y v Actual Y

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