0

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.

  • using na_seadec for time series with series length more than 24
  • using na_kalman for time series with the length between 12 and 24
  • using na_ma for time series with length less than 12

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?

Steffen Moritz
  • 7,277
  • 11
  • 36
  • 55
avij
  • 11
  • 4
  • 3
    please take are of the formatting. It is not understandable at all. – Ahmadov Jun 20 '17 at 19:22
  • Adding extraneous white lines does NOT improve readability of either natural language ..... or of code. And do read [MCVE] and perhaps check spelling in your title. – IRTFM Jun 20 '17 at 22:28
  • thanks for the edit @Ahmedov . Problem here is that the code returns the same dataset without any imputations with warning messages would love to have your views and suggestions on it – avij Jun 21 '17 at 05:08
  • 1
    Found the solution by doing it column wise and some internal edits, – avij Jun 21 '17 at 06:05

0 Answers0