knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

dmsepmlr

Delta Method Standard Errors for various quantities related to the Predictive Margins of a Logistic Regression model with a binary main effect and any number of adjustors.

This package estimates predictive margins, along with their delta method standard errors, for a logistic regression model having a binary main effect and any number of adjustors. The margins are then used to produce additional estimates, including an "adjusted risk difference" and an "adjusted risk ratio."

Installation

You can install the development version of dmsepmlr from GitHub with:

# install.packages("devtools")
devtools::install_github("jameshenegan/dmsepmlr")

Example 1

In this example, we will look at a simulated epidemiological dataset (containing N=10000 observations) where each observation contains information on a subject's death status, disease status, and age.

library(dmsepmlr); data(epiDat); head(epiDat)

Suppose we wish to consider the following model:

glm(formula = death ~ disease + age,
    family=binomial(link = "logit"),
    data = epiDat)

Then we may use dmsepmlr::logisticMargins to obtain the predictive margins associated with disease values of 0 and 1, along with related estimates, for this model as follows:

logisticMargins(formula = death ~ disease + age,
                main_effect = "disease",
                over = FALSE,
                data = epiDat)

Note that

Comparison with Stata's margins command

Suppose that we have epiDat loaded into Stata. Then running logistic death i.disease age followed by margins disease would produce the following results:

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     disease |
          0  |   .0097401   .0009833     9.91   0.000     .0078129    .0116673
          1  |   .0176428   .0059259     2.98   0.003     .0060282    .0292574
------------------------------------------------------------------------------

Meanwhile, running lincom _b[1.disease] - _b[0.disease] would give us the following:

------------------------------------------------------------------------------
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         (1) |   .0079027    .006007     1.32   0.188    -.0038707    .0196761
------------------------------------------------------------------------------

Also, running nlcom (RR: _b[1.disease] / _b[0.disease]) would give us the following:

------------------------------------------------------------------------------
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          RR |   1.811361   .6352954     2.85   0.004     .5662054    3.056517
------------------------------------------------------------------------------

Finally, running

nlcom (OR: (_b[1.disease]/(1-_b[1.disease])) ///
         / (_b[0.disease]/(1-_b[0.disease])) ///
      )

would produce

------------------------------------------------------------------------------
             |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
          OR |   1.825933   .6514781     2.80   0.005     .5490597    3.102807
------------------------------------------------------------------------------

We see that these results closely resemble the results we obtained from our logisticMargins command.

Example 2

In this example, we use the same data and the same logistic model as above.

Now suppose that we ran the following command in Stata

margins, over(disease)

In this case, we would obtain the following results:

------------------------------------------------------------------------------
             |            Delta-method
             |     Margin   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
     disease |
          0  |   .0097077   .0009798     9.91   0.000     .0077874    .0116281
          1  |   .0190476   .0064454     2.96   0.003     .0064149    .0316804
------------------------------------------------------------------------------

We can obtain these results from dmsepmlr::logisticMargins by setting the over option to TRUE:

logisticMargins(formula = death ~ disease + age,
                main_effect = "disease",
                over = TRUE,
                data = epiDat)


jameshenegan/dmsepmlr documentation built on Jan. 1, 2021, 4:27 a.m.