I mentioned in class that there was probably a way of adding a regression line to a Stata chart. Turns out that this is relatively straightforward, and is documented in the Stata on-screen help.
Using the lit_male x lit_fema graph from world95.dta file as an example, the basic method is to use the following commands
regress lit_male lit_fema
predict this_hat
graph lit_male this_hat lit_fema , connect(.l) symbol(Oi) sort
The first command runs the bivariate regression
lit_male = a + b*lit_fema
The second command computes the predicted values of lit_male based on this regression, and puts those results in a new variable called this_hat. The graph command plots both the original scatterplot (lit_male x lit_fema) and the regression line (this_hat x lit_fema) on a single graph. The connect and symbol options specify how the scatterplot will be displayed: lit_male is not connected (.) and has a circle (O) as the symbol; this_hat is connected with a line and has no symbol.
The end result looks like this:
It would be nice to be able to do this with a single command. This can be done by constructing a Stata "do" file, which is a text file containing a series of commands. A general-purpose do file with this problem is
| regress `1' `2' | /* regression on two variables */ |
| predict this_hat | /* put predictions in a temporary variable */ |
| graph `1' this_hat `2' , c(.l) s(Oi) sort | /* plot the graph */ |
| drop this_hat | /* get rid of the temporary variable */ |
(The information inside the /* ... */ are comments that are ignored by Stata)
This do file takes two variable names -- these are put in `1' and `2' -- and creates a graph in a single command. Save the commands in a file, for example regrchart.do and then run it using the command
do regrchart.do lit_male lit_fema
or
do regrchart.do lifeexpm lifeexpf
The second command creates a chart for lifeexpm x lifeexpf.