Simple Linear Regression
Learn how to fit and interpret a simple linear regression model.
StartKey Concepts
Review core concepts you need to learn to master this subject
Linear Regression using statsmodels
Prediction using a Simple Linear Model
Interpreting Regression Parameters
Linear Regression Assumptions
Linear Functional Form Assumption
Normality Assumption
Homoscedasticity Assumption
Fitted Values
Linear Regression using statsmodels
Linear Regression using statsmodels
import statsmodels.api as sm
model = sm.OLS.from_formula('height ~ weight', data = measurements)
results = model.fit()
print(results.summary())
Suppose we have a dataset named measurements
with columns height
and weight
. If we want to fit a model that can predict height based on weight, we would use the formula 'height ~ weight'
as shown in the example code.
- 1Linear regression is a powerful modeling technique that can be used to understand the relationship between a quantitative variable and one or more other variables, sometimes with the goal of making…
- 2Like the name implies, LINEar regression involves fitting a line to a set of data points. In order to fit a line, it’s helpful to understand the equation for a line, which is often written as *y=mx…
- 3In the last exercise, we tried to eye-ball what the best-fit line might look like. In order to actually choose a line, we need to come up with some criteria for what “best” actually means. Dependi…
- 4There are a number of Python libraries that can be used to fit a linear regression, but in this course, we will use the OLS.from_formula() function from statsmodels.api because it uses simple synta…
- 5Suppose that we have a dataset of heights and weights for 100 adults. We fit a linear regression and print the coefficients: model = sm.OLS.from_formula(‘weight ~ height’, data = body_measurements…
- 6Let’s again inspect the output for a regression that predicts weight based on height. The regression line looks something like this: ![plot of height vs. weight with a regression line drawn throu…
- 7There are a number of assumptions of simple linear regression, which are important to check if you are fitting a linear model. The first assumption is that the relationship between the outcome vari…
- 8Once we’ve calculated the fitted values and residuals for a model, we can check the normality and homoscedasticity assumptions of linear regression. ##### Normality assumption The normality ass…
- 9In the previous exercises, we used a quantitative predictor in our linear regression, but it’s important to note that we can also use categorical predictors. The simplest case of a categorical pred…
- 10Now that we’ve seen what a regression model with a binary predictor looks like visually, we can actually fit the model using statsmodels.api.OLS.from_formula(), the same way we did for a quantitati…
What you'll create
Portfolio projects that showcase your new skills
How you'll master it
Stress-test your knowledge with quizzes that help commit syntax to memory