The dataset is identical to the one provided by Stata and available from webuse::webuse("margex") with categorical variables explicitly encoded as factors.

margex

Format

A data frame with 3000 observations on the following 11 variables.

y

A numeric vector

outcome

A binary numeric vector with values (0,1)

sex

A factor with two levels

group

A factor with three levels

age

A numeric vector

distance

A numeric vector

ycn

A numeric vector

yc

A numeric vector

treatment

A factor with two levels

agegroup

A factor with three levels

arm

A factor with three levels

Source

http://www.stata-press.com/data/r14/margex.dta

See also

Examples

# Examples from Stata's help files # Also available from: webuse::webuse("margex") data("margex") # A simple case after regress # . regress y i.sex i.group # . margins sex m1 <- lm(y ~ factor(sex) + factor(group), data = margex) prediction(m1, at = list(sex = c("male", "female")))
#> Data frame with 6000 predictions from #> lm(formula = y ~ factor(sex) + factor(group), data = margex) #> with average predictions:
#> sex x #> male 60.56 #> female 78.88
# A simple case after logistic # . logistic outcome i.sex i.group # . margins sex m2 <- glm(outcome ~ sex + group, binomial(), data = margex) prediction(m2, at = list(sex = c("male", "female")))
#> Data frame with 6000 predictions from #> glm(formula = outcome ~ sex + group, family = binomial(), data = margex) #> with average predictions:
#> sex x #> male 0.1287 #> female 0.1905
# Average response versus response at average # . margins sex prediction(m2, at = list(sex = c("male", "female")))
#> Data frame with 6000 predictions from #> glm(formula = outcome ~ sex + group, family = binomial(), data = margex) #> with average predictions:
#> sex x #> male 0.1287 #> female 0.1905
# . margins sex, atmeans ## TODO # Multiple margins from one margins command # . margins sex group prediction(m2, at = list(sex = c("male", "female")))
#> Data frame with 6000 predictions from #> glm(formula = outcome ~ sex + group, family = binomial(), data = margex) #> with average predictions:
#> sex x #> male 0.1287 #> female 0.1905
prediction(m2, at = list(group = c("1", "2", "3")))
#> Data frame with 9000 predictions from #> glm(formula = outcome ~ sex + group, family = binomial(), data = margex) #> with average predictions:
#> group x #> 1 0.28262 #> 2 0.10748 #> 3 0.02911
# Margins with interaction terms # . logistic outcome i.sex i.group sex#group # . margins sex group m3 <- glm(outcome ~ sex * group, binomial(), data = margex) prediction(m3, at = list(sex = c("male", "female")))
#> Data frame with 6000 predictions from #> glm(formula = outcome ~ sex * group, family = binomial(), data = margex) #> with average predictions:
#> sex x #> male 0.1562 #> female 0.1984
prediction(m3, at = list(group = c("1", "2", "3")))
#> Data frame with 9000 predictions from #> glm(formula = outcome ~ sex * group, family = binomial(), data = margex) #> with average predictions:
#> group x #> 1 0.3211 #> 2 0.1152 #> 3 0.0265
# Margins with continuous variables # . logistic outcome i.sex i.group sex#group age # . margins sex group m4 <- glm(outcome ~ sex * group + age, binomial(), data = margex) prediction(m4, at = list(sex = c("male", "female")))
#> Data frame with 6000 predictions from #> glm(formula = outcome ~ sex * group + age, family = binomial(), #> data = margex) #> with average predictions:
#> sex x #> male 0.1601 #> female 0.1967
prediction(m4, at = list(group = c("1", "2", "3")))
#> Data frame with 9000 predictions from #> glm(formula = outcome ~ sex * group + age, family = binomial(), #> data = margex) #> with average predictions:
#> group x #> 1 0.22513 #> 2 0.15060 #> 3 0.07362
# Margins of continuous variables # . margins, at(age=40) prediction(m4, at = list(age = 40))
#> Data frame with 3000 predictions from #> glm(formula = outcome ~ sex * group + age, family = binomial(), #> data = margex) #> with average prediction:
#> age x #> 40 0.1134
# . margins, at(age=(30 35 40 45 50)) prediction(m4, at = list(age = c(30, 35, 40, 45, 50)))
#> Data frame with 15000 predictions from #> glm(formula = outcome ~ sex * group + age, family = binomial(), #> data = margex) #> with average predictions:
#> age x #> 30 0.0500 #> 35 0.0760 #> 40 0.1134 #> 45 0.1647 #> 50 0.2315
# Margins of interactions # . margins sex#group prediction(m4, at = list(sex = c("male", "female"), group = c("1", "2", "3")))
#> Data frame with 18000 predictions from #> glm(formula = outcome ~ sex * group + age, family = binomial(), #> data = margex) #> with average predictions:
#> sex group x #> male 1 0.23796 #> female 1 0.21586 #> male 2 0.06583 #> female 2 0.20544 #> male 3 0.05380 #> female 3 0.08545