R 如何在 hydroTSM 和 xts 包下的季节性使用复杂功能?
R How to use a complex function at seasonal period under hydroTSM and xts packages?
我想计算我的参数值的季节性平均值(当 x > 0.002
时)。为此,我使用 xts::period.apply()
按季节分隔值。我在endpoints()
中使用"quarter"期间,但"quarter"期间将一年分为四个季节如下:
"January+February+March",
"April+May+June",
"July+August+Septembre",
"October+November+December"
例如:
library(xts)
library(PerformanceAnalytics)
data(edhec)
head(edhec)
edhec_4yr <- edhec["1997/2001"]
ep <- endpoints(edhec_4yr, "quarter")
# mean
period.apply(edhec_4yr, INDEX = ep,
function(x) apply(x,2, function(y) mean(y[y>0.002])))
但是为了我的学习,我希望我的季节划分如下:
"December+January+February",
"March+April+May",
"June+July+August",
"Septembre+October+November"
你能帮我看看如何更改 "quarter" 期间的订单月份吗?[=23=]
我可以使用 hydroTSM 包下的简单功能(mean
、max
、min
),功能如下:
dm2seasonal(edhec_4yr, FUN=mean, season="DJF")
其中:
DJF : December, January, February
MAM : March, April, May
JJA : June, July, August
SON : September, October, November
但我无法将复杂函数(带条件的均值)应用为以下函数:
dm2seasonal(edhec_4yr, season="DJF",
function(x) apply(x,2, function(y) mean(y[y>0.002])))
你能帮我如何改进这个功能,以便计算 DJF 的平均值(当 x > 0.02
时)吗?
xts::endpoints()
函数总是 returns "standard" 周期中的最后一次观察,从原点(午夜,1970-01-01)开始。所以它不能轻易地做你想做的事。
您可以通过在每 3 个月的最后一个月的最后一天找到观察值来计算您自己的期末点 window。这是使用每月数据执行此操作的一种方法:
# .indexmon() returns a zero-based month
ep <- which((.indexmon(edhec_4yr) + 1) %in% c(2, 5, 8, 11))
aggfn <- function(x, bound = 0.002, ...) {
apply(x,2, function(y) mean(y[y > bound], ...))
}
period.apply(edhec_4yr, ep, aggfn)
如果您有每日数据,则需要找到月经结束的每个月的最后一天。您可以使用 .indexmon()
找到每个季节结束的所有月份,然后构造一个 xts对象与原始日常数据对象中所有这些观察的位置。然后你可以使用apply.monthly()
和last()
来提取每个季节结束月份的最后一天的位置。结果对象包含您需要传递给 period.apply()
.
的端点
data(prices)
prices <- as.xts(prices) # 'prices' is zoo; convert to xts
season_months <- (.indexmon(prices)+1) %in% c(2, 5, 8, 11)
ep_months <- xts(which(season_months), index(prices)[season_months])
ep_seasons <- as.numeric(apply.monthly(ep_months, last))
period.apply(prices, ep_seasons, aggfn)
而且我应该指出,我正在考虑如何以更灵活的方式指定终点,我将确保包括一种指定季节的方法。
我想计算我的参数值的季节性平均值(当 x > 0.002
时)。为此,我使用 xts::period.apply()
按季节分隔值。我在endpoints()
中使用"quarter"期间,但"quarter"期间将一年分为四个季节如下:
"January+February+March",
"April+May+June",
"July+August+Septembre",
"October+November+December"
例如:
library(xts)
library(PerformanceAnalytics)
data(edhec)
head(edhec)
edhec_4yr <- edhec["1997/2001"]
ep <- endpoints(edhec_4yr, "quarter")
# mean
period.apply(edhec_4yr, INDEX = ep,
function(x) apply(x,2, function(y) mean(y[y>0.002])))
但是为了我的学习,我希望我的季节划分如下:
"December+January+February",
"March+April+May",
"June+July+August",
"Septembre+October+November"
你能帮我看看如何更改 "quarter" 期间的订单月份吗?[=23=]
我可以使用 hydroTSM 包下的简单功能(mean
、max
、min
),功能如下:
dm2seasonal(edhec_4yr, FUN=mean, season="DJF")
其中:
DJF : December, January, February
MAM : March, April, May
JJA : June, July, August
SON : September, October, November
但我无法将复杂函数(带条件的均值)应用为以下函数:
dm2seasonal(edhec_4yr, season="DJF",
function(x) apply(x,2, function(y) mean(y[y>0.002])))
你能帮我如何改进这个功能,以便计算 DJF 的平均值(当 x > 0.02
时)吗?
xts::endpoints()
函数总是 returns "standard" 周期中的最后一次观察,从原点(午夜,1970-01-01)开始。所以它不能轻易地做你想做的事。
您可以通过在每 3 个月的最后一个月的最后一天找到观察值来计算您自己的期末点 window。这是使用每月数据执行此操作的一种方法:
# .indexmon() returns a zero-based month
ep <- which((.indexmon(edhec_4yr) + 1) %in% c(2, 5, 8, 11))
aggfn <- function(x, bound = 0.002, ...) {
apply(x,2, function(y) mean(y[y > bound], ...))
}
period.apply(edhec_4yr, ep, aggfn)
如果您有每日数据,则需要找到月经结束的每个月的最后一天。您可以使用 .indexmon()
找到每个季节结束的所有月份,然后构造一个 xts对象与原始日常数据对象中所有这些观察的位置。然后你可以使用apply.monthly()
和last()
来提取每个季节结束月份的最后一天的位置。结果对象包含您需要传递给 period.apply()
.
data(prices)
prices <- as.xts(prices) # 'prices' is zoo; convert to xts
season_months <- (.indexmon(prices)+1) %in% c(2, 5, 8, 11)
ep_months <- xts(which(season_months), index(prices)[season_months])
ep_seasons <- as.numeric(apply.monthly(ep_months, last))
period.apply(prices, ep_seasons, aggfn)
而且我应该指出,我正在考虑如何以更灵活的方式指定终点,我将确保包括一种指定季节的方法。