---
title: "Pooling and Selection of Logistic Regression Models"
author: "Martijn W Heymans"
date: "`r Sys.Date()`"
output:
  bookdown::html_document2
vignette: >
  %\VignetteIndexEntry{psfmi_LogisticModels}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Introduction

With the `psfmi` package you can pool logistic regression models by using  
the following pooling methods: RR (Rubin's Rules), D1, D2, D3 and MPR 
(Median R Rule). 

You can also use forward or backward selection from
the pooled model. 

This vignette show you examples of how to apply these 
procedures.

# Examples 

* [Logistic Regression]
    + [Pooling without BS and method D1] 
    + [Pooling with BS and method D1]
    + [Pooling with BS and method MPR] 
    + [Pooling with BS including several interaction terms and method D2]
    + [Pooling with BS and forcing interaction terms and method D1]
    + [Pooling with BS including spline coefficient and method D1]

## Pooling without BS and method D1

```{r}

  library(psfmi)
  pool_lr <- psfmi_lr(data=lbpmilr, nimp=5, impvar="Impnr", Outcome="Chronic",
  predictors=c("Gender", "Smoking", "Function", "JobControl",
  "JobDemands", "SocialSupport"), method="D1")
  
  pool_lr$RR_model
 
```

Back to [Examples]

## Pooling with BS and method D1

Pooling Logistic regression models over 5 imputed datasets with backward selection
using a p-value of 0.05 and as method D1 and forcing the predictor "Smoking" in the 
models during backward selection.

```{r}

  library(psfmi)
  pool_lr <- psfmi_lr(data=lbpmilr, nimp=5, impvar="Impnr", Outcome="Chronic",
  predictors=c("Gender", "Smoking", "Function", "JobControl",
  "JobDemands", "SocialSupport"), keep.predictors = "Smoking",
  p.crit = 0.05, method="D1", direction = "BW")
  
  pool_lr$RR_model_final
  
  pool_lr$predictors_out
  
```

Back to [Examples]

## Pooling with BS and method MPR

Pooling Logistic regression models over 5 imputed datasets with backward selection
using a p-value of 0.05 and as method D1 and forcing the predictor "Smoking" in the 
models during backward selection.

```{r}

  library(psfmi)
  pool_lr <- psfmi_lr(data=lbpmilr, nimp=5, impvar="Impnr", Outcome="Chronic",
  predictors=c("Gender", "Smoking", "Function", "JobControl",
  "JobDemands", "SocialSupport"), keep.predictors = "Smoking",
  p.crit = 0.05, method="MPR", direction = "BW")
  
  pool_lr$RR_model_final
  
  pool_lr$predictors_out
  
```

Back to [Examples]

## Pooling with BS including several interaction terms and method D2
      
Pooling Logistic regression models over 5 imputed datasets with BS
using a p-value of 0.05 and as method D2. Several interaction terms,
including a categorical predictor, are part of the selection procedure.

```{r}

  library(psfmi)
  pool_lr <- psfmi_lr(data=lbpmilr, nimp=5, impvar="Impnr", Outcome="Chronic",
  predictors=c("Gender", "Smoking", "Function", "JobControl"), 
  p.crit = 0.05, cat.predictors = c("Carrying", "Satisfaction"),
  int.predictors = c("Carrying:Smoking", "Gender:Smoking"), method="D2", direction = "BW")
  
  pool_lr$RR_model_final
  
  pool_lr$predictors_out
  
```

Back to [Examples]

## Pooling with BS and forcing interaction terms and method D1
      
Same as above but now forcing several predictors, including interaction terms,
in the model during BS.

```{r}

  library(psfmi)
  pool_lr <- psfmi_lr(data=lbpmilr, nimp=5, impvar="Impnr", Outcome="Chronic",
  predictors=c("Gender", "Smoking", "Function", "JobControl"), 
  p.crit = 0.157, cat.predictors = c("Carrying", "Satisfaction"),
  int.predictors = c("Carrying*Smoking", "Gender*Smoking"),
  keep.predictors = c("Smoking*Carrying", "JobControl"), method="D1", direction = "FW")
  
  pool_lr$RR_model_final
  
  pool_lr$predictors_in
  
```

Back to [Examples]

## Pooling with BS including spline coefficient and method D1

Pooling Logistic regression models over 5 imputed datasets with BS
using a p-value of 0.05 and as method D1. A spline predictor and interaction 
term are part of the selection procedure.

```{r}

  library(psfmi)
  pool_lr <- psfmi_lr(data=lbpmilr, nimp=5, impvar="Impnr", Outcome="Chronic",
  predictors=c("Gender", "Smoking", "JobControl"), 
  p.crit = 0.157, cat.predictors = c("Carrying", "Satisfaction"),
  spline.predictors=c("Function"), int.predictors = c("Carrying:Smoking"), 
  nknots=3, method="D1", direction = "BW")
  
  pool_lr$RR_model_final
  
  pool_lr$predictors_out

```

Back to [Examples]
