mice and miceafter for Regression modelling

Martijn W Heymans

2021-12-15

Installing the miceafter and mice packages

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("mwheymans/miceafter")

You can install mice with:

install.packages("mice")

Examples

mice and miceafter for pooling logistic regression models

lbp_orig is a dataset that is part of the miceafter package with missing values. So we first impute them with the mice function. Than we use the mids2milist function to turn a mids object, as a result of using mice, into a milist object with multiply imputed datasets. Than we use the with function to apply repeated logistic regression analyses. With the pool_glm function we obtain the results for the pooled model.


  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Chronic ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport, 
                      family = binomial))
  
  poolm <- pool_glm(ra, method="D1")
  poolm$pmodel
#>                term     estimate  std.error   statistic        df      p.value
#> 1       (Intercept) -2.260292931 2.60675417 -0.86709094 131.17333 0.3874755733
#> 2            Gender -0.251458399 0.45622031 -0.55117756 123.72743 0.5825059289
#> 3           Smoking  0.092463918 0.36571987  0.25282717 146.12564 0.8007570877
#> 4          Function -0.067385062 0.04894641 -1.37671110 118.82274 0.1711900839
#> 5        JobControl  0.001945212 0.02067257  0.09409629 123.76195 0.9251848357
#> 6        JobDemands -0.001787363 0.04119629 -0.04338651  73.23443 0.9655115098
#> 7     SocialSupport  0.067720088 0.05906083  1.14661589 146.73732 0.2534066780
#> 8 factor(Carrying)2  1.320033423 0.55685121  2.37053165  53.16840 0.0214201916
#> 9 factor(Carrying)3  2.062913771 0.58999052  3.49652019  52.48606 0.0009679294
#>          OR    lower.EXP upper.EXP
#> 1 0.1043199 0.0006009427 18.109291
#> 2 0.7776658 0.3152258882  1.918510
#> 3 1.0968736 0.5324215277  2.259735
#> 4 0.9348352 0.8484829208  1.029976
#> 5 1.0019471 0.9617772901  1.043795
#> 6 0.9982142 0.9195351373  1.083625
#> 7 1.0700657 0.9521814836  1.202545
#> 8 3.7435465 1.2253345525 11.436991
#> 9 7.8688645 2.4091454282 25.701657
  poolm$pmultiparm
#>                  p-values D1 F-statistic
#> Gender           0.581622395 0.303796706
#> Smoking          0.800402969 0.063921577
#> Function         0.168960812 1.895333441
#> JobControl       0.925049540 0.008854112
#> JobDemands       0.965442760 0.001882389
#> SocialSupport    0.251543772 1.314728006
#> factor(Carrying) 0.002281544 6.467414181

Back to Examples

mice and miceafter for pooling linear regression models

The lbp_orig is a dataset that is part of the miceafter package with missing values. So we first impute them with the mice function. Than we use the mids2milist function to turn a mids object, as a result of using mice, into a milist object with multiply imputed datasets. Than we use the with function to apply repeated linear regression analyses. With the pool_glm function we obtain the results for the pooled model.


  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Pain ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport))
  
  poolm <- pool_glm(ra, method="D1")
  poolm$pmodel
#>                term     estimate  std.error   statistic        df      p.value
#> 1       (Intercept)  5.312432063 2.35343514  2.25730974  99.01936 0.0261851098
#> 2            Gender -0.305142321 0.42349524 -0.72053306  78.61191 0.4733340119
#> 3           Smoking -0.155036838 0.32899402 -0.47124516 107.24133 0.6384229209
#> 4          Function -0.056641302 0.04357635 -1.29981738  84.31016 0.1972061839
#> 5        JobControl -0.023216627 0.02073903 -1.11946546  33.37654 0.2709333685
#> 6        JobDemands  0.035812621 0.03393475  1.05533766 120.57064 0.2933810253
#> 7     SocialSupport  0.001992314 0.05241231  0.03801233 128.24564 0.9697369609
#> 8 factor(Carrying)2  0.646981227 0.44018691  1.46978752  70.64756 0.1460588187
#> 9 factor(Carrying)3  1.719404307 0.45865246  3.74881741  98.39179 0.0002999823
#>         2.5 %     97.5 %
#> 1  0.64271746 9.98214666
#> 2 -1.14815322 0.53786857
#> 3 -0.80721233 0.49713865
#> 4 -0.14329300 0.03001040
#> 5 -0.06539242 0.01895917
#> 6 -0.03137259 0.10299783
#> 7 -0.10171250 0.10569712
#> 8 -0.23080243 1.52476488
#> 9  0.80926873 2.62953988
  poolm$pmultiparm
