[R] how to fit a sine curve to those data

chiara.magliozzi at libero.it chiara.magliozzi at libero.it
Wed Feb 26 17:42:24 CET 2014


Hello R-users,
I am a biginner in R and I would like to fit a sinusoidal curve to my data. I 
couldn't get a nice results.

here is an extract of my data:
line  time      meters
1     1 04:27    3.1
2     2 10:48    0.9
3     3 16:49    2.9
4     4 23:00    1.0
5     5 05:03    3.1
6     6 11:29    1.0
7     7 17:26    2.8
8     8 23:42    1.1
9     9 05:42    3.0
10   10 12:14    1.1
11   11 18:09    2.7
12   12 00:31    1.2

the time is in hours. I have tried to run this scrip trying to fit my data by 
eye using a time every 6 hours (just to try with a fixed time)

TimeD <- c(0,6,12,18,24)
MetersD <- c(3.1,0.9,2.9,1,3.1)
AverageHigh <- (max(MetersD) + mean(MetersD)) / 2
AverageLow <- (min(MetersD) + mean(MetersD)) / 2
A <- (AverageHigh - AverageLow) / 2
K <- (AverageHigh + AverageLow) / 2
period <- mean(TimeD[c(-1,-2,-length(TimeD)-1,-length(TimeD))])
f <- 2 * pi / period
phi <- TimeD[2] + (pi / (2 * f))
Curve.plot <-(A * sin(f * (TimeD - phi))) + K


could somebody suggest me a way?It is becoming very complicated to get out 
from here.
Many thanks



More information about the R-help mailing list