Stata 15 help for twoway lfit

[G-2] graph twoway lfit -- Twoway linear prediction plots


twoway lfit yvar xvar [if] [in] [weight] [, options]

options Description ------------------------------------------------------------------------- range(# #) range over which predictions calculated n(#) number of prediction points atobs calculate predictions at xvar estopts(regress_options) options for regress predopts(predict_options) options for predict

cline_options change look of predicted line

axis_choice_options associate plot with alternative axis

twoway_options titles, legends, axes, added lines and text, by, regions, name, aspect ratio, etc. ------------------------------------------------------------------------- All options are rightmost; see repeated options. yvar and xvar may contain time-series operators; see tsvarlist. aweights, fweights, and pweights are allowed. Weights, if specified, affect estimation but not how the weighted results are plotted. See weight.


Graphics > Twoway graph (scatter, line, etc.)


twoway lfit calculates the prediction for yvar from a linear regression of yvar on xvar and plots the resulting line.


range(# #) specifies the x range over which predictions are to be calculated. The default is range(. .), meaning the minimum and maximum values of xvar. range(0 10) would make the range 0 to 10, range(. 10) would make the range the minimum to 10, and range(0 .) would make the range 0 to the maximum.

n(#) specifies the number of points at which predictions over range() are to be calculated. The default is n(3).

atobs is an alternative to n(). It specifies that the predictions be calculated at the xvar values. atobs is the default if predopts() is specified and any statistic other than the xb is requested.

estopts(regress_options) specifies options to be passed along to regress to estimate the linear regression from which the line will be predicted; see [R] regress. If this option is specified, estopts(nocons) is also often specified.

predopts(predict_options) specifies options to be passed along to predict to obtain the predictions after estimation by regress; see [R] regress postestimation.

cline_options specify how the prediction line is rendered; see [G-3] cline_options.

axis_choice_options associate the plot with a particular y or x axis on the graph; see [G-3] axis_choice_options.

twoway_options are a set of common options supported by all twoway graphs. These options allow you to title graphs, name graphs, control axes and legends, add lines and text, set aspect ratios, create graphs over by() groups, and change some advanced settings. See [G-3] twoway_options.


Remarks are presented under the following headings:

Typical use Cautions Use with by()

Typical use

twoway lfit is nearly always used in conjunction with other twoway plottypes, such as

. sysuse auto

. scatter mpg weight || lfit mpg weight (click to run)

Results are visually the same as typing

. regress mpg weight . predict fitted . scatter mpg weight || line fitted weight


Do not use twoway lfit when specifying the axis_scale_options yscale(log) or xscale(log) to create log scales. Typing

. scatter mpg weight, xscale(log) || lfit mpg weight (click to run)

The line is not straight because the regression estimated for the prediction was for mpg on weight, not mpg on log(weight). (The default for n() is 3 so that, if you make this mistake, you will spot it.)

Use with by()

lfit may be used with by() (as can all the twoway plot commands):

. scatter mpg weight || lfit mpg weight ||, by(foreign, total row(1)) (click to run)

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