from plotly.express import scatterfig = scatter(x=incomes, y=expectancies, height=350, title="Life Expectancy by Income", labels={"x": "Income", "y": "Life Expectancy (years)"}, trendline="ols", trendline_color_override="red")fig.show()
FYI
Under the hood, plotly uses the statsmodels package to calculate the trend, so you may have to install that package as well.
A linear trend assumes that there is a straight-line relationship between the independent and dependent variables. In the context of US GDP data, a linear trend suggests that GDP changes at a constant rate over time. When applying linear regression, the goal is to find the best-fit line that minimizes the residuals (differences between the predicted and actual values) under the assumption that the underlying relationship is linear.
For linear trends only, plotly provides access to the underlying regression results summary, to tell us more about how well the trend line fits the data:
from plotly.express import get_trendline_resultsresults = get_trendline_results(fig)print(results.px_fit_results.iloc[0].summary())
Linear regression is simple and interpretable but can be overly restrictive when the real-world data follows a more complex, non-linear pattern.
N.2 Non-linear Trends
In addition to the "ols" (Ordinary Least Squares) linear trend, we can use a "lowess" (Locally Weighted Scatterplot Smoothing) trend, which may be a better fit for non-linear relationships.
from plotly.express import scatterfig = scatter(x=incomes, y=expectancies, height=350, title="Life Expectancy by Income", labels={"x": "Income", "y": "Life Expectancy (years)"}, trendline="lowess", trendline_color_override="red")fig.show()
LOWESS is a non-parametric method that fits multiple local regressions to different segments of the data. Instead of assuming a global linear relationship, it captures local patterns by fitting simple models in small neighborhoods around each point. These local models are then combined to create a smooth curve that adjusts to non-linearities in the data. A LOWESS trend can adapt to sudden changes, curves, and other complex behaviors in the data, making it ideal for datasets where the relationship between variables changes over time.