amices / mice

Multivariate Imputation by Chained Equations
https://amices.org/mice/
GNU General Public License v2.0
424 stars 106 forks source link

ampute() now preserves the data structure #593

Closed gerkovink closed 9 months ago

gerkovink commented 9 months ago

Fixes #592

library(mice)
boys |> 
  str()
#> 'data.frame':    748 obs. of  9 variables:
#>  $ age: num  0.035 0.038 0.057 0.06 0.062 0.068 0.068 0.071 0.071 0.073 ...
#>  $ hgt: num  50.1 53.5 50 54.5 57.5 55.5 52.5 53 55.1 54.5 ...
#>  $ wgt: num  3.65 3.37 3.14 4.27 5.03 ...
#>  $ bmi: num  14.5 11.8 12.6 14.4 15.2 ...
#>  $ hc : num  33.7 35 35.2 36.7 37.3 37 34.9 35.8 36.8 38 ...
#>  $ gen: Ord.factor w/ 5 levels "G1"<"G2"<"G3"<..: NA NA NA NA NA NA NA NA NA NA ...
#>  $ phb: Ord.factor w/ 6 levels "P1"<"P2"<"P3"<..: NA NA NA NA NA NA NA NA NA NA ...
#>  $ tv : int  NA NA NA NA NA NA NA NA NA NA ...
#>  $ reg: Factor w/ 5 levels "north","east",..: 4 4 4 4 4 4 4 3 3 2 ...

boys |>
  na.omit() |>
  ampute() |>
  (\(x) x$amp)() |>
  str()
#> Warning: Data is made numeric internally, because the calculation of weights
#> requires numeric data
#> 'data.frame':    223 obs. of  9 variables:
#>  $ age: num  8.86 8.87 8.91 9 9 ...
#>  $ hgt: num  125 145 138 136 151 ...
#>  $ wgt: num  31 38.2 30 26.9 48.2 29.4 28 22 35.6 32 ...
#>  $ bmi: num  19.9 18.2 15.8 14.5 21.1 ...
#>  $ hc : num  51.6 54.8 54.7 53.4 50.5 53.9 52.1 54.4 53 52.3 ...
#>  $ gen: Ord.factor w/ 5 levels "G1"<"G2"<"G3"<..: 1 2 1 1 2 1 1 1 2 2 ...
#>  $ phb: Ord.factor w/ 6 levels "P1"<"P2"<"P3"<..: 1 1 1 1 1 1 1 1 1 1 ...
#>  $ tv : int  1 2 2 4 2 2 2 3 2 1 ...
#>  $ reg: Factor w/ 5 levels "north","east",..: 1 4 4 2 3 2 1 3 4 3 ...
#>  - attr(*, "na.action")= 'omit' Named int [1:525] 1 2 3 4 5 6 7 8 9 10 ...
#>   ..- attr(*, "names")= chr [1:525] "3" "4" "18" "23" ...

Created on 2023-10-05 with reprex v2.0.2