Failed to convert index to appropriate type when trying to map a weekly ts object to dygraphs

I am trying to create a Holt-Winters forecast from a weekly time series, and then build the original series and predict using digraphs. I have 144 weeks of Monday. For my purpose, I ignore the fact that several years have 53 weeks. The data structure can be modeled as follows:

## create data similar to what I have

week_date <- seq.Date(from = as.Date("2012/05/11"),
                      by = "week",
                      length.out = 144)
set.seed(1)
var1 <- diffinv(rnorm(143))
df <- data.frame(cbind(week_date, var1))

## convert to ts object then
## create Holt Winters forecast

dfts <- ts(df[,2],freq=52, start=c(2012,19))

hw <- HoltWinters(dfts)
p <- predict(hw, 4)
all <- cbind(dfts, p)

## create plots

dygraph(all, "time series dygraph") %>%
      dySeries("var1", label = "Actual") %>%
      dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted")

This results in the following error:

Error in as.xts.ts(data) : could not convert index to appropriate type

I tried the suggested solution here , but getting the same error:

> all <- cbind(dfts = as.xts(dfts), p = as.xts(p))
Error in as.xts.ts(dfts) : could not convert index to appropriate type
+4
source share
1 answer

. , data dygraph " ( xts , xts)" (. ?dygraph).

, dfts xts :

> library(xts)
> dfts <- as.xts(dfts)
Error in as.xts.ts(dfts) : could not convert index to appropriate type 

xts :

> dfts <- xts(dfts)
Error in xts(dfts) : order.by requires an appropriate time-based object

, xts index(x) order.by. ?xts:

order.by    a corresponding vector of unique times/dates - 
            must be of a known time-based class
...
Currently acceptable classes include: ‘Date’, ‘POSIXct’, ‘timeDate’, 
as well as ‘yearmon’ and ‘yearqtr’ where the index values remain unique.

dfts:

> str(index(dfts))
 num [1:148] 2012 2012 2012 2012 2012 ...

> head(index(dfts))
[1] 2012.346 2012.365 2012.385 2012.404 2012.423 2012.442

, xts , .

-, all, zoo :

> library(zoo)
> # You'll need prediction.interval=TRUE to get the bounds:
> p <- predict(hw, 4, prediction.interval=TRUE)
> all <- merge(actual=as.zoo(dfts), predicted=as.zoo(p))
> head(all)
             actual fit upr lwr
2012(19)  0.0000000  NA  NA  NA
2012(20) -0.6264538  NA  NA  NA
2012(21) -0.4428105  NA  NA  NA
2012(22) -1.2784391  NA  NA  NA
2012(23)  0.3168417  NA  NA  NA
2012(24)  0.6463495  NA  NA  NA

xts . , date_decimal lubridate:

> library(lubridate)
> all.xts <- xts(all, date_decimal(index(all)))

, dygraph:

> dygraph(all.xts, "time series dygraph") %>%
      dySeries("actual", label = "Actual") %>%
      dySeries(c("lwr", "fit", "upr"), label = "Predicted")

dygraph output

+7

All Articles