理解Matlab中的回归函数

时间:2021-10-16 18:08:36

I'm having trouble understanding how regress works in Matlab.

我在理解回归在Matlab中是如何工作的方面有困难。

Say I have 2 arrays (X and Y), each having the same size (let's say they're each 1x10). From what I understand, the regress function should help me find the relationship between X and Y (I want to draw a best fit line through the plotted data), and then give me the slope. When I tried this in Matlab, I got an error saying that the 2 variables have a different number of rows....but they don't, do they?

假设我有两个数组(X和Y),每个都有相同的大小(假设它们都是1x10)。根据我的理解,回归函数应该能帮助我找到X和Y之间的关系(我想通过绘制的数据画一条最合适的线),然后给出斜率。当我试着在Matlab中,我得到了一个错误,两个变量有不同的行数....但他们没有,不是吗?

I would just really appreciate it if anyone could help me understand how the function and its parameters work, just to get me going at a basic level.

如果有人能帮助我理解函数及其参数是如何工作的,我将非常感激,这样才能让我达到一个基本的水平。

Here's some code as an example:

这里有一些代码作为例子:

x = [1,2,3,4,5,6,7,8,9,10];
y = [1,2,3,4,5,6,7,8,9,10]; %defining the arrays, they are linearly related
X=[x ones(size(x,1),1)]; %adding the (necessary?) column of ones
regress(y,X) % using the regress function for a relationship

I get this error:

我得到这个错误:

??? Error using ==> regress at 64
The number of rows in Y must equal the number of rows in X.

2 个解决方案

#1


2  

I think that you're confusing rows with columns somehow (Matlab uses column-major ordering). If you print out your two inputs, y and X, you'll see immediately that they're row vectors of different lengths. Read the help/documentation for regress carefully – the first input must be an N-by-1 column vector. And the second an N-by-p matrix. Therefore something like this could work:

我认为你把行和列搞混了(Matlab使用列-主序)。如果你打印出两个输入,y和X,你会马上看到它们是不同长度的行向量。仔细阅读帮助/文档以进行回归——第一个输入必须是一个n×1列向量。第二个是n×p矩阵。因此,类似这样的东西可以起作用:

x = 1:10;
y = 1:10;
X = [x; ones(1,length(x))];
b = regress(y.',X.')

#2


1  

regress is for multiple linear regression. You just want to find relation between X and Y. For that polyfit command should be enough. I think the column of ones is necessary only when you want to calculate statistics.

回归是多重线性回归。你只需要找到X和y之间的关系,因为这个polyfit命令就足够了。我认为只有当你想要计算统计数据时才需要1的列。

From MATLAB documentation:

从MATLAB文档:

regress is for multiple linear regression. You just want to find relation between X and Y. For that polyfit command should be enough. I think the column of ones is necessary only when you want to calculate statistics.

回归是多重线性回归。你只需要找到X和y之间的关系,因为这个polyfit命令就足够了。我认为只有当你想要计算统计数据时才需要1的列。

You will use regress when you want to find out how Z behaves with respect to X and Y. In short, Z=f(X,Y). In this case, you will plug Z as a nx1 vector (first argument in regress command). Then you form another matrix, say D=[X Y]. This is a nx2 vector. This will be the second argument for the regress command.

当你想知道Z在X和Y上的表现时,你会用回归法,简而言之,Z=f(X,Y)在这种情况下,将Z作为一个nx1向量(回归命令中的第一个参数)。然后形成另一个矩阵,D=[X Y]这是一个nx2向量。这将是后退命令的第二个参数。

Now read this from MATLAB docs again, see if it makes sense:

现在再读一遍MATLAB文档,看看是否有意义:

b = regress(y,X) returns a p-by-1 vector b of coefficient estimates for a multilinear regression of the responses in y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. y is an n-by-1 vector of observed responses.

b =回归(y,X)返回一个p-by-1的系数估计向量b,用于对y中预测因子的响应进行多重线性回归。y是观察到的响应的一个n×1向量。

#1


2  

I think that you're confusing rows with columns somehow (Matlab uses column-major ordering). If you print out your two inputs, y and X, you'll see immediately that they're row vectors of different lengths. Read the help/documentation for regress carefully – the first input must be an N-by-1 column vector. And the second an N-by-p matrix. Therefore something like this could work:

我认为你把行和列搞混了(Matlab使用列-主序)。如果你打印出两个输入,y和X,你会马上看到它们是不同长度的行向量。仔细阅读帮助/文档以进行回归——第一个输入必须是一个n×1列向量。第二个是n×p矩阵。因此,类似这样的东西可以起作用:

x = 1:10;
y = 1:10;
X = [x; ones(1,length(x))];
b = regress(y.',X.')

#2


1  

regress is for multiple linear regression. You just want to find relation between X and Y. For that polyfit command should be enough. I think the column of ones is necessary only when you want to calculate statistics.

回归是多重线性回归。你只需要找到X和y之间的关系,因为这个polyfit命令就足够了。我认为只有当你想要计算统计数据时才需要1的列。

From MATLAB documentation:

从MATLAB文档:

regress is for multiple linear regression. You just want to find relation between X and Y. For that polyfit command should be enough. I think the column of ones is necessary only when you want to calculate statistics.

回归是多重线性回归。你只需要找到X和y之间的关系,因为这个polyfit命令就足够了。我认为只有当你想要计算统计数据时才需要1的列。

You will use regress when you want to find out how Z behaves with respect to X and Y. In short, Z=f(X,Y). In this case, you will plug Z as a nx1 vector (first argument in regress command). Then you form another matrix, say D=[X Y]. This is a nx2 vector. This will be the second argument for the regress command.

当你想知道Z在X和Y上的表现时,你会用回归法,简而言之,Z=f(X,Y)在这种情况下,将Z作为一个nx1向量(回归命令中的第一个参数)。然后形成另一个矩阵,D=[X Y]这是一个nx2向量。这将是后退命令的第二个参数。

Now read this from MATLAB docs again, see if it makes sense:

现在再读一遍MATLAB文档,看看是否有意义:

b = regress(y,X) returns a p-by-1 vector b of coefficient estimates for a multilinear regression of the responses in y on the predictors in X. X is an n-by-p matrix of p predictors at each of n observations. y is an n-by-1 vector of observed responses.

b =回归(y,X)返回一个p-by-1的系数估计向量b,用于对y中预测因子的响应进行多重线性回归。y是观察到的响应的一个n×1向量。