How to get predicted value from Stata

(Comments)

How to Obtain Predicted Values and Residuals in Stata


Linear regression is a method we can use to understand the relationship between one or more explanatory variables and a response variable.

When we perform linear regression on a dataset, we end up with a regression equation which can be used to predict the values of a response variable, given the values for the explanatory variables.

We can then measure the difference between the predicted values and the actual values to come up with the residuals for each prediction. This helps us get an idea of how well our regression model is able to predict the response values.

This tutorial explains how to obtain both the predicted values and the residuals for a regression model in Stata.

Example: How to Obtain Predicted Values and Residuals

For this example we will use the built-in Stata dataset called auto. We’ll use mpg and displacement as the explanatory variables and price as the response variable.

Use the following steps to perform linear regression and subsequently obtain the predicted values and residuals for the regression model.

Step 1: Load and view the data.

First, we’ll load the data using the following command:

sysuse auto

Next, we’ll get a quick summary of the data using the following command:

summarize

Summarize command in Stata

Step 2: Fit the regression model.

Next, we’ll use the following command to fit the regression model:

regress price mpg displacement

Regression model output in Stata

The estimated regression equation is as follows:

estimated price = 6672.766 -121.1833*(mpg) + 10.50885*(displacement)

Step 3: Obtain the predicted values.

We can obtain the predicted values by using the predict command and storing these values in a variable named whatever we’d like. In this case, we’ll use the name pred_price:

predict pred_price

We can view the actual prices and the predicted prices side-by-side using the list command. There are 74 total predicted values, but we’ll view just the first 10 by using the in 1/10 command:

list price pred_price in 1/10

Actual vs. predicted values in regression in Stata

Step 4: Obtain the residuals.

We can obtain the residuals of each prediction by using the residuals command and storing these values in a variable named whatever we’d like. In this case, we’ll use the name resid_price:

predict resid_price, residuals

We can view the actual price, the predicted price, and the residuals all side-by-side using the list command again:

list price pred_price resid_price in 1/10

residuals vs predicted values in Stata

Step 5: Create a predicted values vs. residuals plot.

Lastly, we can created a scatterplot to visualize the relationship between the predicted values and the residuals:

scatter resid_price pred_price

Residuals vs. predicted values plot in Stata

We can see that, on average, the residuals tend to grow larger as the fitted values grow larger. This could be a sign of heteroscedasticity – when the spread of the residuals is not constant at every response level.

We could formally test for heteroscedasticity using the Breusch-Pagan Test and we could address this problem using robust standard errors.

Currently unrated

Comments

Riddles

22nd Jul- 2020, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20 Oct- 2019, by: Editor in Chief
524 Shares 4 Comments
Generic placeholder image
20Aug- 2019, by: Editor in Chief
524 Shares 4 Comments
10Aug- 2019, by: Editor in Chief
424 Shares 4 Comments
Generic placeholder image
10Aug- 2015, by: Editor in Chief
424 Shares 4 Comments

More News  »

Why being minimalist good for your money

Recent news

Why being the minimalist is good for your money

read more
2 weeks, 6 days ago

a story which relates the events of your life from past, present and future to the life cycle of a star

Recent news

What will ChatGPT answer if thre is a question? Write a story which relates the events of your life from past, present and future to the life cycle of a star. Use also those elements which could symbolize something or anything in your story.​

read more
1 month ago

Still about chatGPT and how it can pass the school exam

Recent news

ChatGPT passes exams from law and business schools

ChatGPT is trained on vast amounts of online data in order to generate responses to user prompts. While it has gained traction among users, it has also raised some concerns about inaccuracies and its potential to perpetuate biases and spread misinformation.

read more
1 month, 3 weeks ago

How to get predicted value from Stata

Recent news

How to Obtain Predicted Values and Residuals in Stata


Linear regression is a method we can use to understand the relationship between one or more explanatory variables and a response variable.

read more
1 month, 3 weeks ago

List of actor and actrees that left hollywood for the daily job

Recent news

These Former Stars Quit Hollywood for Regular Day Jobs

Stacey Dash ©Eugene Powers/Shutterstock.comStacey Dash ©Eugene Powers/Shutterstock.com

While some celebrities enjoy fame and fortune, others prefer to take the cash and then return to their normal lives, or pursue a different dream. Whether this departure from the limelight is temporary or for good, these former A-listers are now working day jobs that more closely resemble our own.

read more
1 month, 3 weeks ago

How do I remove a specific value from an array?

Recent news

How do I remove a specific value from an array? Something like:

read more
1 month, 3 weeks ago

Meet the real figure of Viking season 2 Valhalla

Recent news

Meet the real historical figures of Vikings Valhalla season 2

The historical drama is back with some new faces from the Viking Age.

The series was inspired by mythology and Viking sagas and is set 100 years after the events of the original Amazon Prime Video series.

read more
1 month, 4 weeks ago

What's the difference between gen and egen in Stata

Recent news

Is there a reason why there are two different commands to generate a new variable?

read more
2 months ago

More News »

Generic placeholder image

Collaboratively administrate empowered markets via plug-and-play networks. Dynamically procrastinate B2C users after installed base benefits. Dramatically visualize customer directed convergence without