“Linear Regression with Two-Way Interactions Using JavaScript” in Visual Studio Magazine

I wrote an article titled “Linear Regression with Two-Way Interactions Using JavaScript” in the October 2025 edition of Microsoft Visual Studio Magazine. See https://visualstudiomagazine.com/articles/2025/10/01/linear-regression-with-two-way-interactions-using-javascript.aspx.

The goal of a machine learning regression problem is to predict a single numeric value. The form of a basic linear regression prediction model without interactions is y’ = (w0 * x0) + (w1 * x1) + . . . + (wn * xn) + b, where y’ is the predicted value, the xi are predictor values, the wi are weights (also called coefficients), and b is the bias (also called the intercept).

The form of a linear regression model with two-way interactions is y’ = (w0 * x0) + . . . + (wn * xn) + (w01 * x0 * x1) + (w02 * x0 * x2) + . . . + b. The interaction terms are the multiplication products of all combinations of pairs of the predictor variables.

Compared to basic linear regression, linear regression with two-way interactions can handle more complex data.

I presented a complete demo program, written using JavaScript for execution by the node.js runtime. The output of the demo is:

: node linear_regression_interactions.js

Begin linear regression with two-way interactions 
using node.js JavaScript

Loading train (200) and test (40) from file

First three train X:
 -0.1660   0.4406  -0.9998  -0.3953  -0.7065
  0.0776  -0.1616   0.3704  -0.5911   0.7562
 -0.9452   0.3409  -0.1654   0.1174  -0.7192

First three train y:
   0.4840
   0.1568
   0.8054

Creating and training model

Setting SGD lrnRate = 0.001
Setting SGD maxEpochs = 100
epoch =      0  MSE = 0.1116  acc = 0.0000
epoch =     20  MSE = 0.0033  acc = 0.4400
epoch =     40  MSE = 0.0012  acc = 0.5950
epoch =     60  MSE = 0.0009  acc = 0.6750
epoch =     80  MSE = 0.0008  acc = 0.7000
Done

Model base weights:
 -0.2625   0.0341  -0.0460   0.0324  -0.1151
Model bias: 0.3619

Model interaction weights:
  0.0000   0.0000   0.0000   0.0000   0.0000
 -0.0008   0.0000   0.0000   0.0000   0.0000
  0.0313   0.0107   0.0000   0.0000   0.0000
  0.0169  -0.0104   0.0013   0.0000   0.0000
  0.0952   0.0315  -0.0445   0.0003   0.0000

Computing model accuracy

Train acc (within 0.15) = 0.8350
Test acc (within 0.15) = 0.8000

Train MSE = 0.0008
Test MSE = 0.0006

Predicting for x =
  -0.1660    0.4406   -0.9998   -0.3953   -0.7065
Predicted y = 0.5090

End demo

The linear model with two-way interactions scored significantly better accuracy than a linear model without interactions.

Linear regression with two-way interactions has a nice balance of prediction power and interpretability. The model weights/coefficients are easy to interpret. If the predictor values have been normalized to the same scale, larger magnitudes mean larger effect, and the sign of the weights indicate the direction of the effect.

Linear regression with two-way interactions is not always effective — if it were, it would have replaced basic linear regression. Put another way, linear regression with two-way interactions can sometimes provide a big improvement in model quality for a relatively small investment in effort, and so it’s usually worth exploring.



There are many terms in machine learning that are similar but refer to completely different things. For example, linear regression and logistic regression are entirely different techniques.

Left: “Battle Beyond the Stars” (1980) – A group of seven space mercenaries is recruited by a young man to defend his planet his planet from the evil Sador. My grade = B- but only that high because of Saint-Exmin’s costume.

Center: “Battle in Outer Space” (1959) – The planet Nadal attempts to conquer Earth using advanced weapons. Things look bad for Earth but scientists develop an atomic heat cannon just in time.

Right: “Battle Beyond the Sun” (1959) – Two nations race to be the first to reach Mars. Neither team makes it, but both return safely. This is a U.S. adaptation of a Soviet film. My grade = C.


This entry was posted in JavaScript, Machine Learning. Bookmark the permalink.

Leave a Reply