regress
Explains a number using other columns, by fitting a straight line.
What we have
- factor notation:
i.,c.,#and## awandpwweightsrobust, checked against R's HC1e()results after the model
Coming later
vce(cluster ...), which is refused rather than quietly ignored- frequency weights (
fw), which are refused predict,test,lincomandmarginsafterwards
You may shorten it to reg.
How you write it
regress depvar indepvars [if] [in] [weight]
Square brackets mean optional. A vertical bar means choose one.
What it does
regress mpce age education asks: as age and education change, how does spending change? The answer comes back as one number per column (a coefficient), each meaning "the change in spending associated with a one-unit rise in this, holding the others fixed".
Beside each coefficient is its margin of error (standard error and confidence interval) and a p-value saying how likely a result this far from zero would be if the column really had no effect. The R-squared at the top is the share of the variation in spending the model accounts for.
Categories go in with i. in front: i.sector fits a separate effect for each sector, measured against whichever one comes first (the base level). c.age forces a column to be treated as a number, and i.a#i.b lets two categories interact.
Try it
Every example below was run after sysuse vira.sample02, clear.
. regress mpce age education i.sector
Linear regression Number of obs = 6,000
F statistic = 423.40
Prob > F = 0.0000
R-squared = 0.1748
Root MSE = 1348.945
------------------------------------------------------------------------------
mpce | Coefficient Std. err. t P>|t| [95% conf. interval]
-------------+----------------------------------------------------------------
sector=Urban | 944.3234 36.88319 25.60 0.000 872.0191 1016.628
age | 6.209437 .9287289 6.69 0.000 4.388795 8.03008
education | 90.83084 3.779603 24.03 0.000 83.42146 98.24022
_cons | 2425.591 57.07151 42.50 0.000 2313.711 2537.472
------------------------------------------------------------------------------
The same thing in pandas and in R
pandas
import statsmodels.formula.api as smf
model = smf.ols("mpce ~ age + education + C(sector)", data=df).fit()
R
model <- lm(mpce ~ age + education + factor(sector), data = df) summary(model) # This R was run in a real R (4.6.1) and gave the same numbers.