Author: Martijn W Heymans
Date: 2022-10-02

# mice and miceafter for Regression modelling

#### Martijn W Heymans

#### 2022-10-02

# 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
- mice and miceafter for pooling linear regression models
- mice and miceafter for selecting logistic regression models
- mice and miceafter for selecting linear regression models

## 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.40338191 2.74423975 -0.87579152  84.20120 0.383634957
    #> 2            Gender -0.28670287 0.44435492 -0.64521143 141.73082 0.519833276
    #> 3           Smoking  0.04202166 0.36469966  0.11522264 147.58067 0.908425278
    #> 4          Function -0.05363049 0.04956821 -1.08195346 107.14308 0.281702325
    #> 5        JobControl -0.00173824 0.02061271 -0.08432858 129.67391 0.932925243
    #> 6        JobDemands  0.01498679 0.04353545  0.34424352  56.23643 0.731947115
    #> 7     SocialSupport  0.05310413 0.05999722  0.88510971 131.15846 0.377717252
    #> 8 factor(Carrying)2  1.34848097 0.55234977  2.44135336  55.53348 0.017845620
    #> 9 factor(Carrying)3  2.12170752 0.63137426  3.36045934  31.10486 0.002072278
    #>           OR    lower.EXP upper.EXP
    #> 1 0.09041167 0.0003857145 21.192541
    #> 2 0.75073476 0.3118843294  1.807089
    #> 3 1.04291707 0.5072829086  2.144121
    #> 4 0.94778225 0.8590808969  1.045642
    #> 5 0.99826327 0.9583722748  1.039815
    #> 6 1.01509966 0.9303289078  1.107595
    #> 7 1.05453944 0.9365209770  1.187430
    #> 8 3.85157044 1.2735219585 11.648480
    #> 9 8.34537526 2.3029413419 30.241885
      poolm$pmultiparm
    #>                  p-values D1 F-statistic
    #> Gender           0.518810621 0.416297788
    #> Smoking          0.908268699 0.013276257
    #> Function         0.279775979 1.170623285
    #> JobControl       0.932805433 0.007111309
    #> JobDemands       0.731342832 0.118503602
    #> SocialSupport    0.376212395 0.783419204
    #> factor(Carrying) 0.002402598 6.548057457

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.890240724 2.23539512  2.63498863 128.45105 0.0094493646
    #> 2            Gender -0.390855578 0.39833904 -0.98121335 107.45765 0.3286915488
    #> 3           Smoking -0.175561523 0.31782610 -0.55238234 121.15417 0.5817040589
    #> 4          Function -0.044340247 0.04278381 -1.03637903  88.25400 0.3028567022
    #> 5        JobControl -0.030759130 0.01776239 -1.73170044 112.07495 0.0860784235
    #> 6        JobDemands  0.030146930 0.03415546  0.88263875 112.08333 0.3793205875
    #> 7     SocialSupport  0.002649424 0.05530205  0.04790825  59.62119 0.9619494899
    #> 8 factor(Carrying)2  0.614705092 0.42825620  1.43536764  72.28901 0.1554952062
    #> 9 factor(Carrying)3  1.735801455 0.44486453  3.90186524 110.28285 0.0001645841
    #>         2.5 %       97.5 %
    #> 1  1.46727777 10.313203676
    #> 2 -1.18047780  0.398766644
    #> 3 -0.80477403  0.453650982
    #> 4 -0.12936067  0.040680181
    #> 5 -0.06595276  0.004434502
    #> 6 -0.03752718  0.097821042
    #> 7 -0.10798560  0.113284450
    #> 8 -0.23894973  1.468359916
    #> 9  0.85420951  2.617393395
      poolm$pmultiparm
    #>                  p-values D1 F-statistic
    #> Gender           0.326939540   0.9627796
    #> Smoking          0.580814131   0.3051262
    #> Function         0.300938676   1.0740815
    #> JobControl       0.083815260   2.9987864
    #> JobDemands       0.377767222   0.7790512
    #> SocialSupport    0.961870518   0.0022952
    #> factor(Carrying) 0.001186615   7.2100032

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 - JobControl
    #> Removed at Step 2 is - Smoking
    #> Removed at Step 3 is - JobDemands
    #> 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.653325 0.4191626 -3.944353 69.55580 1.885794e-04
    #> 2 factor(Carrying)2  1.474823 0.5130543  2.874595 73.06998 5.295555e-03
    #> 3 factor(Carrying)3  2.347491 0.5427445  4.325223 48.89222 7.486964e-05
    #>           OR  lower.EXP  upper.EXP
    #> 1  0.1914124 0.08295868  0.4416499
    #> 2  4.3702631 1.57196111 12.1499185
    #> 3 10.4592952 3.51396246 31.1320502
      poolm$pmultiparm
    #>                   p-values D1 F-statistic
    #> factor(Carrying) 6.656759e-05    10.34468

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 - JobDemands
    #> Removed at Step 4 is - Function
    #> Removed at Step 5 is - Gender
    #>
    #> Selection correctly terminated,
    #> No more variables removed from the model
      poolm$pmodel
    #>                term    estimate  std.error statistic        df      p.value
    #> 1       (Intercept)  6.01797705 1.03680867  5.804327  94.92544 8.517292e-08
    #> 2        JobControl -0.03139866 0.01698247 -1.848887 120.12525 6.693288e-02
    #> 3 factor(Carrying)2  0.75033713 0.41757890  1.796875  53.99690 7.794927e-02
    #> 4 factor(Carrying)3  2.00235646 0.40304875  4.968026  74.18297 4.210313e-06
    #>         2.5 %      97.5 %
    #> 1  3.95963075 8.076323364
    #> 2 -0.06502241 0.002225087
    #> 3 -0.08685926 1.587533529
    #> 4  1.19929722 2.805415710
      poolm$pmultiparm
    #>                   p-values D1 F-statistic
    #> JobControl       6.485343e-02    3.418382
    #> factor(Carrying) 2.386445e-05   12.211908

Back to Examples