# R: Fit sinusoidal curve to timeseries, and interpolate at desired times

I have ocean tide data with just high and low tides. I need to fit an appropriate curve (sinusoidal presumably, maybe just polynomial) to this, and then extract values interpolated at specific times. Eg given the high/low tides, whats the tide height at 14:30 on 2016-05-17? (where that time falls within the span of the data).

So the period may vary, and there may be an overall trend, which is fine - tides are not perfect sine curves. The only requirement is that the high/low values be respected as the local extremes, so I don't interpolate values higher or lower for a given day.

Here's the sample data series, with the lines showing what we dont want -- linear interpolation between high and low values. I have here ~4 days of data, but additional data is available before and after, if helpful to improve fit.

                  time height
1  2016-05-15 02:42:00   1.09
2  2016-05-15 08:31:00   0.31
3  2016-05-15 15:04:00   1.02
4  2016-05-15 20:51:00   0.30
5  2016-05-16 03:31:00   1.07
6  2016-05-16 09:23:00   0.31
7  2016-05-16 15:56:00   1.02
8  2016-05-16 21:43:00   0.32
9  2016-05-17 04:11:00   1.06
10 2016-05-17 10:11:00   0.30
11 2016-05-17 16:41:00   1.02
12 2016-05-17 22:30:00   0.33
13 2016-05-18 04:46:00   1.05
14 2016-05-18 10:55:00   0.29
15 2016-05-18 17:20:00   1.04
16 2016-05-18 23:14:00   0.34
17 2016-05-19 05:19:00   1.06


You can import to R with:

data = structure(list(time = structure(c(1463294520, 1463315460, 1463339040, 1463359860, 1463383860, 1463404980, 1463428560, 1463449380, 1463472660, 1463494260, 1463517660, 1463538600, 1463561160, 1463583300, 1463606400, 1463627640, 1463649540), class = c("POSIXct", "POSIXt"), tzone = ""), height = c(1.09, 0.31, 1.02, 0.3, 1.07, 0.31, 1.02, 0.32, 1.06, 0.3, 1.02, 0.33, 1.05, 0.29, 1.04, 0.34, 1.06)), .Names = c("time", "height"), row.names = c(NA, -17L), class = "data.frame")

Thanks for any tips on how best to proceed.