R mice doesn't give a 'valid' sollution EDITED: See below for additional information..

TL;DR: How can I add missing data in a dataset like the sample in a way that it doesn't deviate much from the original dataset.

ORIGINAL:

I have a question about the mice package.

I am looking at a dataset like the example code:

set.seed(1337)
df &lt;- data.frame(x = c(seq(100, 10, -4), seq(100, 20, -3), seq(100, 60, -3), 
seq(80, 40, -3), seq(100, 10, -3))) %&gt;% data.table()
df[sample(seq_len(nrow(df)), size = nrow(df) * 0.3), x := NA]
df$y &lt;- seq(1,nrow(df),1)

Tmp &lt;- mice(df, m = 1, maxit = 30, seed = 1337, print = FALSE)
Completed &lt;- mice::complete(Tmp, 1)

plot(df$y, df$x, col = "blue")
points(Completed[is.na(df$x), ]$y, Completed[is.na(df$x), ]$x, col = "red")
legend(70, 30, legend = c("actual", "pred"), col = c("blue", "red"), lty = 1, cex = 0.5)


I am trying to fit the missing data points, but as you can see in the image below, the fit (red) isn't like I want it to be, because I would expect it to appear in between the blue line. Would anyone know how to 'fix' this?

For clearance: The data on the x-axis is interpreted as a datetime object while the value on the right is the current battery level (in percentage) of ONE device. The points where the blue dotted line stops and starts at a higher number is when the battery is charged.



UPDATE:
I have looked at the DMwR-package and especially at knnImputation. I was able to fit it to the sample set without any issue, see image below.


However it gave an issue when I tried to implement it with the real data set, because it threw an error:

Error in scale.default(xcomplete, dm[i, ], FALSE) : length of 'center' must equal the number of columns of 'x'

See sessionInfo below: 

&gt; sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-redhat-linux-gnu (64-bit)
Running under: CentOS release 6.9 (Final)

attached base packages:
[1] grid      stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] lubridate_1.3.3     zoo_1.7-12          tidyr_0.6.0         pROC_1.13.0         randomForest_4.6-14 DMwR_0.4.1          lattice_0.20-35     stringr_1.0.0      
 [9] openxlsx_3.0.0      tictoc_1.0          data.table_1.10.4-3 wlhive_1.1.9003     ggplot2_1.0.1       dplyr_0.5.0
After a lucky google search, I found the function na.interpolation from the imputeTS-package. After changing the dataset to a time series (XTS), I was able to impute the right data.

library(xts)
library(imputeTS)

set.seed(1337)
df &lt;- data.frame(x = c(seq(100, 10, -4), seq(100, 20, -3), seq(100, 60, -3), 
                       seq(80, 40, -3), seq(100, 10, -3))) %&gt;% data.table()
df[sample(seq_len(nrow(df)), size = nrow(df) * 0.3), x := NA]
df$y &lt;- seq(1,nrow(df),1)

ts &lt;- xts(df[, -c("y")], as.Date(df$y))
head(ts)
#&gt;              x
#&gt; 1970-01-02 100
#&gt; 1970-01-03  96
#&gt; 1970-01-04  NA
#&gt; 1970-01-05  88
#&gt; 1970-01-06  NA
#&gt; 1970-01-07  80

full &lt;- na.interpolation(ts)
plot(full, type = "p")