Title

scatterfit Scatter plots with fit lines that visualize the effect of xvar on yvar at different values of zvar

Syntax

scatterfit
yvar xvar zvar [if] [in]
[weight] , [options]

Overall slope
fit(str) Specifies the functional form of the fit line that plots the overall interaction model results. May be linear or quadratic for a second polynomial.

Individual slopes
indslopes(str) Determines for what values of zvar individual slopes of xvar are plotted. May be quantiles to divide z into quantiles, unibin for uniformly spaced bins, or discrete.

binvar(varlist) Plots the effects of xvar for each within each distinct value of varlist. Used when the bins are already defined in the dataset.

indslopesmethod(str) Determines how the individual slopes are computed. May be interact for a unified regression models including interactions or stratify for separate regressions.

Confidence intervals and standard errors
ci Includes 95% confidence intervals on the overall fitted slope.

indslopesci Includes 95% confidence intervals for the individual slopes.

vce(string) Specifies the estimated standard errors, which is relevant for confidence intervals and p-values. Supports all possible vce() options of the respective regression model as well as vce(wbcluster clustervar) for wild cluster bootstrap.

level(num) Changes the confidence level of the CIs.

Regression model
fitmodel(model) Changes the regression model used to estimate the individual and overall slopes. See the table below the options for a full list of supported models.

Conditioning on covariates
controls(varlist) Adjusts the relationship between xvar and yvar linearly for continuous covariates. Achieved by residualization.

fcontrols(varlist) Controls for factor variables, i.e. categorical variables such as gender or region.

Regression parameters
regparameters(str) Prints parameters into the plot. May contain coef for information on how the slope of xvar changes when zvar increases by one unit, pval for the associated p-value, sig for significance (*<.1, **<.05, ***<.001), and nobs for N.

parpos(numlist) Overrides the position of the parameters within the plot. For example, parpos(0 5) choses 0 as the y-coordinate and 5 as the x-coordinate.

Scatter points
mweighted(num) Adjusts the marker size depending on the number of observations at distinct values of yvar and xvar. Useful with discrete and unibin().

mlabel(var) Uses the strings / value labels stored in var as scatter markers instead of the usual circles, diamonds, etc.

jitter(num) Randomly varies the location of scatter points.

Distribution of z variable
zdistribution(string) Adds a kernel density plot of zvar. May be set to auto or a b, where a contains the position and b the height of the density.

zdistrbw(num) Specifies the bandwidth of the kernel density estimator.

Scheme and colors
plotscheme(str) Defines an alternative graph scheme, such as plotscheme(white_tableau). See the collection in https://github.com/asjadnaqvi/stata-schemepack.

colorscheme(str) Defines a custom color palette (e.g. colorscheme(tableau)). To define your own colors, use a list of hex colors (e.g. colorscheme(#E6007E #009FE3 #312783)).

cintensity(num) Changes the color intensity. Higher values make colors darker.

Plot and element size
scale(num) Resizes all elements in the plot. For example, scale(1.1) increases text, marker, and lines sizes by 10%.

msize(num) Resizes the scatter markers.

parsize(num) Resizes the printed regression parameters.

legsize(num) Resizes the legend.

xysize(num) Specifies the relative width to height. For example, xysize(1.2) draws a plot with 20% more width than height.

Other
standardize Standardizes xvar and yvar to a standard deviation of one and a mean of zero.

leginside Places the legend inside the plot region.

noyline Removes the line at y=0.

slow Uses (slower) native Stata commands instead of packages such as Gtools.

opts(str) Passes on options to the twoway plot. For example, opts(xtitle("Example")) changes the x axis title. See help twoway.

Supported regression models

model Description

ols Linear regression estimated via ordinary least squares. The standard for continuous DVs.

poisson Poisson regression for count data

quantile Quantile regression. Standard setting is a regression at the 50th percentile. For other quantiles, specify fitmodel(quantile, p(x)), where x is the respective quantile between 1 and 99.

randomint Multilevel model with random intercepts. The higher-level cluster(s) must be specified with the following syntax: fitmodel(randomint, cluster(x)), where x is the list of cluster variables.

randomint, reml Estimates the model with REML and DF adjustion for unbiased estimation of SEs with few clusters (see Elff et al. in BJPS). Use the following syntax: fitmodel(randomint, reml cluster(x))

flogit / fprobit Fractional logit/probit model for 0-1 DVs

logit / probit Logistical regression with the logit or probit link function. logit it the standard for binary DVs.

lpm Linear probability model for binary DVs, estimated with OLS

Examples

A detailed tutorial with examples is available at https://github.com/leojahrens/scatterfit

. sysuse auto

. slopefit weight length turn

. slopefit weight length turn, ci

. slopefit weight length turn, ci method(discrete)