Re: 2 Way Linear Table regression



Veli-Pekka Nousiainen wrote:

Are you asking for a program that will calculate a best-fit linear
(plane) function of two variables, like z = a*x + b*y + c ? If so, I
wrote a small UserRPL program (~350 byte) that will do this.
Actually, it will do linear regression on an arbitrary number of
variables, y = a_1 * x_1 + a_2 * x_2 + a_3 * x_3 + ... + a_n *
x_n + a_0

If this is what you're looking for, it's small enough to just post
here.

Post it anyway...
Thanks,
Veli-Pekka

Well......., okay, you talked me into it.

The 49g+ User's Guide (p. 18-56) has a tiny program that also does a
linear least-squares fit on multiple variables. However, the method it
uses involves multiplying matrices of size (D+1)xN by another of size
Nx(D+1) where D is the number of independent variables and N is the
number of data points. I thought since N could be quite large, this
might not be too efficient. I decided to write my own program using
lists. Looking at it now, it's probably not any more efficient, but I
use it for the same reason that any programmer uses his own program --
because I wrote it. :-)

To compare the two methods, I replace my list method with the matrix
method from the User's Guide. The list method seems to be faster for
large N, while the smaller matrix method is faster for smaller N. Take
your pick below:


@ MLinFit(datamatrix, {list of columns}) - multi var linear fit
@ Wes Loewer
@
@ parameters:
@ data - matrix of data
@ cols - list of columns {x1, x2, x3,..., y}
@
@ returns an array of coefficients in order of x's listed in
@ the cols list followed by the constant last.
@ [ a1 a2 a3 ... a0]
@ y = a_1*x_1 + a_2*x_2 + a_3*x_3 + ... a_d*x_d + a0
@ I put the constant at the end to be consistent with PEVAL

Save either of the following two programs as MLinFit.

using lists:
%%HP: T(3)A(R)F(.);
\<< \-> data cols
\<< PUSH -105. SF cols SIZE \-> d
\<< 1. d
FOR k data 'cols' k GET COL- AXL NIP
NEXT d ROLLD DUP AXL 1. CON AXL d \->LIST \-> yl xll
\<< 1. d
FOR k yl 'xll' k GET * \GSLIST
NEXT d \->LIST AXL 1. d
FOR I 1. d
FOR J 'xll' I GET 'xll' J GET * \GSLIST
NEXT
NEXT d DUP 2. \->LIST \->ARRY /
\>>
\>> POP
\>>
\>>

using only matrices:
%%HP: T(3)A(R)F(.);
\<< \-> data cols
\<< PUSH -105. SF cols SIZE \-> d
\<< data 'cols' d GET COL- NIP data 'cols' 1. GET COL- NIP OBJ\->
1. + \->ARRY
IF d 2. >
THEN 2. d 1. -
FOR k data 'cols' k GET COL- NIP k COL+
NEXT
END DUP SIZE 1. 1. SUB 1. CON d COL+ DUP TRAN DUP ROT * INV SWAP
* SWAP *
\>> POP
\>>
\>>


Since the data often resides in SigmaDAT (\GSDAT), you can use the
following wrapper program for convinience.

@ \GSMLinFit() - multi var linear fit using all of \GSDAT
@
@ assumes that the last column of \GSDAT is the y-column
@ and all the previous ones are the x-columns.

Save the following program as \GSMLinFit

%%HP: T(3)A(R)F(.);
\<< \GSDAT { } 1. PICK3 SIZE 2. GET
FOR k k +
NEXT MLinFit
\>>

Notice that \GSMLinFit takes no arguments but simply uses the values in
\GSDAT.

Enjoy,
-wes
PS. I also have a similar best-fit polynomial program if anybody's
interested.

.



Relevant Pages

  • Re: Linear Algebra Done Right
    ... I really enjoyed Sheldon Axler's book "Linear Algebra Done Right". ... lists of vectors, rather than just considering them as sets. ... cumbersome for students to use in writing formal proofs, and Axler ... I assume the lists play a role especially with linear dependency and ...
    (sci.math)
  • Re: vector difference
    ... being suggested are quadratic for a well/over researched problem that has ... will remove all copies of any matching element. ... With linear traversal through two sorted lists one can ...
    (comp.lang.fortran)
  • Re: Create subspace from Basis : Maple
    ... I have the Basis vectors of a subspace. ... subspace (all possible linear combinations) from these basis vectors in ... implemented as lists or Vectors, then the span of a set of vectors V ...
    (sci.math.symbolic)
  • Re: Speed revisited
    ... Both essentially use a loop with ... >> snippet 2 seems after initial slowdown looks exactly linear, ... dictionaries, lists, sets, etc. ... speeds regardless of what length of tape was already read. ...
    (comp.lang.python)
  • Re: Manager Summaries
    ... Assume the source list is in sheet: ... Using empty cols to the right of the data, ... Joe Bloggs ... lists for all the other Mgrs. ...
    (microsoft.public.excel)