Linear Regression

Overview

Linear regression is a statistical modeling technique used to model the relationship between two variables by fitting a linear equation to observed data. This method is particularly useful for trend analysis and forecasting, allowing us to highlight trends in time series data and predict future data points. For example, linear regression can be used to predict future values based on the relationship between variables such as rainfall and umbrella sales.

Implementation

In the context of data wrangling with JavaScript, we can implement linear regression using the simple-statistics library. Below is a code snippet that demonstrates how to add a linearRegression function to a time series toolkit. This function computes a linear regression for a series and uses it to produce a forecast.

[Listing 9.13: Adding a Linear Regression Function to Our Time Series Toolkit](https://livebook.manning.com/book/data-wrangling-with-javascript/chapter-9/142)

const statistics = require('./statistics.js');
const average = statistics.average;
const std = statistics.std;
const simpleStatistics = require('simple-statistics');
const dataForge = require('data-forge');

// ... rollingAverage function omitted ...

function linearRegression (series, period, forecastIndexA, forecastIndexB) {    #1
    const regressionInput = series.toPairs();    #2
    const regression = simpleStatistics.linearRegression(regressionInput);    #3
    const forecaster = simpleStatistics.linearRegressionLine(regression);    #4

    return new dataForge.Series({
        values: [forecaster(forecastIndexA), forecaster(forecastIndexB)],
        index: [forecastIndexA, forecastIndexB],
    });
};

module.exports = {
    rollingAverage: rollingAverage,
    linearRegression: linearRegression,
};

#1   Computes a linear regression for a series and uses it to produce a forecast
#2   Extracts index/value pairs of data. These are the x and y values that we use as input to the linear regression.
#3   Creates the linear regression
#4   Creates a forecaster that can predict future values for us

This function takes a time series and computes a linear regression, which is then used to forecast future values. The simple-statistics library is used to perform the regression calculations, and data-forge is used to handle the series data.

Application Example

The following figure illustrates the application of linear regression to forecast the average temperature for New York City in the year 2100. This example demonstrates how linear regression can be used to make long-term predictions based on historical data.

Figure 9.12 Linear regression used to forecast the average temperature for NYC in 2100 Figure 9.12 Linear regression used to forecast the average temperature for NYC in 2100

By running the linearRegression function on a time series of NYC temperatures, we can generate a forecast that aligns with the trend depicted in Figure 9.12. This practical application showcases the power of linear regression in data analysis and forecasting.