MATLAB: Multiple linear regression explanation

MATLABmultiple linear regressionregressregressionscatter

Hi there I have been given code that has been used to produce a multiple regression line on a 2D plot, the code is provided below. I have got my head around what multiple regression is but am unsure exactly what is happening in the code to produce the regression line and what the two predictors being used to predict the response actually are. The arrays being used AFall(4,:) refers to an RR value and AFall(1,:) refers to a QTc value. These are labelled as the axis of the attached figure.
scatter(( AFall(4,:) ),AFall(1,:)); %plot QTc against RR
hold on;%keep the plotted data in the graph
%set bQTc coefficient estimates for y responses on x predictors
bQTc2 = regress(AFall(1,:)',[ones(length (AFall(1,:)),1) AFall(4,:)']);
%plot multiple linear regression response against RR
scatter(( AFall(4,:) ), bQTc2(1)+ bQTc2(2)*AFall(4,:),'xr');
legend('AF group'); %add label to graph
It is the line starting with bQTc2 that I am having the most trouble with, it creates two values for bQTc2 (I am guessing as coeffecients for the two x predictors). I think the first predictor bQTc2(1) is based solely on the RR values (please correct me if I am wrong) but I am not sure what the bQTc2(2) values is referenced to as I don't fully understand what is happening in the code.
If someone could provide information as to what exactly is happening in the code here I would be very grateful.
Many Thanks,
Ross

Best Answer

There is only a single regressor and a response variable so despite the comment in the code, this is NOT multiple regression (well, ok, technically it is but for the degenerate case of Nregressors==1).
The above code is identically(*) the same in result as would be
bQTc2 = polyfit(AFall(4,:)',AFall(1,:)',1); % fit QTc=bQTc2(1)*RR + bQTc2(2) <--> y=mx+b
%plot linear regression response against RR
scatter(AFall(4,:), polyval(bQTc2,AFall(4,:)),'xr') % put QTc_hat on plot as well...
All regress is doing is solving for the same coefficients of slope, interecept using a more general routine to do so but there's no data for more coefficients. polyfit supplies the needed column of ones to solve for the intercept term automagically whereas in regress, being a general routine, you have to supply the variables including the intercept term explicitly.
See
doc regress
doc polyfit
for details.
One can only presume that since there are at least four variables (rows) in the original array that possibly the original code did, in fact, try to regress the dependent variable on more than just the one variable; hence the comments and use of regress. It would appear that if so, the results weren't promising enough to have kept and the developer just pruned the additional variable(s) from the fitting expression and didn't modify the comments to match.
() Identical excepting the order of the coefficients is reversed; |*polyfit| returns coefficients for
p(x)=p(1)*x^n + p(2)*x^(n1) + ... + p(n)*x + p(n+1)
whereas the model matrix as written above for regress has the column of ones for the intercept first so that will be the first coefficient returned; iow, the order of bQTc2 values is reversed between the two formulations. That is taken care of as I wrote it as polyval is written as the boon companion of its brother and expects the coefficient array in the above order. NB: the explicit evaluation expression the original author wrote is reversed sense in keeping with the model as specified.