Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 36 ยท Regression

Linear Regression with regress

Fit and read an ordinary least squares model with Stata's regress command: coefficients, standard errors, R-squared, predict and a runnable Python stand-in.

Regression is the workhorse of applied statistics, and in Stata it starts with one command: regress. You name the outcome variable first, then the predictors, and Stata prints a compact results table with coefficients, standard errors, t statistics, p-values and confidence intervals. This lesson shows how to run it, how to read each part of the output, and how to get fitted values and residuals afterwards.

Stata is commercial software and cannot run on this site. Commands are shown in read-only blocks and we never invent Stata output. The runnable examples use a small Python function that computes the same quantities regress reports, so you can see the arithmetic behind the table. Every stand-in is labelled.

The basic command

The general form is regress depvar indepvars [if] [in] [, options]. Using the teaching dataset that ships with Stata:

STATA
sysuse auto, clear
regress price mpg weight

This models price as a linear function of mpg and weight. Stata estimates the intercept (called _cons) automatically. Add noconstant to drop it, but only when theory demands it.

The output has three parts. The top-left block is an analysis-of-variance table (model, residual and total sums of squares with degrees of freedom and mean squares). The top-right block holds summary statistics: number of observations, the F test that all slopes are zero, R-squared, Adj R-squared and Root MSE. The bottom block is the coefficient table.

Reading the coefficient table

Each row is a predictor. The columns are:

Column Meaning
Coefficient Estimated change in the outcome for a one-unit change in the predictor, holding the others fixed
Std. err. Standard error of the estimate
t Coefficient divided by its standard error
P>t Two-sided p-value for the null hypothesis that the coefficient is zero
[95% conf. interval] Range of plausible values; change the level with level(90)

The phrase to remember is "holding the other variables constant". The coefficient on mpg is not the raw relationship between price and mileage; it is the relationship after weight has been accounted for.

A runnable stand-in

The block below builds simulated data and computes the same table with NumPy. It is a Python stand-in, not Stata: the variable names mimic a wage example and the numbers are simulated with a fixed seed.

Python OLS table by hand (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The true slopes in the simulation were 1.5 and 0.4. The estimates are close but not equal, and the confidence intervals usually contain the truth. That is exactly what an interval is for.

Goodness of fit

R-squared is the share of the outcome's variance explained by the model. Adj R-squared penalizes extra predictors, so it can fall when you add a useless variable. Root MSE is the typical size of a residual in the outcome's own units, which is often easier to explain to a reader than R-squared. A high R-squared does not prove the model is correct, and a low one does not make a coefficient meaningless.

Predictions and residuals

After any estimation command, predict creates new variables from the stored results:

STATA
regress price mpg weight
predict price_hat, xb        // fitted values
predict price_res, residuals // residuals
list make price price_hat price_res in 1/5

estat summarize shows the means of the variables used in the last model, and estimates store m1 saves the model so you can compare it with others later (see Regression tables).

Python Fitted values and residuals (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two facts the output demonstrates: OLS residuals sum to zero when the model has an intercept, and they are uncorrelated with every predictor. If you ever see otherwise in Stata after regress, suspect noconstant or a sample mismatch.

The estimation sample and stored results

Stata drops any observation with a missing value in any variable of the model. e(N) tells you how many observations it used, and e(sample) marks them. Many beginners compare two models fitted on different samples without noticing. A safe habit:

STATA
regress price mpg weight
gen byte insample = e(sample)
regress price mpg weight foreign if insample

Other useful stored results are e(r2), e(rmse), e(df_r) and the matrices e(b) and e(V). Print them all with ereturn list. You will use these when you write your own programs.

Common mistakes

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In regress price mpg weight, which variable is the outcome?

    The first variable after regress is always the dependent variable; the rest are predictors.
  2. 2. What does the coefficient on mpg represent?

    Multiple regression coefficients are partial effects: other predictors in the model are held constant.
  3. 3. Which command creates fitted values after regress?

    predict with the xb option stores the linear prediction; the residuals option stores residuals.
  4. 4. Why can two models in the same do-file be fitted on different samples?

    Listwise deletion removes observations missing any variable in the model, so adding a variable can shrink the sample.

Summary

  • regress y x1 x2 fits OLS and reports an ANOVA block, fit statistics and a coefficient table.
  • Coefficients are partial effects; read t, p-values and confidence intervals together.
  • predict gives fitted values and residuals; e(sample) marks the estimation sample.
  • Our Python blocks are stand-ins that reproduce the arithmetic, not Stata itself.

Next: Factor variables and interactions