Search code examples
rtime-seriesspline

Use Spline interpolation to deal with missing time series data


I have a question about dealing with missing data in time series. I got the data for day1-day7, and day14 and day30 below. I want to predict the data for day 60, 90 and 180. But the time interval varies, so I want to generate the data points for the missing data.

day ltv
1   8.94
2   18.93
3   26.19
4   31.97
5   38.19
6   45.59
7   52.06
8   NA
9   NA
10  NA
11  NA
12  NA
13  NA
14  69.10
15  NA
16  NA
17  NA
18  NA
19  NA
20  NA
21  NA
22  NA
23  NA
24  NA
25  NA
26  NA
27  NA
28  NA
29  NA
30  103

Is there anyway to use spline() function to do that? Thank you!


Solution

  • We can use na.spline from zoo

    library(zoo)
    df1$ltv <- na.spline(df1$ltv)
    df1$ltv
    #[1]   8.94000  18.93000  26.19000  31.97000  38.19000  45.59000  52.06000  57.25141  61.32302  64.39529
    #[11]  66.58868  68.02362  68.82058  69.10000  68.98271  68.59102  68.04762  67.47520  66.99644  66.73402
    #[21]  66.81064  67.34898  68.47173  70.30157  72.96119  76.57328  81.26051  87.14559  94.35119 103.00000
    

    data

    df1 <- structure(list(day = 1:30, ltv = c(8.94, 18.93, 26.19, 31.97, 
    38.19, 45.59, 52.06, NA, NA, NA, NA, NA, NA, 69.1, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 103)),
     class = "data.frame", row.names = c(NA, 
    -30L))