R - estimate of missing values

Suppose I have a table as such:

Date        Sales
09/01/2017  9000
09/02/2017  12000
09/03/2017  0
09/04/2017  11000
09/05/2017  14400
09/06/2017  0
09/07/2017  0
09/08/2017  21000
09/09/2017  15000
09/10/2017  23100
09/11/2017  0
09/12/2017  32000
09/13/2017  8000

Here is the data in the table

The values ​​in the table are evaluated by the R program, to which I do not have access (now this is a black box). Now there are several days with 0 values ​​that tend to creep due to problems in our receive / ETL process. I need to evaluate the values ​​for dates with 0 data.

Our approach is as follows:

  • Draw a line from the date preceding the missing data to the date after the missing data
  • Estimate the value of a missing date from a string

, , . , , .

The intersection of the green and red lines will give the desired values

R? n00b R, , .

+4
2

approxfun.

## Your data
df = read.table(text="Date        Sales
09/01/2017  9000
09/02/2017  12000
09/03/2017  0
09/04/2017  11000
09/05/2017  14400
09/06/2017  0
09/07/2017  0
09/08/2017  21000
09/09/2017  15000
09/10/2017  23100
09/11/2017  0
09/12/2017  32000
09/13/2017  8000",
header=TRUE, stringsAsFactors=FALSE)
df$Date = as.Date(df$Date, format="%m/%d/%Y")


## Create function for linear interpolation
Interp = approxfun(df[df$Sales > 0, ])

## Use function to fill in interpolated values
Vals = Interp(df$Date[df$Sales == 0])
df$Sales[df$Sales == 0] = Vals
plot(df, type="l")
grid()

Interpolated Values

+6

na.interpolation imputeTS. na.interpolation - , , .

library(dplyr)
library(imputeTS)

dt2 <- dt %>%
  replace(. == 0, NA) %>%
  mutate(Sales = na.interpolation(Sales))

dt2
         Date Sales
1  09/01/2017  9000
2  09/02/2017 12000
3  09/03/2017 11500
4  09/04/2017 11000
5  09/05/2017 14400
6  09/06/2017 16600
7  09/07/2017 18800
8  09/08/2017 21000
9  09/09/2017 15000
10 09/10/2017 23100
11 09/11/2017 27550
12 09/12/2017 32000
13 09/13/2017  8000

dt <- read.table(text = "Date        Sales
09/01/2017  9000
                 09/02/2017  12000
                 09/03/2017  0
                 09/04/2017  11000
                 09/05/2017  14400
                 09/06/2017  0
                 09/07/2017  0
                 09/08/2017  21000
                 09/09/2017  15000
                 09/10/2017  23100
                 09/11/2017  0
                 09/12/2017  32000
                 09/13/2017  8000",
                 header = TRUE, stringsAsFactors = FALSE)
+4

Source: https://habr.com/ru/post/1685568/


All Articles