Hagen Schmöller
2003-Mar-19 10:35 UTC
[R] adjusting a periodical function to a time series
Hi R-Community, is there a possibility to adjust a periodical function like s(t) = a1 sin(f1 t) + b1 cos(f1 t) + a2 sin(f2 t) + b2 cos(f2 t) + ... to a time series with R, if necessary with given frequencies fi. Much thanks in advance, Hagen Schmoeller -- Dipl.-Ing. Hagen K. Schm?ller Institut f?r Elektrische Anlagen und Energiewirtschaft, RWTH Aachen Schinkelstra?e 6, D-52056 Aachen, Germany Tel.: +49 (0)241 80-96734 Fax : +49 (0)241 80-92197 Hagen.Schmoeller at iaew.rwth-aachen.de
Adrian Trapletti
2003-Mar-19 11:35 UTC
[R] adjusting a periodical function to a time series
> Subject: [R] adjusting a periodical function to a time series > Date: Wed, 19 Mar 2003 11:35:37 +0100 > From: Hagen Schm?ller<Hagen.Schmoeller at iaew.rwth-aachen.de> > To: <r-help at stat.math.ethz.ch> > > Hi R-Community, > > is there a possibility to adjust a periodical function like > > s(t) = a1 sin(f1 t) + b1 cos(f1 t) + a2 sin(f2 t) + b2 cos(f2 t) + ... > > to a time series with R, if necessary with given frequencies fi. > > Much thanks in advance, > > Hagen Schmoeller > -- > Dipl.-Ing. Hagen K. Schm?ller > Institut f?r Elektrische Anlagen und Energiewirtschaft, RWTH Aachen > Schinkelstra?e 6, D-52056 Aachen, Germany > Tel.: +49 (0)241 80-96734 > Fax : +49 (0)241 80-92197 > Hagen.Schmoeller at iaew.rwth-aachen.deMaybe something like this: data(lh) y <- lh t <- 1:length(y) err <- function(par) { x <- par[1]+par[2]*sin(t)+par[3]*cos(t) sum((y-x)^2) } op <- optim(c(0,0,0), err) op$par plot(lh) lines(op$par[1]+op$par[2]*sin(t)+op$par[3]*cos(t), col="red") best Adrian -- Dr. Adrian Trapletti Phone : +41 (0) 1 994 5631 Trapletti Statistical Computing Mobile: +41 (0)76 370 5631 Wildsbergstrasse 31 Fax : +41 (0) 1 994 5631 CH-8610 Uster Email : mailto:a.trapletti at bluewin.ch Switzerland WWW : http://trapletti.homelinux.com