avij
avij

Reputation: 11

Missing value imputation in time series using ImputeTS in R

I have a dataset that contains monthly time series of multiple products.

Each row has the same end point but different starting points(as the time stamp for that product might have started late) I need to impute intermediate missing values, i.e. values between the actual start and end points.

Imputation needs to be done in 3 steps i.e.

NOTE: The starting point of time series is the first non-zero value along the row.

All the values from the first column to the first non-zero value needs to be kept as zero.

Following is the code snippet which utilizes the apply function together with if/else conditions.


library("imputeTS")
temp2<- as.data.frame(t(apply(temp,1,function(x) #**temp is the datset of   multiple** time series
  {

  ind<-min(which(x!=0)) #**first non zero/ starting point**

  series<-(length(x)-ind+1) # **total length after removing front zeroes**


  if(ind==Inf)return(x)

  x[x==0]<-NA

  timeseries=ts((x[ind:length(x)]),frequency = 12,end = c(2017,3)) #**converting it to ts format with same ending point**

  if(series>24) #**if,else for different imputations based on series length**

  { y[1:ind]<-0

     y[ind:length(x)]<-t(na_seadec(t(timeseries),algorithm = "ma"))

}

    else if(series >12 && series <25)

  {    y[1:ind]<-0

  y[ind:length(x)]<-t(na_kalman(t(timeseries),model="StructTS"))

    }

  else

   { y[1:ind]<-0

  y[ind:length(x)]<-t(na_ma(t(timeseries),k=1,weighting = "simple"))

   }

    return(y)


}
)))

The problem is that when I execute the above code snippet I get the following warnings:

input data has only na's

As a result, the imputation process fails with no missing values imputed.

What do you think is the reason for the error message and how I can fix it?

Upvotes: 0

Views: 1105

Answers (0)

Related Questions