Skip to contents

Impute missing values in baseline covariates. For numeric columns, the mean value of the observed values is imputed. For factor columns, the mode value of the observed values is imputed.

Usage

impute_covariates_mean_mode(
  data,
  baseline_covariates,
  binary_categorical = TRUE
)

Arguments

data

a data.frame to impute

baseline_covariates

A character vector containing the names in data to impute.

binary_categorical

A logical scalar indicating whether binary variables (logicals, numeric columns containing only 0/1/NA) should be imputed as a categorical variable (using the mode) or a continuous variable (using the mean). The latter preserves the type of the column in output.

Value

A data.frame after imputing the columns named in baseline_covariates

Examples

set.seed(12345)
mtcars_missing <- mtcars
mtcars_missing$cyl <- factor(mtcars_missing$cyl)
mtcars_missing$vs <- factor(mtcars_missing$vs)
mtcars_missing$am <- factor(mtcars_missing$am)
mtcars_missing$gear <- factor(mtcars_missing$gear)
for(i in 1:ncol(mtcars_missing))
  mtcars_missing[sample(x = 1:nrow(mtcars_missing), size = 3), i] <- NA

impute_covariates_mean_mode(
  data = mtcars_missing,
  baseline_covariates = names(mtcars_missing)
)
#>                          mpg cyl     disp       hp     drat       wt     qsec
#> Mazda RX4           21.00000   6 160.0000 110.0000 3.900000 2.620000 16.46000
#> Mazda RX4 Wag       21.00000   6 160.0000 145.8621 3.900000 2.875000 17.02000
#> Datsun 710          22.80000   4 108.0000  93.0000 3.850000 2.320000 18.61000
#> Hornet 4 Drive      21.40000   6 258.0000 110.0000 3.080000 3.215000 19.44000
#> Hornet Sportabout   18.70000   8 360.0000 175.0000 3.150000 3.440000 17.02000
#> Valiant             18.10000   6 225.0000 105.0000 3.643103 3.460000 20.22000
#> Duster 360          14.30000   8 360.0000 245.0000 3.210000 3.212828 17.85862
#> Merc 240D           24.40000   4 146.7000  62.0000 3.690000 3.190000 17.85862
#> Merc 230            22.80000   4 140.8000  95.0000 3.920000 3.150000 22.90000
#> Merc 280            19.20000   6 167.6000 123.0000 3.920000 3.212828 18.30000
#> Merc 280C           17.80000   6 233.9828 123.0000 3.643103 3.440000 18.90000
#> Merc 450SE          16.40000   8 275.8000 180.0000 3.070000 4.070000 17.40000
#> Merc 450SL          17.30000   8 275.8000 180.0000 3.070000 3.730000 17.60000
#> Merc 450SLC         20.23793   8 275.8000 180.0000 3.070000 3.780000 18.00000
#> Cadillac Fleetwood  10.40000   8 472.0000 205.0000 2.930000 5.250000 17.98000
#> Lincoln Continental 20.23793   8 460.0000 215.0000 3.000000 5.424000 17.82000
#> Chrysler Imperial   14.70000   8 440.0000 230.0000 3.230000 5.345000 17.85862
#> Fiat 128            32.40000   4  78.7000  66.0000 4.080000 2.200000 19.47000
#> Honda Civic         20.23793   4  75.7000  52.0000 4.930000 1.615000 18.52000
#> Toyota Corolla      33.90000   4  71.1000  65.0000 4.220000 1.835000 19.90000
#> Toyota Corona       21.50000   4 120.1000  97.0000 3.700000 2.465000 20.01000
#> Dodge Challenger    15.50000   8 318.0000 150.0000 3.643103 3.520000 16.87000
#> AMC Javelin         15.20000   8 304.0000 150.0000 3.150000 3.435000 17.30000
#> Camaro Z28          13.30000   8 350.0000 145.8621 3.730000 3.840000 15.41000
#> Pontiac Firebird    19.20000   8 400.0000 175.0000 3.080000 3.845000 17.05000
#> Fiat X1-9           27.30000   8 233.9828  66.0000 4.080000 1.935000 18.90000
#> Porsche 914-2       26.00000   4 120.3000  91.0000 4.430000 2.140000 16.70000
#> Lotus Europa        30.40000   8  95.1000 113.0000 3.770000 1.513000 16.90000
#> Ford Pantera L      15.80000   8 233.9828 264.0000 4.220000 3.170000 14.50000
#> Ferrari Dino        19.70000   6 145.0000 175.0000 3.620000 3.212828 15.50000
#> Maserati Bora       15.00000   8 301.0000 335.0000 3.540000 3.570000 14.60000
#> Volvo 142E          21.40000   4 121.0000 145.8621 4.110000 2.780000 18.60000
#>                     vs am gear     carb
#> Mazda RX4            0  1    4 4.000000
#> Mazda RX4 Wag        0  1    4 4.000000
#> Datsun 710           1  1    3 1.000000
#> Hornet 4 Drive       1  0    3 1.000000
#> Hornet Sportabout    0  0    3 2.000000
#> Valiant              0  0    3 1.000000
#> Duster 360           0  0    3 4.000000
#> Merc 240D            1  0    4 2.000000
#> Merc 230             1  0    4 2.827586
#> Merc 280             1  0    4 4.000000
#> Merc 280C            1  0    4 4.000000
#> Merc 450SE           0  0    3 3.000000
#> Merc 450SL           0  0    3 2.827586
#> Merc 450SLC          0  0    3 2.827586
#> Cadillac Fleetwood   0  0    3 4.000000
#> Lincoln Continental  0  0    3 4.000000
#> Chrysler Imperial    0  0    3 4.000000
#> Fiat 128             1  1    4 1.000000
#> Honda Civic          1  1    4 2.000000
#> Toyota Corolla       1  0    4 1.000000
#> Toyota Corona        1  0    3 1.000000
#> Dodge Challenger     0  0    3 2.000000
#> AMC Javelin          0  0    3 2.000000
#> Camaro Z28           0  0    3 4.000000
#> Pontiac Firebird     0  0    3 2.000000
#> Fiat X1-9            1  1    3 1.000000
#> Porsche 914-2        0  1    5 2.000000
#> Lotus Europa         1  1    5 2.000000
#> Ford Pantera L       0  1    5 4.000000
#> Ferrari Dino         0  1    5 6.000000
#> Maserati Bora        0  1    5 8.000000
#> Volvo 142E           1  1    4 2.000000