Key Concepts

Review core concepts you need to learn to master this subject

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.

Introduction to Linear Regression
Lesson 1 of 1
  1. 1
    Linear 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…
  2. 2
    Like 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…
  3. 3
    In 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…
  4. 4
    There 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…
  5. 5
    Suppose 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…
  6. 6
    Let’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…
  7. 7
    There 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…
  8. 8
    Once 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…
  9. 9
    In 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…
  10. 10
    Now 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…
  11. 11
    Congratulations! As a recap, you’ve learned to: * Fit a simple OLS linear regression model * Use both quantitative and binary categorical predictors * Interpret the coefficients of a regression mo…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo