[英]Group 1's and 0's into two phrases, identify start and end, and count duration
我正在做一些周期性分析。
我有变量X,如果处于收缩状态,则为true,否则为false
X
##[1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
....
我改成0和1
X2<-as.ts(X*1)
然后我有一个日期序列。
td
## [1] "2000-01-31" "2000-02-29" "2000-03-31" "2000-04-30" "2000-05-31" "2000-06-30"
....
然后我用'zoo'以td顺序索引X2
。
library(zoo)
na_ts = zoo(x=X2, order.by=td)
现在是我的问题。 我想确定值更改时的日期,并计算该系列停留在1和0的时间。
如此理想的结果:
start end type duration
2000-01-31 - 2001-05-31 contraction 17 months
2001-06-30 - 2004-05-31 expansion ....
有人可以帮我吗? 提前谢谢了。
您可以使用X
的游程编码将时间序列分成具有相同值的连续元素:
# Reproducible example
X <- c(F, F, F, T, T, F)
td <- c( "2000-01-31", "2000-02-29", "2000-03-31", "2000-04-30", "2000-05-31", "2000-06-30")
library(zoo)
na_ts = zoo(x=X, order.by=td)
# Split with run-length encoding
runlens <- rle(X)
(ts.spl <- split(na_ts, rep(seq_along(runlens$lengths), times=runlens$lengths)))
# $`1`
# 2000-01-31 2000-02-29 2000-03-31
# FALSE FALSE FALSE
#
# $`2`
# 2000-04-30 2000-05-31
# TRUE TRUE
#
# $`3`
# 2000-06-30
# FALSE
现在,您可以从存储在ts.spl
列表中的每个时间序列中提取所需的任何信息。 例如:
dat <- data.frame(start = sapply(ts.spl, start),
end = sapply(ts.spl, end),
val = ifelse(runlens$values, "contraction", "expansion"))
dat$days <- as.numeric(as.Date(dat$end) - as.Date(dat$start), units="days")
dat
# start end val days
# 1 2000-01-31 2000-03-31 expansion 60
# 2 2000-04-30 2000-05-31 contraction 31
# 3 2000-06-30 2000-06-30 expansion 0
这种方法是“拆分应用合并”的一个示例,其中我们根据数据的某些属性拆分原始数据,应用函数提取有关每个片段的感兴趣信息,然后将其重新组合在一起。
这是我稍加修改后的代码。 谢谢josilber! 我们通常会在周期性分析中处理月度数据,因为约会长达数天并不准确。 同样,经济可能处于衰退/扩张中,因此不会为零。
na_ts = zoo(x=X, order.by=td)
# Split with run-length encoding
runlens <- rle(X)
(ts.spl <- split(na_ts, rep(seq_along(runlens$lengths), times=runlens$lengths)))
dat <- data.frame(start = sapply(ts.spl, start),
end = sapply(ts.spl, end),
val = ifelse(runlens$values, "contraction", "expansion"))
dat$months<- runlens$lengths
dat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.