Stata For Beginners: How to run a simple linear regression in Stata

✦ ✦ ✦
Descriptive alt text here

What is a linear regression?

A linear regression is used to predict the dependent variable using one or more independent variables.

Basically, we are seeing if the IV can predict changes in the DV.

This is different to correlation which just tells us whether there’s any relationship.

But…how does regression predict values?? Through the magical regression line. It’s not magical. I’m so tired right now – full uni day.

Anyway, cast your mind back to high school math. Do you remember this equation?

Regression Equation
Y = mx + b
Y = outcome (DV)
X = predictor (IV)
m = slope (change in Y per 1 unit of X)
b = intercept (Y when X = 0)
Regression basically uses this equation to draw the line of best fit through your data and make predictions.

Yup. That’s the equation for a line! Remember how you could use this line to predict patterns like for every one increase in x, y will increase by a certain amount of units? This is exactly the model that regression works off.

When we plot all our x and y values with a line of best fit, we can begin to see potential patterns.

Great, but how do you actually do this in stata?

Ironically, running regression in stata is a piece of cake. The command is literally just: regress DV IV

The tricky part is all the assumption tests, because simple linear regressions have 4 assumptions they must meet.

Assumptions of Simple Linear Regression

  1. Independence of observations (All data points should be unrelated to each other.)
  2. Normal distribution of residuals (Errors should form a bell curve shape.)
  3. Homoscedasticity (Spread of errors should be consistent across all values.)
  4. Linearity (Relationship between X and Y should be roughly a straight line)

So let’s get started with the actual Stata stuff…

Checking Assumptions in Stata

Here are all the commands you will need to run your assumption checks for simple linear regression:

AssumptionStata CommandWhat you’re looking for (GOOD)What’s BAD
Linearitytwoway (scatter DV IV) (lfit DV IV)Points follow a roughly straight-line trendCurved pattern (U-shape, exponential)
Homoscedasticity (constant variance)rvfplot, yline(0)Random cloud, even spread across plotFunnel shape, widening/narrowing spread
Normality of residuals (visual)histogram residuals, normalBell-shaped distributionSkewed, heavy tails
Normality of residuals (plot)pnorm residuals or qnorm residualsPoints close to straight lineStrong curve or deviation
Normality of residuals (test)swilk residualsp > .05 (normal enough)p < .05 (not normal)
Independence of observationsestat dwatson (optional)Value ~ 2 (no autocorrelation)Far from 2 (autocorrelation)
No multicollinearity (only multiple regression)vifVIF < 5 (safe), < 10 acceptableVIF > 10 (problematic)

Here’s an example of what a set of met/good assumption checks would look like:

Homoscedasticity & linearity

Command used: rvfplot, yline(0)

Interpretation: You can see the graph below doesn’t show any weird fanning patterns (skinny or wide on one end). It looks like a nice even-ish spread.

Normality of residuals

Command used:histogram residuals, normal

Interpretation: Looks like a pretty good bell shape

Command used: pnorm residuals

Interpretation: We can see the points lie nicely around the line, so that’s good.

Command used: swilk residuals

Interpretation: This is just a final, trusty Shapiro Wilk test for normality. We can see the p value is > 0.05, so we fail to reject the null hypothesis and assume normality. Yay.

Running the Regression

Good news, the hard part is over. Now all you do is run this command:

regress DV IV

And you will get something that looks a bit like this:

Interpreting Results:

I’ve circled the most important bits of info you need.

The Prob > F = the p value. This tells you whether the test is statistically significant.

R-squared = your effect size.

_cons = the coefficient (y value when x=0)

P> t = p values for each IV

The coefficient values of your IV are like the slope of the line.

But what does it all mean?

Putting it together, we can see that there is a statistically significant positive relationship between the hours of research a student did and their project rating (p = 0.017). However, the model explains about 11.23% of the variability in project scores (R² = 0.112), suggesting a small effect size. We can also see that for every increase of 1 hour of research, the project’s score is predicted to rise by 1.36 points. Additionally, when no research hours are completed, the predicted project rating is 44.50.

And that’s all!

Hi, I’m Daisy!

I created The Psych Diaries to make studying psychology a little less overwhelming. Here you’ll find study guides, Stata tutorials, psychology resources, and everything I’m learning along the way!

About Me →

Popular Guides


🤍 Thanks for stopping by. I hope you find something here that helps!

Discover more from The Psych Diaries

Subscribe now to keep reading and get access to the full archive.

Continue reading