4

I am looking for a function that fills my NA values in a data.table column with a "trend" of the previous values. I know that there is the function: na.locf() But this function only uses the last available value and forwards it into the future. What I want is that the specific function looks a the "trend", so at the evolution of more than just the last available values und writes this trend into the future.

data
year,V1
2020,1
2021,1.5
2023,2
2024,2.5
2025,3
2026,NA
2027,NA
2028,NA
2029,NA
2030,NA


expected output:
data
year,V1
2020,1
2021,1.5
2023,2
2024,2.5
2025,3
2026,3.5
2027,4
2028,4.5
2029,5
2030,5.5

Related but not a duplicate of these posts:

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • Do you always want a linear interpolation? What happens when the trend is _not_ linear? You also might want to include a larger data set which includes multiple gaps. – Tim Biegeleisen Nov 26 '20 at 08:51
  • Not a duplicate of linked question, @zx8754 please read both – jangorecki Nov 26 '20 at 11:34
  • @jangorecki re-opened. – zx8754 Nov 26 '20 at 12:30
  • 1
    No, I do not necessarily need to have a linear interpolation. What I am looking for are multiple ways also in a mathematical sense how to decrease a value based on previous evolution. – Sebastian.DataAnalyst Nov 26 '20 at 12:43
  • 1
    "multiple ways" - the question is becoming too broad to be answerable, be more specific, what is the expected output for your example data. From existing example data, the trend is only positive, increasing at 0.5 per row. – zx8754 Nov 26 '20 at 13:06
  • What is needed is to define how far in the past you want to look to project interpolation. For example na.locf looks only for single value in the past. – jangorecki Nov 26 '20 at 14:18
  • You may want to look into topic of time series data forecasting because you are precisely asking for that AFAIU. – jangorecki Nov 26 '20 at 14:19
  • It would be great if I do have a function that would look 3 time steps in the past and follow this trend for future values. – Sebastian.DataAnalyst Nov 26 '20 at 14:25
  • Sounds like a linear regression over previous 3 values. I would try something like `some_lm_fun(shift(V1, 1:3))`. – jangorecki Nov 26 '20 at 17:01

0 Answers0