2

I'm filling NA with missForest()package in R. In my subset where there is no POSIXct date format, it's working well but when I try in my sample where I have POSIXct format, an error occurred Error in sample.int(length(x), size, replace, prob) : invalid first argument... but both samples are quite similar, just a POSIXct with NA inside for the second.

I make a test just adding a column to the first subsample with today's date and adding 3 NA in it. The package is not working.

Do you have any idea to fix it, or any other package may exist taking into account date format ?

You could take an example as following to see what I mean:

df <- data.frame(
  Date = Sys.Date(),
  LOT = rnorm(5), 
  S12_A = c(7,7,7,7,7),  
  S123_AA = c(1,1,NA,1,1), 
  S135_AA = rnorm(5), 
  S1763_BB = rnorm(5), 
  S173_BB = rnorm(5)
)

df$Date[4] <- NA

library(missForest)

missForest(df)
Alex Germain
  • 411
  • 4
  • 16
  • 3
    Please add a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example), what you've tried and what is the expected result – Roman Luštrik Feb 24 '19 at 09:42

1 Answers1

3

I get an error Error in sample.int(length(x), size, replace, prob) : invalid first argument. If I exclude the first column the imputation goes through with a warning. Are you sure you want to impute dates?

What you could do is coerce dates into a numeric variable, impute and convert back.

out <- missForest(df[, -1])
out$ximp$Date_numeric <- as.Date(out$ximp$Date_numeric, origin = "1970-01-01")
out$ximp

         LOT S12_A S123_AA     S135_AA  S1763_BB      S173_BB Date_numeric
1  0.9056106     7       1 -0.90207400 0.8070748 -1.080159330   2019-03-14
2  0.4843268     7       1  0.54376134 0.1376736  0.453839813   2019-03-14
3 -0.5628681     7       1  0.05709977 0.6064287  0.827101136   2019-03-14
4 -0.1781705     7       1  0.18665039 1.5048530 -0.009170842   2019-03-14
5  0.8294332     7       1  0.40884799 1.6707076 -0.350740495   2019-03-14
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197