[19134] in s-news-athena

home help back first fref pref prev next nref lref last post

Re: [S] query on a more efficient method for writing a nonlinear

daemon@ATHENA.MIT.EDU (Bill Venables)
Tue Sep 7 20:06:57 1999

Message-Id: <199909080000.KAA25075@snowy.nsw.cmis.CSIRO.AU>
To: "Gene Felber" <gfelber@rxttm.com>
Cc: s-news@wubios.wustl.edu
In-Reply-To: Your message of "Tue, 07 Sep 1999 08:49:58 -0400."
             <LOBBKJFMEMHLDHOLHNOFGENHCAAA.gfelber@rxttm.com> 
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Date: Wed, 08 Sep 1999 09:58:40 +1000
From: Bill Venables <venables@acland.qld.cmis.csiro.au>


> 
> Dear:
> 
> I am using the function below to fit an nls model:
> 
> sin.cos.fcn <- function(A,B,C1,C2,C3,C4,D1,D2,D3,D4,time)
> {
> 	A+B*time
> 	+ (C1 * sin(2*1*pi*time/52)) + (D1 * cos(2*1*pi*time/52))
> 	+ (C2 * sin(2*2*pi*time/52)) + (D2 * cos(2*2*pi*time/52))
> 	+ (C3 * sin(2*3*pi*time/52)) + (D3 * cos(2*3*pi*time/52))
> 	+ (C4 * sin(2*4*pi*time/52)) + (D4 * cos(2*4*pi*time/52))
> }
> 
> Is there a more efficient method to write this function so that one could
> more easily change the summation of k=1:4 to 1:n without having to
> explicitly write (and obtain estimates for) each parameter?

Eh?  This isn't a non-linear model at all, it's only a linear model, as you
should be delighted to discover.  The way I would handle such a model with
variable numbers of terms in the approximating fourier series would be as
follows:

sin.cos.fcn <- function(k, time) {
	X <- matrix(0, length(time), 2*k)
	for(i in 1:k) {
		X[,i] <- sin(i*2*pi*time/52)
		X[,k+i] <- cos(i*2*pi*time/52)
	}
	X
}

m0 <- lm(y ~ time                       , data = mycat)
m1 <- lm(y ~ time + sin.cos.fcn(1, time), data = mycat)
m2 <- lm(y ~ time + sin.cos.fcn(2, time), data = mycat)
&c

anova(m0, m1, m2, .....)

If you do have a genuinely nonlinear model there are very useful ways to use
vector (as opposed to scalar) parameters that should be better known than they
are, but that's another fascinating story...

Bill Venables.
-----------------------------------------------------------------
Bill Venables, Statistician, CMIS Environmetrics Project.

Physical address:                            Postal address:
CSIRO Marine Laboratories,                   PO Box 120,       
233 Middle St, Cleveland, Queensland         Cleveland, Qld, 4163
AUSTRALIA                                    AUSTRALIA

Telephone: +61 7 3826 7251     Email: Bill.Venables@cmis.csiro.au     
      Fax: +61 7 3826 7304


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

home help back first fref pref prev next nref lref last post