Skip to content

Instantly share code, notes, and snippets.

@Mike-Honey
Last active October 20, 2024 21:12
Show Gist options
  • Save Mike-Honey/12fac1cf8a004452855f75e8f1edaa4d to your computer and use it in GitHub Desktop.
Save Mike-Honey/12fac1cf8a004452855f75e8f1edaa4d to your computer and use it in GitHub Desktop.
A Simple Linear Regression function for M / Power Query
// Found at https://www.bookkempt.com/2017/10/simple-linear-regression-in-power-query.html
// Expects a table with numeric columns x and y.
// To run over an entire table, first Group By nothing and Aggreagate All Rows.
// To run over subsets of a table, Group By columns defining your groups and Aggreagate All Rows.
// Then use Add Column / Invoke Custom Function and pass the All Rows column as the myTable parameter.
// Expand the resulting new column to get Slope, Intercept and RSquared
(myTable)=>
let
AddedXY = Table.AddColumn(myTable, "xy", each [x]*[y]),
AddedXX = Table.AddColumn(AddedXY, "xx", each [x]*[x]),
CountX = List.Count(AddedXX[x]),
SumX = List.Sum(AddedXX[x]),
SumY = List.Sum(AddedXX[y]),
SumXX = List.Sum(AddedXX[xx]),
SumXY = List.Sum(AddedXX[xy]),
Slope = ((CountX * SumXY) - (SumX * SumY)) / ((CountX * SumXX) - (SumX * SumX)),
Intercept = (SumY - (Slope * SumX)) / CountX,
PredictedY = Table.AddColumn(AddedXX, "PredictedY", each Slope * [x] + Intercept),
ErrorSquared = Table.AddColumn(PredictedY, "ErrorSquared", each Number.Power(([PredictedY]-[y]),2)),
DistanceSquared = Table.AddColumn(ErrorSquared, "DistanceSquared", each Number.Power(([y] - List.Average(AddedXX[y])),2)),
SumError = List.Sum(DistanceSquared[ErrorSquared]),
SumDistance = List.Sum(DistanceSquared[DistanceSquared]),
RSquared = 1 - (SumError / SumDistance)
in
[Slope = Slope, Intercept = Intercept, RSquared = RSquared]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment