Ordinary Least Squares
Anonymous contributor
Published Dec 19, 2024
Contribute to Docs
Ordinary least squares (OLS) is a statistical method that reduces the sum of squared residuals to assess the correlation between independent and dependent variables. In linear regression, it is widely used to predict values and analyze correlations between variables.
Syntax
Here’s syntax to implement Ordinary Least Squares in Python:
import statsmodels.api as sm # Import the statsmodels library
# Add a constant to the independent variable(s) for the intercept
X = sm.add_constant(X) # Method to add a constant to X
# Fit the OLS model
model = sm.OLS(y, X).fit() # `OLS` function applied to y (dependent variable) and X (independent variables)
# Access the model summary
model.summary() # Method to get summary statistics
sm.add_constant(x)
: Adds an intercept (constant term) to the independent variables X.sm.OLS(y, X)
: Creates the OLS model with y as the dependent variable and X as the independent variables.model.summary()
: Displays the model’s results, including coefficients andR-squared
values.
Example
Here’s an example predicting test_scores
based on hours_studied
:
import statsmodels.api as smimport matplotlib.pyplot as pltimport numpy as np# Hours studied and corresponding test scoreshours_studied = [1, 2, 3, 4, 5]test_scores = [50, 55, 60, 65, 70]# Add a constant to the independent variablehours_with_constant = sm.add_constant(hours_studied)# Fit the OLS modelmodel = sm.OLS(test_scores, hours_with_constant).fit()# Display the summary of the modelprint(model.summary())# Predict the test scores using OLS modelpredicted_scores = model.predict(hours_with_constant)# Plot the data and lineplt.scatter(hours_studied, test_scores, color='blue', label='Observed data')plt.plot(hours_studied, predicted_scores, color='red', label='Fitted line')# Displaying the plotplt.xlabel('Hours Studied')plt.ylabel('Test Scores')plt.title('OLS Regression: Test Scores vs Hours Studied')plt.legend()# Show the plotplt.show()
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly54 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours