Greetings, I have several sets of oscillation data and would like to estimate the parameters of a sine function to each set (and hopefully automate this). A colleague provided an excel sheet that uses solver to minimize the RSS after fitting the sine function to each data set, but this cumbersome and difficult to automate. Is there a method in R for fitting a given sine function to a supplied data using maximum likelihood estimation (or minimizing the RSS). Thanks in advance. -- Benjamin Zuckerberg, Ph.D. Post-doctoral Associate Spatial Ecologist, Citizen Science Cornell Laboratory of Ornithology 159 Sapsucker Woods Road Ithaca, NY 14850 Tele: 607-254-2174 Fax: 607-254-2111
See e.g. http://finzi.psych.upenn.edu/R/Rhelp02a/archive/131024.html RSiteSearch() produced this and similar relevant past postings. On Thu, 20 Nov 2008, Ben Zuckerberg wrote:> Greetings, > > I have several sets of oscillation data and would like to estimate the > parameters of a sine function to each set (and hopefully automate this). A > colleague provided an excel sheet that uses solver to minimize the RSS after > fitting the sine function to each data set, but this cumbersome and difficult > to automate. Is there a method in R for fitting a given sine function to a > supplied data using maximum likelihood estimation (or minimizing the RSS). > Thanks in advance. > > -- > Benjamin Zuckerberg, Ph.D. > Post-doctoral Associate > Spatial Ecologist, Citizen Science > Cornell Laboratory of Ornithology > 159 Sapsucker Woods Road > Ithaca, NY 14850 Tele: 607-254-2174 > Fax: 607-254-2111 > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Ben Zuckerberg schrieb:> Greetings, > > I have several sets of oscillation data and would like to estimate the > parameters of a sine function to each set (and hopefully automate > this). A colleague provided an excel sheet that uses solver to minimize > the RSS after fitting the sine function to each data set, but this > cumbersome and difficult to automate. Is there a method in R for > fitting a given sine function to a supplied data using maximum > likelihood estimation (or minimizing the RSS). Thanks in advance. >Hi Ben, why not using FFT? See one of the examples below. HTH Thomas P. ### Some periodic data ---------------------------- n <- 360 ord <- 180 x <- 0:(n-1) y <- sin((50 + x) * 2 * pi / n) + rnorm(n) * 0.1 plot(x, y,xlim=c(-10,370)) ### example 1 ------------------------------------- ## Fast Fourier Transform pf <- fft(y) ## Plot highest possible order pf[(n/2):n] <- 0 yy <- 2*Re(fft(pf, inverse=TRUE)/n) - Re(pf[1])/n lines(x, yy, col="gray", lwd=1) ### example 2 ------------------------------------- ## Fast Fourier Transform pf <- fft(y) ## another, lower order pf[4:n] <- 0 yy <- 2*Re(fft(pf, inverse=TRUE)/n) - Re(pf[1])/n lines(x, yy, col="blue", lwd=2) ### example 3 ------------------------------------- ## Fast Fourier Transform pf <- fft(y) ## synthesize it the traditional way n <- length(y) a0 <- Re(pf[1])/n pf <- pf[-1] a <- 2*Re(pf)/n b <- -2*Im(pf)/n harmonic <- function(x, a0, a, b, n, ord) { k <- (2 * pi * x/n) %*% t(1:ord) y <- a0 + cos(k) %*% a[1:ord] + sin(k) %*% b[1:ord] y } lines(x, harmonic(x, a0, a, b, n, ord=2), col="red", lty=2, lwd=2)
Ben Zuckerberg <bz73 <at> cornell.edu> writes:> I have several sets of oscillation data and would like to estimate the > parameters of a sine function to each set (and hopefully automate > this).There is an example using lme (yes, LINEAR) fit on page 239 of Pinheiro/Bates Mixed Effects Book (ovarian cycle). If you do not have the book at hand, have a look at library/nlme/scripts/ch05.r. Dieter
To follow Dieter's comment, You can in fact fit a data to a sine in Excel using LINEST. I've done it. I don't recommend it :-) . What I did was create columns containing sin(x) and cos(x) , roughly speaking, and fit using LINEST([y=values],{sines, cosines},...) Ya need the cosines or something similar to get rid of phase ambiguity. But using either FFTs or glm() should do fine. And stay away from Excel! :-( Carl