#>                  p-values D1 F-statistic
#> Gender           0.472016303 0.519167896
#> Smoking          0.637663081 0.222071997
#> Function         0.194894308 1.689525218
#> JobControl       0.268422594 1.253202906
#> JobDemands       0.291542849 1.113737573
#> SocialSupport    0.969682958 0.001444937
#> factor(Carrying) 0.001209598 7.021696406

Back to Examples

mice and miceafter for selecting logistic regression models

We follow the same procedure as the first example but also apply model selection here.


  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Chronic ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport, 
                      family = binomial))
  
  poolm <- pool_glm(ra, method="D1", p.crit = 0.15, direction = "BW")
#> Removed at Step 1 is - JobDemands
#> Removed at Step 2 is - JobControl
#> Removed at Step 3 is - Smoking
#> Removed at Step 4 is - Gender
#> Removed at Step 5 is - SocialSupport
#> Removed at Step 6 is - Function
#> 
#> Selection correctly terminated, 
#> No more variables removed from the model
  poolm$pmodel
#>                term  estimate std.error statistic       df      p.value
#> 1       (Intercept) -1.641978 0.4235486 -3.876717 64.96485 2.492862e-04
#> 2 factor(Carrying)2  1.473997 0.5234692  2.815823 59.30149 6.595524e-03
#> 3 factor(Carrying)3  2.310557 0.5400553  4.278370 52.47278 8.005775e-05
#>           OR  lower.EXP  upper.EXP
#> 1  0.1935967 0.08308659  0.4510917
#> 2  4.3666529 1.53211445 12.4453221
#> 3 10.0800344 3.41128404 29.7855859
  poolm$pmultiparm
#>                   p-values D1 F-statistic
#> factor(Carrying) 0.0001027386    9.900436

Back to Examples

mice and miceafter for selecting linear regression models

We follow the same procedure as the second example but also apply model selection here.


  library(mice)
  library(miceafter)
  
  imp <- mice(lbp_orig, m=5, maxit=5, printFlag = FALSE) 
 
  dat_imp <- mids2milist(imp)
  
  ra <- with(dat_imp, expr = glm(Pain ~ factor(Carrying) + Gender + Smoking + 
                      Function + JobControl + JobDemands + SocialSupport))
  
  poolm <- pool_glm(ra, method="D1", p.crit = 0.15, direction = "BW")
#> Removed at Step 1 is - SocialSupport
#> Removed at Step 2 is - Smoking
#> Removed at Step 3 is - Gender
#> Removed at Step 4 is - JobControl
#> Removed at Step 5 is - JobDemands
#> 
#> Selection correctly terminated, 
#> No more variables removed from the model
  poolm$pmodel
#>                term    estimate  std.error statistic        df      p.value
#> 1       (Intercept)  5.11202356 0.59979204  8.522993 147.94717 1.643130e-14
#> 2          Function -0.07162454 0.04236532 -1.690641  88.95577 9.440666e-02
#> 3 factor(Carrying)2  0.65368674 0.41643596  1.569717  87.50921 1.200899e-01
#> 4 factor(Carrying)3  1.67783504 0.45042607  3.724995 109.48190 3.106190e-04
#>        2.5 %     97.5 %
#> 1  3.9267575 6.29728959
#> 2 -0.1558041 0.01255502
#> 3 -0.1739569 1.48133040
#> 4  0.7851493 2.57052077
  poolm$pmultiparm
#>                  p-values D1 F-statistic
#> Function         0.092086236    2.858267
#> factor(Carrying) 0.001327855    6.867307

Back to Examples