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

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

问题描述

我有一组时间序列数据,其中包含来自三个不同位置的多天(实际上是 2 年的数据)每 10 分钟测量一次地表温度.我感兴趣的是计算每个站点每天任何 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.

所以基本上我想每天工作,每次 10 分钟,有 60 分钟的窗口并计算每个窗口的斜率,然后确定最大斜率以及一天中发生的时间.然后我想将此函数应用于数据集中的每一天.日期/时间采用以下格式 (%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 分钟)内的时间,天"只是日期,因此该函数可以应用于所有日期.显然,斜率"不是 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.

有没有人对zoo和rollapply有比较多的经验或者能想到其他的方法来解决这个问题?

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天全站免登陆