Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. library(mice)
  2. library(missForest)
  3.  
  4. set.seed(123)
  5.  
  6. # Create a dataset with missing values on 2 columns
  7. iris.mis = prodNA(iris[, c('Sepal.Length', 'Petal.Length')], 0.20)
  8. iris.mis = cbind(iris.mis, iris[, c('Sepal.Width', 'Petal.Width', 'Species')])
  9.  
  10. # Setup MICE
  11. init = mice(iris.mis, maxit = 0)
  12. iris.metod = init$method
  13. iris.method['Sepal.Length'] = "" # Do not impute Sepal.Length
  14.  
  15. # Run MICE
  16. imp = mice(iris.mis, m = 5, maxit = 5, method = iris.method)
  17.  
  18. # Inspect Result
  19. res = mice::complete(imp, 1)
  20. print(length(res$Petal.Length[is.na(res$Petal.Length)]))
  21.  
  22. # [1] 8
  23. # i.e. still 8 missing values in Petal.Length, when we wanted 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement