SteffenMoritz/imputeTS

Feature: Allow bounded time series interpolation

ashirwad opened this issue · 1 comments

Hello,

Thanks for this great package! I am currently using the na_kalman() function to impute missing glucose readings for diabetes patients. Since there is no option to fix the upper and lower bounds for the imputed values, the imputed data sometimes takes values (e.g., <0) that are physiologically not possible. At the moment, I am replacing such values with reasonable numbers once I am done with the interpolation, but I think it will be useful to add arguments to na_kalman and friends to provide bounds for the imputed values.

Thanks!

Hello Ashirwad,
sounds like a really interesting dataset, that you have.

Might be a good suggestion! How do you do the 'replacing such values with reasonable numbers'?

In general there are multiple approaches for setting the limits / constrain the imputations to an interval. Here are the two most common ones:

METHOD 1:
One approach is transforming the data using a scaled logit transform.
(see also https://robjhyndman.com/hyndsight/forecasting-within-limits/)
This works also for imputations.

Would be implemented like this:
(your dataset is in your_data)


# Bounds
# min and max have to be lower/higher than the existing min/max in the series
min <- 200
max <- 700


# Transform data
y <- log((your_data-min)/(max-your_data))

# Perform imputation on transformed data
imp <- na_kalman(y)

# Back-transform the now imputed dataset
back <- (max-min)*exp(imp)/(1+exp(imp)) + min

# Result is in back
back

Downside is, it changes the whole distribution of imputed values.
Meaning also the imputations that were within the limits before will now be slightly different.
(but as you see in the next method this is also the upside of this method)

METHOD 2:

You could just leave the imputations that are within the limit untouched and set the imputations violating the limits to the limit.


# Bounds
min <- 200
max <- 700

# Perform imputation
imp <- na_kalman(your_data)

# Replace out of bound values with the min/max
imp[ imp > max] <- max
imp[ imp < min] <- min

# Result is in imp
imp

Downside here is you will possibly have a quite a bunch of values being exactly the limits. Which might not be the distribution you want to have. Upside is, the other imputations are not affected.

Completely depends on your data, what makes most sense. That is also why I am a little bit hesitant with adding the limit, because there is no one fits it all solution.

But what I definitely want to add (once if find the time) is more documentation, that you can maybe read about these things in the documentation.

Think it would be really beneficial to have some advices about choosing the right algorithm and special cases like this one.