Linear regression estimates a linear relationship between an outcome and one or more variables. For example, a business can ask how product weight and volume relate to shipping costs. The fitted relationship helps describe the data and can be used to make predictions, but it does not by itself establish why costs change. section 3.4.2
Start with a research question and an analysis plan: decide what possible results would mean for the business before fitting a model. Revisit the plan if data preparation reveals limitations or new questions. section 3.1 section 3.2
Weight, volume, and freight value
Join order items to products by product ID, calculate volume from length, height, and width, and remove rows missing the variables needed for analysis. Fit separate models with freight value as the outcome and weight or volume as the predictor. Then fit a third model using both predictors. A constant supplies the intercept in each model. section 3.4.2
Listing 3.5 Predicting product weight and volume influence on freight value
import pandas as pd import numpy as np import statsmodels.api as sm # Merge the DataFrames merged_df = df_order_items.merge(df_products, on="product_id", how="left") # Calculate product volume merged_df["product_volume"] = merged_df["product_length_cm"] * merged_df["product_height_cm"] * merged_df["product_width_cm"] # Filter out the relevant columns analysis_df = merged_df[ ["freight_value", "product_weight_g", "product_volume"]] # Remove rows with NaN values analysis_df = analysis_df.dropna() # Define the independent variables (weight and volume) and the dependent variable (freight value) X = analysis_df[['product_weight_g', 'product_volume']] y = analysis_df['freight_value'] # Add a constant for the intercept X = sm.add_constant(X) # Fit the model model = sm.OLS(y, X).fit() # Print the summary print(model.summary())
Follow the combined weight-and-volume model from data preparation through fitting with statsmodels. section 3.4.2
A scatterplot with a fitted line and confidence interval helps inspect each single-variable relationship. Confidence intervals communicate uncertainty; they are a reason to look beyond a single estimated relationship. section 3.4.2
Figure 3.11 Scatterplot of the relationship between (a) freight value and product weight, and (b) freight value and volume
Compare the plotted relationships between freight value and the two product attributes. section 3.4.2
Check the results rather than accepting a generated interpretation at face value. R² describes the share of outcome variability explained by the model; error measures such as MAE and RMSE describe prediction errors. In the example, R² is 0.37 for weight alone, 0.34 for volume alone, and 0.399 for the combined model. The combined fit leaves substantial variability unexplained, so avoid presenting these attributes as the whole shipping-cost story. section 3.4.2 section 4.6
The reported single-variable prediction metrics use the same analysis data used to fit the models; they do not demonstrate performance on a separate test set. Even a near-zero result can be useful if it challenges a business assumption. section 3.4.2 section 4.6






















