[R] adjusting a periodical function to a time series

Adrian Trapletti adrian.trapletti at lmttrading.com
Wed Mar 19 12:35:06 CET 2003


> 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.de

Maybe 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



More information about the R-help mailing list