我有一个包含多个产品的月度时间序列的数据集 .

每行具有相同的终点但起点不同(因为该产品的时间戳可能已经开始较晚)我需要估算中间缺失值,即实际起点和终点之间的值 .

估算需要分三步完成,即

  • 使用na.seadec获取系列长度超过24的时间序列

  • 使用na.kalman作为时间序列,长度在12到24之间

  • 使用na.ma表示长度小于12的时间序列

注意:时间序列的起点是沿着行的第一个非零值 .

从第一列到第一个非零值的所有值都需要保持为零 .

以下是使用apply函数和if / else条件的代码片段 .


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)


}
)))

问题是,当我执行上面的代码片段时,我收到以下警告:

input data has only na's

结果,插补过程失败,没有估算缺失值 .

您认为错误消息的原因以及我如何解决它的原因是什么?