# [R] fitting cosine curve

Charles C. Berry ccberry at ucsd.edu
Wed Jun 21 06:24:18 CEST 2017

```On Tue, 20 Jun 2017, lily li wrote:

> Hi R users,
>
> I have a question about fitting a cosine curve. I don't know how to set the
> approximate starting values.

See

Y.L. Tong (1976) Biometrics 32:85-94

The method is known as `cosinor' analysis.  It takes advantage of the
*intrinsic* linearity of y = a + b * cos( omega*t - c ), when omega is
given.

It you are scratching your head saying 'that thing is not linear', you
need to go back to your linear models text and review what `linearity'
actually refers to.  Also, reading the Tong paper is recomended as you
will need the transformations given there in any case.

What you end up doing is fitting

fit <- lm(y~cos(t.times.omega)+sin(t.times.omega))

and then transforming coef(fit) to get back a, b, and c. So, you only need
to have omega. If it is not obvious what value to use,  then that will be
more of a challenge.

The paper gives asymptotics for the dispersion matrix of (a, b, c), I
recall.

> Besides, does the method work for sine curve as well?

Seriously? See

https://en.wikipedia.org/wiki/List_of_trigonometric_identities#Shifts_and_periodicity

HTH,

Chuck

```