1Finding Segmentation in Time Series错误

1Finding Segmentation in Time Series error

我有以下数据:

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

此外,日期也发生了变化

strucchange 包依赖于 zoo,因此您不需要显式加载 zoo。如果您使用 read.zoo,则不需要 read.table,并且没有必要将时间序列转换为 "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])