Stata 15 help for margins_generate

Title

[R] margins, generate() -- Create margin-response variables in the current dataset

Syntax

margins [marginlist] [if] [in] [weight] [, generate(stub) response_options options]

Description

margins, generate() creates a new variable containing the response values used to produce each margin or marginal effect reported by margins.

Option

generate(stub) creates a new variable containing the response values used to produce each margin or marginal effect reported by margins. The variables are named consecutively, starting with stub1. margins will skip over variable names that already exist. stub may not exceed 16 characters in length.

generate() is not allowed with contrasts; see margins, contrast.

generate() is not allowed with pairwise comparisons; see margins, pwcompare.

Remarks

Suppose we are interested in the effects of a regressor on the predicted probability from a logistic regression. Here is the setup.

. webuse margex . logistic outcome i.sex i.group sex#group age

We can compute the average marginal effect of age on the predicted probability for outcome.

. margins, dydx(age) generate(dage)

The new variable dage1 contains the values that were summarized to produce the average marginal effect reported by margins. We can plot this variable against age.

. scatter dage1 age

This scatterplot is not very helpful; the markers do not distinguish themselves according to the levels of sex or group. Let's use the by() option to help separate the plots according to sex and group.

. scatter dage1 age, by(sex group)

To overlay line plots, we will first sort on age and then use if conditions to identify the plots that correspond to the levels of sex and group. We also need to specify our own labels for the legend.

. sort age . line dage1 age if 0.sex&1.group || line dage1 age if 0.sex&2.group || line dage1 age if 0.sex&3.group || line dage1 age if 1.sex&1.group || line dage1 age if 1.sex&2.group || line dage1 age if 1.sex&3.group || , legend(label(1 female, group 1) label(2 female, group 2) label(3 female, group 3) label(4 male, group 1) label(5 male, group 2) label(6 male, group 3))

Suppose we were interested in the effects of age when setting all other predictors at their means. The other predictors are factor variables, so let's see how the effects of age differ when we use the sample means (observed relative frequencies) of sex and group compared with treating them as balanced.

First, let's compute the effects of age at the means.

. margins, dydx(age) at((means) sex group) generate(dage)

The default label is not very informative in this case, so we will use our own.

. label variable dage2 "dydx(age) at means of factors sex and age"

Second, we compute the effects of age while treating the factors as balanced and give the generated variable our own label.

. margins, dydx(age) asbalanced generate(dage) . label variable dage2 "dydx(age) treating sex and age as balanced"

Now we can plot these two response variables.

. line dage2 dage3 age, legend(cols(1))

Stored results

margins, generate() stores the following additional results in r():

Macros r(generate) list of new variables created because of the generate() option

margins, generate() with the post option also stores the following additional results in e():

Macros e(generate) list of new variables created because of the generate() option


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index