*! version November 29, 2021 @ 17:24:25 UK

Title

twostep -- Twostep multilevel analysis

Syntax 1

twostep is both, a prefix command (Syntax 1), and a standalone
command (Syntax 2). The main purpose is the prefix command:

twostep
cluster_id
[,
twostep-options
]:
cmd-1
||
cmd-2

cmd-1 may be an estimation command such as regress, logit, etc., or

unitcpr
varlist1
[if] [in]
[weight]
||
varlist2
[ using filename ]
[, unitcpr-options ]

cmd-2 may be one of the following special purpose commands

clustercpr
varlist2
[if] [in]
[ using filename ]
[, clustercpr-options ]

dot
varlist2
[if] [in]
[ using filename ]
[, dot-options ]

edv
varlist2
[if] [in]
[ using filename ]
[, edv-options ]

mk2nd
varlist2
[if] [in]
[ using filename ]
[, mk2nd-options ]

unitreby
varlist2
[if] [in]
[ using filename ]
[, unitregby-options ]

or an arbitrary Stata command with the syntax

cmd
varlist2
[if] [in]
[, options ]

The latter usage is being called "fallback mode" in the
following.

clustercpr-options
method(arg) Options allowed for the EDV model twostep

lowessopts(options) Options allowed for twoway lowess

regopts(options) Options allowed for twoway line

scopts(options) Options allowed for twoway scatter

twoway options Options allowed for graph twoway

dot-options
ciopts(options) Options allowed for twoway rcap

scopts(options) Options allowed for twoway scatter

twoway options Options allowed for graph twoway

edv-options
method(arg) Method for cluster level estimation

regress options Method allowed for regress

mk2nd-options
clear It's ok to replace data

twostep-options
stats(namelist) Additional Stats for cluster level

unitcpr-options
allopts(options) Options allowed for twoway line

byopts(options) Options allowed for graph, by()

lowessopts(options) Options allowed for twoway lowess

regopts(options) Options allowed for twoway line

scopts(options) Options allowed for twoway scatter

twoway options Options allowed for graph twoway

unitregby-options
allopts(options) Options listed in line option

byopts(options) Options allowed for graph, by()

discrete(varlist) Treat cluster level vars as discrete}

