Hello R community, Does anyone know of a package that will perform cycle regression analysis? I have searched the R-help archives etc. but have come up with nothing so far. If I am unable to find an existing R package to do so, is there anyone familiar with fitting sine functions to data. My problem is this: I have a long time-series of daily SWE estimates (SWE = snow water equivalence, or the amount of water stored in a snowpack) which follows a sinusoidal pattern, and I need to estimate the parameters of the sine function that best fits this data. While there may be many contributing sine functions and/or linear trends, I am only interested in a single sine function that most closely fits the data (trends can be removed separately if need be). Perhaps some sort of non-linear least squares method would be best? Any help, or suggestions to get me on the right track are greatly appreciated. Carson
To do it from first principles using nonlinear optimization see: http://finzi.psych.upenn.edu/R/Rhelp02a/archive/20100.html On Jan 10, 2008 5:27 PM, Carson Farmer <cfarmer at uvic.ca> wrote:> Hello R community, > > Does anyone know of a package that will perform cycle regression > analysis? I have searched the R-help archives etc. but have come up with > nothing so far. > If I am unable to find an existing R package to do so, is there anyone > familiar with fitting sine functions to data. My problem is this: > I have a long time-series of daily SWE estimates (SWE = snow water > equivalence, or the amount of water stored in a snowpack) which follows > a sinusoidal pattern, and I need to estimate the parameters of the sine > function that best fits this data. While there may be many contributing > sine functions and/or linear trends, I am only interested in a single > sine function that most closely fits the data (trends can be removed > separately if need be). Perhaps some sort of non-linear least squares > method would be best? > > Any help, or suggestions to get me on the right track are greatly > appreciated. > > Carson > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
On Thu, 10 Jan 2008, Carson Farmer wrote:> Hello R community, > > Does anyone know of a package that will perform cycle regression > analysis? I have searched the R-help archives etc. but have come up with > nothing so far. > If I am unable to find an existing R package to do so, is there anyone > familiar with fitting sine functions to data. My problem is this: > I have a long time-series of daily SWE estimates (SWE = snow water > equivalence, or the amount of water stored in a snowpack) which follows > a sinusoidal pattern, and I need to estimate the parameters of the sine > function that best fits this data. While there may be many contributing > sine functions and/or linear trends, I am only interested in a single > sine function that most closely fits the data (trends can be removed > separately if need be). Perhaps some sort of non-linear least squares > method would be best?Or maybe stl? ( Seasonal Decomposition of Time Series by Loess ) Try example( stl ) and see the reference in ?stl HTH, Chuck p.s. Surely, SWE is NOT sinusoidal. At least not in my local mountains.> > Any help, or suggestions to get me on the right track are greatly > appreciated. > > Carson > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Here are a couple of thoughts, The basic idea of the sine function is: y = a + b * sin( c + d*x ) where: a is a vertical offset from 0 b is the amplitude c is the phase shift d is related to the period. You could put this function into nls or other non-linear optimization problem, however with a few assumptions this can be turned into a linear regression problem:>From the sound of it you already know the period you want to use (1 day or 1 year) so d is determined by that and does not need to be estimated. The other nonlinear parameter which may be of interest to estimate is c. The rule from trig is that:sin( c+dx ) = cos(c) * sin(dx) + sin(c) * cos(dx) so if you calculate sin(dx) (call this x1) and cos(dx) (call this x2) from fixed values and call b*cos(x) beta1 and b*sin(x) beta2 then the model becomes: y= a + beta1 * x1 + beta2 * x2 Now that is a linear regression model and you don't need to fight with non-linear issues, just calculate sin(dx) and cos(dx) and use those as the predictors. If you are interested in the values of b and c then you can use trig and algebra to back transform the estimates of beta1 and beta2 back to b and c (may not have a unique solution in all cases, but should be able to find a good approximation). Another option if you don't want to restrict yourself to sine curves (maybe it climbs faster than it drops) is periodic splines. See: http://www.biostat.wustl.edu/archives/html/s-news/1999-06/msg00235.html for some basic functions to do this. Hope this helps, ________________________________ From: r-help-bounces@r-project.org on behalf of Carson Farmer Sent: Thu 1/10/2008 3:27 PM To: r-help@r-project.org Subject: [R] Cycle Regression Analysis in R? Hello R community, Does anyone know of a package that will perform cycle regression analysis? I have searched the R-help archives etc. but have come up with nothing so far. If I am unable to find an existing R package to do so, is there anyone familiar with fitting sine functions to data. My problem is this: I have a long time-series of daily SWE estimates (SWE = snow water equivalence, or the amount of water stored in a snowpack) which follows a sinusoidal pattern, and I need to estimate the parameters of the sine function that best fits this data. While there may be many contributing sine functions and/or linear trends, I am only interested in a single sine function that most closely fits the data (trends can be removed separately if need be). Perhaps some sort of non-linear least squares method would be best? Any help, or suggestions to get me on the right track are greatly appreciated. Carson ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]