5
votes

I'm having trouble understanding how regress works in 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?

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

2 Answers

2
votes

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:

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

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.

From MATLAB documentation:

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.

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.

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

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.