nquantiles(#) Grouping for cluster level varlist

regopts(options) Options listed in line option

unitby(varlist) Additional grouping on unit level varlist

twoway options Options allowed for graph twoway

clusterid refers to a varlist identifying
clusters in the unit level data set. varlist2 refer to
variables on the cluster level, including the variables holding the
statistics estimated on the unit level for each cluster.

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquelly
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

weights are allowed for cmd1 if the estimation command
being used allows weights. In the case of unitcpr, fweights,
aweights and pweights are allowed; see help weight.
weights are not allowed for cmd2.

Description

twostep is a bundle of programs to ease multilevel
analyses with the twostep approach. The twostep approach to mulitlevel
analysis means to separately estimate a parameter of interest in a unit
level data set (e.g. individuals) for all categories of a cluster
level variable (e.g. countries, states, schools, etc.). The twostep approach
is sometimes seen as superior to the more standard one-step approach
(see mixed) if the numbers of observation on the second level
becomes small (see Achen 2005). Additionally,
two-step mulitlevel analysis may be used for checking the model
assumptions of the one-step approach, or for exploratory data analysis
of multilevel data.

Most of the methods implemented in twostep are just
wrappers, which can be also produced by standard Stata
commands. However, the idea of twostep is to offer
several related methods with very similar commands. We hope that this helps
researchers to follow the twostep approach more interactively.

The following examples highlight this consistent design of
twostep using a subset of the forth round of the European
Quality of Life Survey (EQLS). The EQLS is a survey on probability
samples of respondents in all European Union member countries and
hence a typical example of an international comparative data set. A
special feature of the EQLS is that it also contains a number of
cluster level variables in the unit level data set. The following
examples use hdirank, i.e. the rank of a country in the Human
Development Index.

twostep contains the following methods:

o Estimated Dependend Variable Model (EDV regression) as
described by Lewis and Linzer (2005). In this
model, the estimated regression coefficients for each cluster of the
unit level data set are feed as dependent variable into a linear
regression on the cluster level. The cluster level regression is
weighted by an inverse function of the uncertainty of the model
estimates. For example,

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || edv _b_hhinc hdirank

estimates an EDV regression of the unit level coefficients
for hhinc (household income) on the cluster level variable hdirank
(the rank of the human development index). See section EDV model for variants of the EDV model.

o Horizontally labeled dot charts of coefficients with
confidence intervals as shown by Bowers and Drake (2005, figure
1). For example,

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || dot _b_hhinc hdirank

shows a plot of all the estimated unit level coefficients for
hhinc in a dot chart, where the coefficients are sorted by the field
rank of the cluster level variable hdirank. See section
Dot chart for variants of this plot.

o Component plus residual plots for all the unit level
models. This plot is a variant of a plot proposed by Bowers and
Drake (2005, figure 2). For example,

. use eqls_4x, clear

. twostep cntry: unitcpr lsat hhinc i.sex || _b_hhinc hdirank

shows the component plus residual plots of the coefficient for
hhinc for each cluster (i.e. country). Each of the plots show the regression
line for all observations in comparison to the cluster specific
regression lines and in comparison to a cluster specific
non-parametric regression (LOWESS). The single plots are sorted by the
field rank of the cluster level variable hdirank. See section
unitcpr for variants of this plot. Also see
cprplot for a general description of the component plus
residual plot.

o Component plus residual plots for the EDV regression
models on the cluster level. This plot is a straightforward application
of the standard component plus residual plot for the case of an
estimated dependend variable. For example,

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || clustercpr _b_hhinc hdirank

shows a component plus residual plot for the EDV model of
the estimated unit level regression coefficients of hhinc on the
cluster level variable hdirank. The plot contains the regression line
and a non-parametric regression line (LOWESS). See section
clustercpr for variants of this
plot. Also see cprplot for a general description of the
component plus residual plot.

o Plots of grouped predictions of unit level models as
proposed by Bowers and Drake (2005, figure
3) -- we call this a "unitregby plot" in the following. For example,

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || unitregby _b_hhinc hdirank

uses a unitregby plot to show regression lines based on the
coefficient of hhinc for the unit level regressions of all clusters,
whereby the clusters are grouped into categories derived from the
cluster level variable hdirank.
See section unitregby for variants of this
plot.

o Distributional diagnostic plots for the estimated
regression coefficients of the unit level regression model. These
plots are particularly useful to inspect the distributional
assumptions for random intercept and/or random coefficient models. For
example,

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || pnorm _b_hhinc

shows a standardized normal plot for the cluster specific
estimated regression coefficient of hhinc. Instead of pnorm
all commands described in diagnostic plots except qqplot
can be used.

o Fallback mode: Besides the methods described,
twostep has a fallback mode that allows the specification of
arbitrary Stata commands to analyze the unit level regression
coefficients (or other model statistics) on the cluster level. For example,

. use eqls_4x, clear

. twostep cntry, stats(r2): regress lsat hhinc i.sex || scatter _stat_r2 hdirank

shows a scatter plot of R2 form the unit level regressions
on the cluster level variable hdirank. The fallback mode, assumes that
the Stata command follows a standard syntax. Moreover, the first
variable of the variable list of the fallback mode command must be an
estimate or a statistic of the unit level models.

o Creation of cluster level data set As a second fallback
mode, twostep can be used to create a cluster level data set
holding regression coefficients of the unit level regressions. This is
useful for two-step multilevel analyses that cannot be
done with any the implemented methods. For example,

. use eqls_4x, clear

. twostep cntry, stats(r2): regress lsat hhinc i.sex || mk2nd _all hdirank

creates a data set holding all the cluster specific unit
level regression coefficients, their standard errors and the cluster
level variable hdirank. The standalone command twostep can then
be used to estimate an EDV model for each of the coefficents,
including the constant:

. twostep _b_cons hdirank

. twostep _b_hhinc hdirank

. twostep _b_2_sex hdirank

The following descriptions of the programs are arranged in
alphabetical order.

Cluster level component plus residual plot

Cluster level component plus residual plots are shown with the cluster
level command clustercprt. The general syntax is :

twostep
varlist1: modcmd depvar1
varlist2 [, options1] ||
clustercpr
depvar1
varlist3
[ using filename ]
[, options2 ]

with modcmd being an arbitrary estimation command (such as
regress, logit, areg, etc.

options1
regress options Options allowed for modcmd

options2
scopts(options) Options allowed for twoway scatter

regopts(options) Options allowed for twoway line

lowessopts(options) Options allowed for twoway lowess

twoway options Options allowed for graph twoway

varlist1 defines the clusters within the unit level
data set, i.e. the variable(s) identifying the cluster to which each
unit belongs. These are typically countries in international
comparative data, or states within countries, or students within
faculties, etc. The model behind the colon is then estimated for each
cluster, and one of the coefficients, or another model statistic are
feed into an EDV model. Finally, a component plus residual plot is
shown for the first independent variable of the EDV model.

depvar1 and varlist2 define the dependent and
independent variables for the unit level regression model
respectively. The model is estimated for each category of varlist1.

depvar2 and varlist3 define both, the EDV model and
the basic design of the component plus residual plot. depvar2
must refer to the estimated coefficient of one of the independent
variables of the unit level regression. This is done by adding
_b_ in front of variable name, or, in case of factor variables,
by adding _b_#_ with # referring to the category of the
respective variable. We suggest to use twostep ... || mk2nd _all
for checking the implied coefficient names in case of factor variables
with interactions.

varlist3 specifies the independent variables of the EDV
model and the first variable of varlist3 defines the variable for
which the component plus residual plot is being shown. The variables
must be constant within each category of varlist1. Moroever the
variables must exist, either in the unit level data set itself, or in
the data set specified by using.

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquely
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

clustercpr options

scopts(options) define the look of the symbols for the
cluster level observations. options can be any of the options
allowed for twoway scatter. Among these options,
msymbol(), msize(), and mcolor() may be particularly
usefull.

regopts(options) define the look of the regression
line. options can be any of the options allowed for
twoway line. Among these options, lcolor(), lwidth(), and
msize() may be particularly useful.

lowess(options) define the look of the lines for the
non-parametric regression line. options can be any of the options
allowed for twoway line. Among these options, lcolor(),
lwidth(), and msize() may be particularly usefull.

twoway options options allowed for graph twoway.

clustercpr examples

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || clustercpr _b_hhinc hdirank

. twostep cntry: regress lsat hhinc i.sex || clustercpr _b_hhinc hdirank corrupt

. twostep cntry: regress lsat hhinc i.sex || clustercpr _b_hhinc hdirank, regopts(lcolor(none))

erases the linear fit from the graph.

Cluster level data sets

mk2nd creates cluster level data sets holding selected
estimated coefficients, their standard errors and selected cluster
level variables

twostep
[ , stats(name) ]
varlist1: modcmd depvar1
varlist2 [, options1] ||
mk2nd
elist
varlist3
[ using filename ]
[ , clear ]

with modcmd being an arbitrary estimation command (such as
regress, logit, areg, etc., and elist
being a list of names for regression coefficients and model statistics.

twostep-options
stats(namelist) Additional Stats for cluster level

options1
model options Options allowed for modcmd

mk2nd-options
clear Okay to replace the data in memory

varlist1 defines the clusters within the unit level
data set, i.e. the variable(s) identifying the cluster to which each
unit belongs. These are typically countries in international
comparative data, or states within countries, or students within
faculties, etc.

elist and varlist2 define the dependent and
independent variables for the unit level regression model
respectively. The model is estimated for each category of varlist1.

elist and varlist3 define the coefficients and cluster
level variables to be stored in the data set to be created. elist
must refer to the estimated coefficient of one of the independent
variables of the unit level regression. The keyword _all can be
used to store all coefficients and standard errors.

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquely
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

mk2nd options

stats(namelist) define the model statistics to be stored
together with the model coefficients. names can be any scalar
stored behind by the estimation command used for the unit level
regression. For example, to store the R-square of a
linear regression model, use stats(r2).

clear specifies that it is okay to replace the data in
memory, even though the current data have not been
saved to disk.

mk2nd examples

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || mk2nd _b_hhinc hdirank

creates a cluster level data set holding the unit level's estimated regression
coefficients of hhinc with their standard errors, and numbers of
observations, as well as the cluster level variable
hdirank.

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || mk2nd _all hdirank

as before, but with all the unit level regression
coefficients/standard errors.

. use eqls_4x, clear

. twostep cntry, stats(r2 r2_a ll): regress lsat hhinc i.sex || mk2nd _all hdirank

as before, but including unit selected statistics of the unit level regression models.

Dot chart

Horizontally labeled dot charts with confidence intervals
of the unit level regression coefficients are shown with the cluster
level command dot. The general syntax is :

twostep
[ , stats(names) ]
varlist1: modcmd depvar1
varlist2 [, options1] ||
dot
depvar1
varlist3
[ using filename ]
[, options2 ]

with modcmd being an arbitrary estimation command (such as
regress, logit, areg, etc.

twostep
stats(namelist) Additional model statistics

options1
regress options Options allowed for regress

options2
scopts(options) Options allowed for twoway scatter

ciopts(options) Options allowed for twoway rcap

twoway options Options allowed for graph twoway

varlist1 defines the clusters within the unit level
data set, i.e. the variable(s) that identify the cluster to which each
unit belongs. These are typically countries in international
comparative data, or states within countries, or students within
faculties, etc. The model behind the colon is then estimated
for each cluster defined by varlist1 and one of the coefficients,
or another model statistic is then shown as a horizontally labeled dot
chart. Confidence intervals are shown for the estimated coefficients,
only.

depvar1 and varlist2 define the dependent and
independent variables for the unit level regression model
respectively. The model is estimated for each category of varlist1
and the results are used for the plot.

depvar2 and varlist3 control the design of the dot
chart. Thereby, depvar2 can be any variable available in the
cluster level data set. Usually depvar2 will refer to the variable
that holds the estimated coefficient of one of the independent
variables of the unit level regression. Alternatively, depvar2
may refer to the name of some other model statistic of the unit level
regression model. These other model statics can be requested with the
options stats(name) (see below).

In order to create a plot for the estimated regression
coefficients, add _b_ in front of name of the selected
independent variable, or, in case of factor variables, by adding
_b_#_ whith # refering to the category of the respective
variable. We suggest to use twostep ... || mk2nd _all for
checking the implied coefficient names in case of factor variables
with interactions.

In order to create a plot for an arbitrary model statistic
_stat_ in front of name of the statistic. The statistic must
have been specified in the option
stats(name). Theses statistics are shown without confidence
intervals.

varlist3 specifies the order of the statistics in plot. If
varlist3 is not specified, the plot is ordered by statistic
itself (from the lowest to the largest). If specified, the statistcs
are ordered by the field rank of varlist3. The variables must
be constant within each category of varlist1. Moroever the
variables must exist, either in the unit level data set itself, or in
the data set specified by using.

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquelly
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

Dot options

stats(namelist) define the model statistics to be stored
together with the model coefficients. names can be any scalar
stored behind by the estimation command used for the unit level
regression. For example, to store the R-square of a
linear regression model, use stats(r2).

scopt(options) defines the look of the dots. options
can be any of the options allowed for twoway scatter. Among
these options, msymbol(), msize(), and
mcolor() may be particularly usefull.

ciopt(options) defines the look of the confidence
intervals (if any). options can be any of the options allowed for
twoway rcap. Among these options, lcolor(),
lwidth(), and msize() may be particularly useful.

twoway options options allowed for graph twoway.

Dot examples

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || dot _b_hhinc

. twostep cntry: regress lsat hhinc i.sex || dot _b_hhinc hdirank

. twostep cntry: regress lsat hhinc i.sex || dot _b_hhinc hdirank, scopts(mcolor(red) ms(S)) ciopts(lwidth(0))

just to show how scopts() and ciopts() work.

. twostep cntry, stats(r2): regress lsat hhinc i.sex || dot _stat_r2

to show R-squared instead of coefficients.

. twostep cntry, stats(r2): regress lsat hhinc i.sex || dot _stat_r2, title(R2 by country) xtitle(R2) ysize(5)

an example with twoway options.

EDV model

The Estimated Dependent Variable Model (EDV model) is
estimated with the cluster level command edv, or with
twostep used as a standalone command on a data set created with
the cluster level command mk2nd. The general syntax is :

twostep
varlist1: regress depvar1
varlist2 [, options1] ||
edv
depvar1
varlist3
[ using filename ]
[, options2 ]

or

twostep
depvar
varlist
[, options2 ]

options1
regress options Options allowed for regress

options2
method(arg) One of ols, wls, borjas, fgls1, fgls2

regress options Options allowed for regress

varlist1 defines the clusters within the unit level
data set, i.e. the variable(s) that identify the cluster to which each
unit belongs. These are typically countries in international
comparative data, or states within countries, or students within
faculties, etc. The regression model behind the colon is then
estimated for each cluster defined by varlist1 and one of the
coefficients of that model is feed as the dependent variable into the
EDV model.

depvar1 and varlist2 define the dependent and
independent variables for the unit level regression model
respectively; see help regress. The model is estimated for each
category of varlist1 and the results are pushed into the EDV
model.

depvar2 and varlist3 define the EDV model. Thereby,
depvar2 must refer to the estimated coefficient of one of the
independent variables of the unit level regression. This is done by
adding _b_ in front of variable name, or, in case of factor
variables, by adding _b_#_ with # referring to the category of
the respective variable. We suggest to use twostep ... || mk2nd _all
for checking the implied coefficient names in case of factor
variables with interactions.

varlist3 specifies the independent variables of the EDV
model. The variables must be constant within each category of
varlist1. Moreover the variables must exist, either in the unit
level data set itself, or in the data set specified by using.

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquely
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

Technical Note: Instead of regress, twostep
allows arbitrary estimation commands for cmd1. Hence, it is
technically possible to run the EDV model on unit level regression
coefficients of logit, probit, xtreg or
whatever. Since the statistical properties of the EDV model has only
been shown for linear regression, twostep issues a warning
message when the unit level command is not regress.

Edv Options

method(name) defines the function used to weight the
observations in the EDV model. name can be any of borjas,
fgls1 (default), fgls2, ols, and wls.

borjas weights the observations using as proposed by
Borjas and Sueyoshi (1994).

fgls1 weights the observations using the FGLS approach
described by Lewis and Linzer (2005:351-352). This is the default if
method() is not specified.

fgls2 weights the observations using the FGLS approach
described by Lewis and Linzer (2005:352-354).

ols does not apply weights to the second level
regression.

wls weights the observations by the reciprocal value of
the regression coefficient's variance.

regress option options allowed for regress.

EDV model examples

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || edv _b_hhinc hdirank

. twostep cntry: regress lsat hhinc age if sex == 1 || edv _b_hhinc hdirank if !mi(eu15)

. twostep cntry: regress lsat hhinc i.sex || edv _b_hhinc hdirank, method(ols)

to regress _b_hhinc on hdirank without any weights at all

. gen lsat2 = lsat > 5 if !mi(lsat)

. twostep cntry: probit lsat2 hhinc i.sex || edv _b_hhinc hdirank, method(borjas)

for using twostep with probit.

Unit level component plus residual plot

Component plus residual plots for all the unit level
regression models are shown with the unit level command
unitcpr. The general syntax is :

twostep
[, stats(name) ]
varlist1: unitcpr depvar1
varlist2
[, options1 ]
||
depvar2
varlist3
[ using filename ]
[, options1 ]

Note: Unlike the other parts of towstep, unitcpr does not have a cluster level command.

options1
regress options Options allowed for regress

options2
allopts(options) Options allowed for twoway line

regopts(options) Options allowed for twoway line

lowessopts(options) Options allowed for twoway line

scopts(options) Options allowed for twoway scatter

twoway options Options allowed for graph twoway

varlist1 defines the clusters within the unit level
data set, i.e. the variable(s) identifying the cluster to which each
unit belongs. These are typically countries in international
comparative data, or states within countries, or students within
faculties, etc.

depvar1 and varlist2 define the dependent and
independent variables for unit level linear regression models. The
models are estimated for each cluster defined by varlist1. A
component plus residual plot is then shown for each of these
models.

depvar2 and varlist3 control the design of the
component plus residual plots. depvar2 refers one of the
independent variables of the unit level model, i.e. to
varlist2. Corresponding to the other twostep-methods, this is
done by putting _b_ in front of the name of the selected
independent variable, or, in case of factor variables, by adding
_b_#_ with # referring to the category of the respective
variable. We suggest to use twostep ... || mk2nd all for
checking the implied coefficient names in case of factor variables
with interactions.

varlist3 specifies the order of single component plus
residual plots in the graph. If specified, the plots are ordered by
the field rank of varlist3. The variables must be constant within
each category of varlist1. Moreover the variables must exist,
either in the unit level data set itself, or in the data set specified
by using. varlist3 may refer to stats specified in the
option stats().

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquelly
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

unitcpr options

stats(namelist) define the model statistics to be stored
together with the model coefficients. names can be any scalar
stored behind by regress. For example, to store the R-square,
use stats(r2).

allopts(options) defines the look of regression line
for all units . options can be
any of the options allowed for twoway line. Among these options,
lcolor(), and lwidth() may be particularly
usefull.

regopts(options) defines the look of cluster specific
regression lines. options can be any of the options allowed for
twoway line. Among these options, lcolor(), and
lwidth() may be particularly usefull.

lowessopts(options) defines the look of cluster specific
LOWESS lines. options can be any of the options allowed for
twoway line. Among these options, lcolor(), and
lwidth() may be particularly usefull.

scopt(options) defines the look of the dots. options
can be any of the options allowed for twoway scatter. Among
these options, msymbol(), msize(), and
mcolor() may be particularly usefull.

twoway options options allowed for graph twoway.

unitcpr examples

. use eqls_4x, clear

. twostep cntry: unitcpr lsat hhinc i.sex || _b_hhinc

for the most simple case}

. twostep cntry: unitcpr lsat hhinc i.sex || _b_hhinc hdirank, scopts(ms(i)) allopts(lwidth(0))

to show cluster specific regression and LOWESS lines, only.

. twostep cntry, stats(r2): unitcpr lsat hhinc i.sex || _b_hhinc _stat_r2

to order plots by R-square.

Unitregby plot

The unitregby plot shows regression lines of all the unit
level regressions for groups of cluster and/or unit level data,
similar to figure 3 in Bowers and Drake (2005). The general syntax is:

twostep
varlist1: modcmd depvar1
varlist2 [, options1] ||
clustercpr
depvar2
varlist3
[ using filename ]
[, options2 ]

with modcmd being an arbitrary estimation command (such as
regress, logit, areg, etc.

options1
regress options Options allowed for modcmd

options2
allopts(options) Options listed in line option

byopts(options) Options allowed for graph, by()

discrete(varlist) Treat cluster level vars as discrete}

nquantiles(#) Grouping for cluster level varlist

regopts(options) Options listed in line option

unitby(varlist) Additional grouping on unit level varlist

twoway options Options allowed for graph twoway

varlist1 defines the clusters within the unit level
data set, i.e. the variable(s) identifying the cluster to which each
unit belongs. These are typically countries in international
comparative data, or states within countries, or students within
faculties, etc. The model behind the colon is then estimated for each
cluster, and one of the coefficients is feed into the
unitregby plot.

depvar1 and varlist2 define the dependent and
independent variables for the unit level regression model
respectively. The model is estimated for each category of
varlist1.

depvar2 and defines the regression coefficient that defines
the slope of the regression lines shown in the
unitregby plot. depvar2 must refer to the estimated coefficient
of one of the independent variables of the unit level regression. This
is done by adding _b_ in front of variable name, or, in case of
factor variables, by adding _b_#_ with # referring to the
category of the respective variable. We suggest to use
twostep ... || mk2nd _all
for checking the implied coefficient names in case
of factor variables with interactions.

varlist3 is used to specify groups of cluster level
units. varlist3 thereby refer to cluster level variables, i.e.
the variables must be constant within each category of
varlist1. Moreover the variables must exist, either in the unit
level data set itself, or in the data set specified by using. By
default the variables of varlist3 are dichotomized at the median
to reduce the number of groups, but this can be changed considerably;
see options discrete(), nquantiles(), and unitby()
below.

using filename refers to a data set holding the
cluster level variables. In this data set, varlist1 must uniquely
identify the observations. If using is not specified, the
cluster level variables must be in the data set in memory.

unitregby options

allopts(options) define the look of the lines in the
background of the sub-graphs. By default, unitregby draws a
background graph that shows the regression lines for all clusters
units. This is meant to ease the comparison of each subgroup with the
other group. However, the background graph can be nuisance in some
situations, and is perhaps also a matter of taste. The background
options allows the user to fine tune the background graph, including a
complete removal. options can be any of the options listed in
line option. Specify allopts(lcolor(none)) to remove the
background graph.

byopts(options) is used to define the overall
arrangement of the unitregby plot. options can be any of the
options allowed for graph by. Among these options,
rows(#), cols(#), and compact may be particularly
useful. We stress that the overall titles, subtitle, notes, and
captions should be specified here.

discrete(varlist) turns of the default categorization
off the variables in the varlist. Assuming continuous cluster level
variables to be the standard case, unitregby dichotimizes all the
cluster level variables by default. This automatic grouping is turned
of for all variables in the varlist. Note that the option allows
arbitrary groupings of the cluster units by feeding custom-made
variables into discrete(). See option nquantiles() for
other means to change the default categorization of cluster level
variables.

regopts(options) defines the appearance of the regression
lines. options can be any of the line options. The line
options lcolor() and lwidth() may be particularly
useful.

nquantiles(#) defines the number of groups created from
each of the cluster level variables of varlist3. Assuming
continuous cluster level variables to be the standard case,
unitregby dichotimizes all the cluster level variables by
default. The option nquantiles(#) can be used to define the
number of groups to be created. Specifically, # is a number that
defines the number of quantiles to be used for the grouping of the
variables. nquantiles(4), for example, groups cluster level
variables into four groups by using the 1st, 2nd, and 3rd
quartile. See option disrete() for other means to control
the grouping.

unitby(varlist) is used to allow additional groupings
based on unit-level variables. In presence of unitby(), the
unit-level regression models are estimated separately for each
combination of the cluster level identifier and the variables
specified. The unitby-variables are then also used for the definition
of the various sub-graphs.

twoway options options allowed for graph twoway.

clustercpr examples

. use eqls_4x, clear

. twostep cntry: regress lsat hhinc i.sex || unitregby _b_hhinc hdirank

. twostep cntry: regress lsat hhinc i.sex || unitregby _b_hhinc hdirank corrupt

. twostep cntry: regress lsat hhinc i.sex || unitregby _b_hhinc hdirank, allopts(lcolor(none))

erases the background graph.

. twostep cntry: regress lsat hhinc age || unitregby _b_hhinc hdirank, nq(4) unitby(sex) byopts(cols(2)) ysize(8)

demonstrates some flexibility.

. gen oldeu = !mi(eu15)

. twostep cntry: regress lsat hhinc age || unitregby _b_hhinc hdirank oldeu, nq(4) discrete(oldeu) byopts(cols(2)) ysize(8)

shows an example for the usage of option discrete.

Acknowledgements

We wish to thank Kekeli Abbey, Lena Hipp, Armin Sauermann and for beta
testing. Ulrich Kohler wishes to thank the participants of summer's
2017 and winter's 2020/21 multilevel seminar for commenting earlier
versions of twostep.

Authors

Ulrich Kohler, University of Potsdam

email: ukohler@uni-potsdam.de

web: https://www.uni-potsdam.de/soziologie-methoden/

Johannes Giesecke, Humbold University Berlin

email: johannes.giesecke@hu-berlin.de"

web: https://www.sowi.hu-berlin.de/de/lehrbereiche/empisoz/a-z/giesecke

References

Achen, C., 2005. Two-Step Hierarchical
Estimation: Beyond Regression Analysis. Political Analysis, 13,
447-456.

Borjas, G.J. and G.T. Sueyoshi, G.T. (1994). A
two-stage estimator for probit models with structural group effects.
Journal of Econometrics, 1994, 64, 165-182

Bowers, J. and K. Drake, 2005. EDA for
HLM: Visualization when Probabilistic Inference Fails Political
Analysis, 13, 301-326.

Lewis, F.B. and D.A. Linzer, 2005. Estimating
Regression Model in Which the Dependent Variable is Based on
Estimates. Political Analysis, 2005, 13, 345-364