0

I am performing Wavelet Analysis using biwavelet package in R. The date variable does not have continuous dates but with gaps. When I try to create the graph, I get the following error.

Error in check.datum(d) : The step size must be constant (see approx function to interpolate)

An MWE is given below:

   library(foreign)
   library(biwavelet)
   library(xts)
   library(labelled)
   library(zoo)

   date =c("2020-02-13", "2020-02-14", "2020-02-17", "2020-02-18", "2020-02-19", "2020-02-20", "2020-02-21", "2020-02-24", "2020-02-25", "2020-02-26", "2020-02-27", "2020-02-28", "2020-03-02", "2020-03-03", "2020-03-04", "2020-03-05", "2020-03-06", "2020-03-09", "2020-03-10", "2020-03-11", "2020-03-12", "2020-03-13")
   rdate = as.Date(date)
   date <- as.Date(date, format = "%Y-%m-%d")
   date
   class(date)
   var = c(-0.077423148, -0.083293147, -0.089214072, -0.095185943, -0.101208754, -0.107282504, -0.113407195, -0.119582824, -0.125809386, -0.125806898, -0.132149309, -0.138584509,  -0.145112529, -0.151733354, -0.158446968, -0.165253401, -0.172152638, -0.179144681, -0.186229542, -0.193407193, -0.200677648, -0.208040923)
   data = data.frame(date, var)
   View(data)
   X <- as.xts(data[,-1], order.by = date)
   ABC <- data.frame(date, var)
   wt.t1=plot(wt(ABC), form = "%b-%d")

How can I resolve this issue?

Ahmed Arif
  • 189
  • 1
  • 2
  • 10

1 Answers1

0

You can interpolate missing days by following the instructions in the error message:

alldates <- seq(min(date), max(date), by = 1)
interpdata <- approx(date, var, xout = alldates)
ABC <- data.frame(date = alldates, var = interpdata$y)
wt.t1 <- plot(wt(ABC, form = "%b-%d")

However, I think the reason you are missing some days is that they are Saturday or Sunday; I only see weekdays in the dataset.

For many datasets (e.g. stock market trading, etc.) it doesn't make sense to interpolate "what would the price have been on Saturday?", because trades never occur on Saturday or Sunday. In that case, I'd suggest replacing the "date" variable with a simple increment, e.g.

date <- 1:length(date)
ABC <- data.frame(date, var)
wt.t1=plot(wt(ABC), form = "%b-%d")
user2554330
  • 37,248
  • 4
  • 43
  • 90
  • You are right missing days are Saturday and Sunday as trading does not take place on these days. I do not interpolate. However, replacing the date variable with the increment does not show the date in the date format. Rather, I get the number of days on the x-axis like 50, 100, 150 etc. – Ahmed Arif Jul 01 '21 at 08:37