help scatterfit

scatterfit Scatter plots with fit lines

Syntax

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

Fit
fit(str) Functional form of the fit line. May be linear, quadratic for a second polynomial, cubic for a third polynomial, lpoly for a local polynomial fit, or lowess for a lowess smoother.

fitmodel(model) Changes the regression model used to estimate the fit line. See the table below the options for a full list of supported models.

bwidth(num) Bandwidth for the local polynomial / lowess smoother fit line.

Confidence intervals and standard errors
ci Includes 95% confidence intervals.

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.

Bins
binned Divides xvar into equally sized bins based on quantile cutoff points and plots mean values of yvar and xvar within these bins.

nquantiles(num) Choose the number of equally sized bins / quantiles.

discrete Treats xvar as discrete and plots the means of yvar within each distinct value of xvar.

unibin(num) Divides xvar into num equally spaced bins and plots the means of yvar and xvar within them.

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

binmethod(str) Specifies how data points are aggregated within bins. May be mean (the standard) or median.

Subsample analysis
by(var) Draws separate scatterplots and fit lines for each value of var.

onefit Only plots a single fit line when by() is specified.

bymethod(str) Defines whether the results are derived from separate analyses of stratified samples (bymethod(stratify)) or a unified regression model containing interaction terms (bymethod(interact)).

Conditioning on covariates
controls(varlist) Adjusts the relationship between xvar and yvar linearly for continuous covariates. Achieved by residualization or, when binned is used, the method by Cattaneo et al. (2022).

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

Regression parameters
regparameters(str) Prints regression parameters into the plot. May contain coef, se, pval, sig (*<.1, **<.05, ***<.001), int (for bymethod(interact)), r2 / adjr2, nobs (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 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 x variable
xdistribution(string) Adds a kernel density plot of xvar. May be set to auto or a b, where a contains the position and b the height of the density.

xdistrbw(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.

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