1查找时间序列中的分段错误

时间:2016-01-12 07:17:09

标签: r breakpoints

我有以下数据:

Lines <- "D1,Value
1,20/11/2014 16:00,0.01
2,20/11/2014 17:00,0.01  
3,20/11/2014 19:00,0.01  
4,20/11/2014 22:00,0.20  
5,20/11/2014 23:00,0.03"

library(zoo)
library(strucchange)
ts1 <- read.csv(text = Lines, as.is = TRUE)
z <- read.zoo(ts1, tz = "", format = "%d/%m/%Y %H:%M")

我使用插值填充缺失值:

interp <- na.approx(z, xout = seq(start(z), end(z), "hours"))

我想找到段(最多2个段)并为每个段绘制相关的行:

interp1 <- as.ts(interp)

我收到以下错误:

bp.interp1<- breakpoints(interp1 ~ 2)
Error in terms.formula(formula, data = data) : 
  invalid model formula in ExtractVars

此外,日期已更改

1 个答案:

答案 0 :(得分:1)

strucchange包依赖于zoo,因此您不需要显式加载zoo。如果您使用read.table,则不需要read.zoo,也无需将时间序列转换为"ts"对象。您需要指定公式。见?breakpoints

library(strucchange)

# test input
Lines <- "D1,Value
1,20/11/2014 16:00,0.01
2,20/11/2014 17:00,0.01  
3,20/11/2014 19:00,0.01  
4,20/11/2014 22:00,0.20  
5,20/11/2014 23:00,0.03"
z <- read.zoo(text = Lines, tz = "", format = "%d/%m/%Y %H:%M", sep = ",")

bp <- breakpoints(z ~ 1, h = 2)

,并提供:

> bp

         Optimal 2-segment partition: 

Call:
breakpoints.formula(formula = z ~ 1, h = 2)

Breakpoints at observation number:
3 

Corresponding to breakdates:
0.6 

注意:要绘制:

plot(z)
abline(v = time(z)[bp$breakpoints])