Skip to content

Determining maximum hourly slope per day

3 messages · Nathan Miller, Peter Ehlers

#
On 2013-03-12 17:10, Nathan Miller wrote:
First, let's ignore location; if you can do it for one location,
you can surely do it for others.

Second, let's ignore date; if you can do it for one date, you
can surely do it for others.

That leaves us with the question of what you want to do for one
given date. If you want the maximum slope for any 60-minute interval
on that date (which I take your question to mean), then rollapply
should do the job. But I'm not very familiar with zoo, so here's a
crude approach:

   d <- data.frame(time = 1:72, temp = rnorm(72))
   slope <- rep(NA, 72)
   for(i in 6:72) {
      slope[i] <- coef(lm(temp ~ time, data = d, subset = (i-5):i))[2]
   }
   maxslope <- max(slope, na.rm = TRUE)
   idx <- which.max(slope)

Obviously, this can be extended to cover more than a 24-hour period.

Now, let's wait for Gabor to show us the trivial way with zoo::rollapply.

Peter Ehlers