Re: 2 Way Linear Table regression
- From: "Wes" <wjltemp-gg@xxxxxxxxx>
- Date: 27 Aug 2006 09:51:01 -0700
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.
.
- Follow-Ups:
- Re: 2 Way Linear Table regression
- From: Veli-Pekka Nousiainen
- Re: 2 Way Linear Table regression
- References:
- 2 Way Linear Table regression
- From: Harold Climer
- Re: 2 Way Linear Table regression
- From: Wes
- Re: 2 Way Linear Table regression
- From: Veli-Pekka Nousiainen
- 2 Way Linear Table regression
- Prev by Date: Re: HP50g vs. Voyage 200
- Next by Date: Re: 49g+ vs. 50g
- Previous by thread: Re: 2 Way Linear Table regression
- Next by thread: Re: 2 Way Linear Table regression
- Index(es):
Relevant Pages
|