每天给定间隔的最大斜率 [英] Maximum slope for a given interval each day

查看:98
本文介绍了每天给定间隔的最大斜率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组时间序列数据,其中地表温度是在三个不同位置连续多天每10分钟测量一次(实际上是2年的数据).我感兴趣的是计算每个站点每天60分钟间隔内的最大斜率(温度上升率).

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.

因此,从本质上讲,我希望每天使用60分钟的窗口,每天一次,每次10分钟,并计算每个窗口的斜率,然后确定最大斜率以及一天中的最大斜率.然后,我想将此功能应用于数据集中的每一天.日期/时间采用以下格式(%m/%d/%y%H:%M).

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).

我正在想象使用ddply和zoo包以及函数rollapply来执行类似此伪代码的操作

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)))

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

其中时间"是每天(每10分钟)内的时间,而天"仅是日期,因此该功能可应用于所有日期.显然,"slope"不是R函数,必须将其写出才能计算出实际的斜率.

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?

我在一个位置包含了一些示例数据(因此位置列已被删除) https ://gist.github.com/natemiller/42eaf45747f31a6ccf9a

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

感谢您的协助, 内特

此后,我一直使用下面的geektrader的Joshua Ulrich答案,并使用基本代数将值转换回每小时的ºC单位

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))

效果很好.

推荐答案

您可以使用xts时间序列包,这对于时间序列分析非常有用. 结合TTR软件包,您可以轻松获得所需的内容.

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

这篇关于每天给定间隔的最大斜率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