vignettes/regression_modelling.Rmd
regression_modelling.Rmd
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")
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.960648806 2.72404744 -1.0868566 117.27816 0.2793289649
#> 2 Gender -0.180328611 0.44970827 -0.4009902 138.17900 0.6890467201
#> 3 Smoking 0.068334034 0.36803017 0.1856751 143.33859 0.8529620542
#> 4 Function -0.072190836 0.04792059 -1.5064681 146.19589 0.1341047367
#> 5 JobControl 0.008134227 0.02100327 0.3872839 125.39176 0.6992026151
#> 6 JobDemands 0.013944105 0.03994138 0.3491142 125.29641 0.7275895600
#> 7 SocialSupport 0.061680286 0.05912646 1.0431926 145.16914 0.2985932062
#> 8 factor(Carrying)2 1.261310390 0.51899537 2.4302922 103.26284 0.0168112650
#> 9 factor(Carrying)3 2.014313910 0.56782913 3.5473945 71.03321 0.0006939056
#> OR lower.EXP upper.EXP
#> 1 0.05178531 0.0002351356 11.404987
#> 2 0.83499578 0.3431703946 2.031696
#> 3 1.07072291 0.5172992965 2.216217
#> 4 0.93035333 0.8462864214 1.022771
#> 5 1.00816740 0.9671201139 1.050957
#> 6 1.01404178 0.9369709348 1.097452
#> 7 1.06362224 0.9463151471 1.195471
#> 8 3.53004419 1.2611659054 9.880708
#> 9 7.49558297 2.4159774853 23.255086
poolm$pmultiparm
#> p-values D1 F-statistic
#> Gender 0.688449430 0.16079314
#> Smoking 0.852702972 0.03447524
#> Function 0.131953888 2.26944625
#> JobControl 0.698612964 0.14998880
#> JobDemands 0.727063567 0.12188075
#> SocialSupport 0.296870051 1.08825075
#> factor(Carrying) 0.001523331 6.67912444
Back to Examples
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.061670808 2.37558194 2.1307077 92.34091 0.035770634
#> 2 Gender -0.412535088 0.42615605 -0.9680376 62.33263 0.336766766
#> 3 Smoking -0.226978139 0.33652558 -0.6744751 68.87306 0.502267042
#> 4 Function -0.042163929 0.04345018 -0.9703972 78.59812 0.334825390
#> 5 JobControl -0.018217096 0.01806178 -1.0085989 123.40437 0.315140565
#> 6 JobDemands 0.038345247 0.03548651 1.0805583 79.31704 0.283168284
#> 7 SocialSupport -0.006239621 0.05266348 -0.1184810 112.00348 0.905898787
#> 8 factor(Carrying)2 0.632072006 0.48548993 1.3019261 27.56761 0.203715768
#> 9 factor(Carrying)3 1.781114782 0.50829159 3.5041201 34.45430 0.001291286
#> 2.5 % 97.5 %
#> 1 0.34379210 9.77954952
#> 2 -1.26431848 0.43924831
#> 3 -0.89835038 0.44439410
#> 4 -0.12865622 0.04432836
#> 5 -0.05396813 0.01753393
#> 6 -0.03228450 0.10897500
#> 7 -0.11058552 0.09810628
#> 8 -0.36311225 1.62725626
#> 9 0.74864345 2.81358611
poolm$pmultiparm
#> p-values D1 F-statistic
#> Gender 0.334856369 0.93709682
#> Smoking 0.501016804 0.45491671
#> Function 0.332990684 0.94167065
#> JobControl 0.313389972 1.01727182
#> JobDemands 0.281135147 1.16760621
#> SocialSupport 0.905724346 0.01403775
#> factor(Carrying) 0.003269697 6.58147389
Back to Examples
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
#>
#> Selection correctly terminated,
#> No more variables removed from the model
poolm$pmodel
#> term estimate std.error statistic df p.value
#> 1 (Intercept) -0.6463402 0.7055623 -0.9160639 138.76581 0.3612226050
#> 2 Function -0.0777323 0.0463289 -1.6778359 136.46709 0.0956679402
#> 3 factor(Carrying)2 1.3293534 0.4983916 2.6672870 133.49874 0.0085929718
#> 4 factor(Carrying)3 1.9359933 0.5441045 3.5581276 99.03639 0.0005751815
#> OR lower.EXP upper.EXP
#> 1 0.5239599 0.1298490 2.114255
#> 2 0.9252121 0.8442153 1.013980
#> 3 3.7785992 1.4099975 10.126126
#> 4 6.9309254 2.3546140 20.401529
poolm$pmultiparm
#> p-values D1 F-statistic
#> Function 0.093532445 2.815133
#> factor(Carrying) 0.003795054 5.923500
Back to Examples
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 - JobControl
#> Removed at Step 4 is - Gender
#>
#> Selection correctly terminated,
#> No more variables removed from the model
poolm$pmodel
#> term estimate std.error statistic df p.value
#> 1 (Intercept) 3.24769137 1.38299331 2.348306 73.95247 0.0215346818
#> 2 Function -0.06968319 0.04126367 -1.688730 87.79055 0.0948202978
#> 3 JobDemands 0.05244183 0.03509141 1.494435 63.86245 0.1399870309
#> 4 factor(Carrying)2 0.77400583 0.39918838 1.938949 112.53216 0.0550114940
#> 5 factor(Carrying)3 1.75128657 0.44974755 3.893932 84.95802 0.0001957944
#> 2.5 % 97.5 %
#> 1 0.49198762 6.0033951
#> 2 -0.15168879 0.0123224
#> 3 -0.01766424 0.1225479
#> 4 -0.01689393 1.5649056
#> 5 0.85706157 2.6455116
poolm$pmultiparm
#> p-values D1 F-statistic
#> Function 0.0924799344 2.851809
#> JobDemands 0.1374825475 2.233335
#> factor(Carrying) 0.0004978015 7.869088
Back to Examples