2

Motivation

I'm using TeXmacs to write math assignments and Maxima as CAS program. I recently found out how to use plot2d in gnuplot via Maxima.

Questions

  1. I now need to plot a linear regression line using some coordinates / table data.
  2. And it would be nice to be able to get the linear regression function f(x) = mx+b or f(x) = a+bx from the same data. Nice to have - not required.
  3. I also need to draw a residual plot from the same data. Perhaps getting the r^2 value.

Researched

I have read https://stackoverflow.com/a/9077851/205696 which answers how to draw a regression line in gnuplot but I do not know how to port that to the plot2d function in Maxima.

I also read this Q/A that explains how to calculate the equation for linear regression with Maxima but I am frankly not good enough in math to understand what is actually going on.

I found out that I can plot linear regressions with GeoGebra, but I do not see a way to draw a residual plot or to get f(x) = mx+b on the data I have provided (I can calculate f(x) = ax+b from observation but it would be nice to get exact numbers).

Explanatory images

Data table and linear regression line in TI Nspire Data table and linear regression line in TI Nspire.

Data table, linear regression line and residual plot in TI Nspire Data table, linear regression line and residual plot in TI Nspire.

Statistical calculations -> Linear regression in TI Nspire Statistical calculations -> Linear regression in TI Nspire.

dotnetCarpenter
  • 10,019
  • 6
  • 32
  • 54

1 Answers1

2

Here is a sketch of a solution. There is a function linear_regression in the stats package which does most of the work.

Let x and y be two lists, which contain your data. linear_regression expects a matrix of two or more columns. To paste the lists together into a matrix, you can say: xy: addcol(matrix(), x, y); (There are other ways to do it.)

To get an object which contains the coefficients and residuals and other items, you can say: results: linear_regression(xy);

To get the residuals: myresiduals: take_inference('residuals, results);

To get the coefficients: coeffs: take_inference('b_estimation, results);

Now with the coefficients, you can construct a function to represent the regression line: myline: subst(['b = coeffs[1], 'm = coeffs[2]], lambda([x], m*x + b));

(Be sure to check the coefficients -- I might have gotten them reversed. Maybe it's coeffs[2] and then coeffs[1]? You should check.)

At this point you have x, y, residuals, and myline. Plot them all together:

plot2d ([[discrete, x, y], [discrete, x, residuals], myline], ['x, lmin(x) - 1, lmax(x) + 1]);

To get more info, see: ? linear_regression

Robert Dodier
  • 16,905
  • 2
  • 31
  • 48
  • Thanks! Still trying to trying to get this to work. I'm wondering if there is also a `exponential_regression` function? I can't find it in [the list of Maxima functions](https://maxima.sourceforge.io/docs/manual/maxima_377.html#Function-and-Variable-Index) – dotnetCarpenter Mar 14 '23 at 04:57
  • `nth_root` is also very welcome, so I can calculate the constants when two points are known. – dotnetCarpenter Mar 14 '23 at 05:02
  • Nevermind about `nth_root` - I found a working implementation https://rosettacode.org/wiki/Nth_root#Maxima – dotnetCarpenter Mar 14 '23 at 05:05
  • 1
    There isn't an exponential regression function, but I think you can get the same effect by taking the logarithm of the data (dunno whether you want log(x) and log(y), or only one or the other) and then applying linear regression. About nth root, `sqrt(x)` is the square root, and `x^(1/n)` is the nth root. – Robert Dodier Mar 14 '23 at 16:38
  • hehe I finally got it! I did not know that I had to `load (stats);` first. I thought it was built-in when I saw the documentation. I actually got that from chatGPT - the only thing it got right :) – dotnetCarpenter Mar 14 '23 at 18:53