HP Forums

Full Version: (38G) LSQS Linear Fit & Multiple Linear Regression Programme
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So here we have a useful programme called LSQS.

If you have a body of n vector observations & wish to fit your data in some linear relationship

a*V1+b*V2+c*V3.....=V

enter V1 in the first column of M1, V2 in the second column of M1....Vn in the nth column & V in the n+1th column.

The programme LSQS calculates the values of co-efficients a,b,c.... such that the errors in the resulting values of V are minimised.

The co-efficients are returned in Ans in Home view.

LSQS

SIZE(M1)►L1:
Ans(1)►R:
L1(2)►C:
M1►M2:
DELCOL M2;C:
SUB M3;M1;{1,C};{R,C}:
TRN(M2):
INVERSE(Ans*M2)*Ans*(M3*[1]):
If you want a constant in your linear regression like this

C+a*V1+b*V2+c*V3.....=V

then use the programme MLR below.

Constant & regression co-efficients are returned in Ans & M5, your data remains unchanged in M1.

MLR

M1►M4:
SIZE(M1):
MAKEMAT(1,Ans(1),1)*[1]:
ADDCOL M1;Ans;1:
RUN LSQS:
Ans►M5:
M4►M1:
M5:
Reference URL's