1
votes

I have a set of time series data with ground surface temperatures measured every 10 minutes over multiple days (actually 2 years of data) from three different locations. What I am interested in calculating is the maximum slope (rate of temperature increase) for any 60 minute interval for each day for each site.

So essentially I would like to work through each day, 10 minutes at a time, with a 60 minute window and calculate the slope for each window, and then determine the maximum slope and when during the day it occurred. I would then like to apply this function to every day in the data set. The date/time is in the following format (%m/%d/%y %H:%M).

I am imagining something using ddply and the zoo package and function rollapply, to do something like this pseudocode

ddply(data, .(location, day), function(d) max(rollapply(slope(d$temp~d$time, data=d)))

Where "time" is the time within each day (every 10 min) and "day" is simply the date so the function can be applied across all dates. Obviously, "slope" is not an R function and would have to be written to calculate actual slopes.

Does anyone have more experience with zoo and rollapply or can think of another way to solve this problem?

I've included some sample data here from a single location (so the location column has been removed) https://gist.github.com/natemiller/42eaf45747f31a6ccf9a

Thanks for any assistance, Nate

EDIT: I have since used a combination of geektrader's Joshua Ulrich's answers from below, and used basic algebra to convert the values back to units of ÂșC per hour

    CperH<-dat$Temp-(dat$Temp/(1+dat$ROC))

Works well.

1
The slope can change across the hour window right (because you have 3 readings)? So do you want the average slope of each window and then determine when the maximum slope occurred during the day? - Simon O'Hanlon
Hi Simon. Yes I would like the average slope for each window and then to determine which window had the greatest slope during the day. It will be a maximum rate of change in 60 minutes, but it can be any consecutive 60 minutes within the same day. Hope this clarifies. - Nate Miller

1 Answers

3
votes

You can use xts timeseries package which is very good for timeseries analysis. Combined with TTR package, you can get what you want quite easily.

require(xts)
require(TTR)
dat <- read.csv("https://gist.github.com/natemiller/42eaf45747f31a6ccf9a/raw/916443cfb353d82e8af6cdebdd80b2e956317b24/sampleTempData.csv")

dat.xts <- .xts(x = dat$Temp, index = as.POSIXct(strptime(dat$Date, format = "%m/%d/%y %H:%M")))
names(dat.xts) <- "Temp"
head(dat.xts)
##                     Temp
## 2011-04-11 03:48:00  9.5
## 2011-04-11 03:58:00  9.5
## 2011-04-11 04:08:00  9.5
## 2011-04-11 04:18:00  9.5
## 2011-04-11 04:28:00  9.5
## 2011-04-11 04:38:00  9.5


dat.xts$ROC <- ROC(dat.xts, n = 6)
head(dat.xts, 10)
##                     Temp ROC
## 2011-04-11 03:48:00  9.5  NA
## 2011-04-11 03:58:00  9.5  NA
## 2011-04-11 04:08:00  9.5  NA
## 2011-04-11 04:18:00  9.5  NA
## 2011-04-11 04:28:00  9.5  NA
## 2011-04-11 04:38:00  9.5  NA
## 2011-04-11 04:48:00  9.5   0
## 2011-04-11 04:58:00  9.5   0
## 2011-04-11 05:08:00  9.5   0
## 2011-04-11 05:18:00  9.5   0

dat.xts[which.max(dat.xts$ROC), ]
##                     Temp       ROC
## 2011-04-12 09:48:00 14.5 0.5340825


# If you want to do analysis on per day basis.
dat.xts <- .xts(x = dat$Temp, index = as.POSIXct(strptime(dat$Date, format = "%m/%d/%y %H:%M")))
names(dat.xts) <- "Temp"
head(dat.xts)
##                     Temp
## 2011-04-11 03:48:00  9.5
## 2011-04-11 03:58:00  9.5
## 2011-04-11 04:08:00  9.5
## 2011-04-11 04:18:00  9.5
## 2011-04-11 04:28:00  9.5
## 2011-04-11 04:38:00  9.5


ll <- split.xts(dat.xts, f = "days")


ll <- lapply(ll, FUN = function(x) {
    x$ROC <- ROC(x, 6)
    return(x)
})

max.ll <- lapply(ll, function(x) x[which.max(x$ROC), ])

max.ll
## [[1]]
##                     Temp       ROC
## 2011-04-11 13:38:00 20.5 0.4946962
## 
## [[2]]
##                     Temp       ROC
## 2011-04-12 09:48:00 14.5 0.5340825
## 
## [[3]]
##                     Temp       ROC
## 2011-04-13 10:18:00 15.5 0.4382549
## 
## [[4]]
##                     Temp       ROC
## 2011-04-14 10:38:00 14.5 0.3715636