10

Is there a Ruby library that allows me to do either linear or non-linear least squares approximation of a set of data.

What I would like to do is the following:

  • Given a series of [x,y] data points
  • Generate a linear or non linear least squares approximation against that data
  • The library doesn't have to figure out if it needs to do a linear or non linear approximation. The caller of the library should know what type of regression they need

I'd prefer not to have to try to port some C/C++/Java library to get this functionality so I'm hoping there is some existing Ruby lib that I can use.

Peter C
  • 101
  • 1
  • 4
  • Did you try reading http://stackoverflow.com/questions/703717/anything-like-scipy-in-ruby and http://stackoverflow.com/questions/5416655/ruby-mathematic-gem and http://stackoverflow.com/questions/4775013/scientific-programming-with-ruby ? – Andrew Grimm May 25 '11 at 03:03
  • I did and I read through those libraries and only the linalg library implies that it can do least squares approx but when I dug through the source I couldn't find an implementation. – Peter C May 25 '11 at 17:57
  • It might have been a good idea to mention that in your question. – Andrew Grimm May 25 '11 at 23:17

3 Answers3

8

Try using the 'statsample' gem. You can perform logarithmic, exponential, power, or any other transformation using the example that is provided below. I hope this helps.

require 'statsample'

# Independent Variable
x_data = [Math.exp(1), Math.exp(2), Math.exp(3), Math.exp(4), Math.exp(5)]

# Dependent Variable
y_data = [3, 5, 7, 9, 11]

# Logarithmic Transformation of X data 
# Math.log in Ruby has the base of Euler's number 'e' ~= '2.71828', 
# instead of the base '10'. Just a note.
log_x_data = x_data.map { |x| Math.log(x) }

# Linear Regression using the Logarithmic Transformation
x_vector=log_x_data.to_vector(:scale)
y_vector=y_data.to_vector(:scale)
ds={'x'=>x_vector,'y'=>y_vector}.to_dataset
mlr=Statsample::Regression.multiple(ds,'y')
mlr.summary

# Provides the value of the y-intercept 
#p mlr.constant

# Lists the coefficients of each casual variable. In this case, we have only one--'x'.
#p mlr.coeffs

# The regression output produces the line y = 1 + 2*x, but 
# considering that we transformed x earlier, it really produces
# y = 1 + 2*ln(x).
dpott197
  • 1,060
  • 1
  • 9
  • 11
6

I used this snippet to work out some regressions. The first parameter is an array containing the x coordinates, the second an array containing the y coordinates and the last is the degree of the polynomial you are looking for. Not sure if it is this what you are looking for, but hopes it helps.

Serabe
  • 3,834
  • 19
  • 24
1

I am maintaining a C library for non-linear least squares minimization, http://apps.jcns.fz-juelich.de/lmfit, that comes with swig files for Ruby.

Joachim W
  • 7,290
  • 5
  • 31
  • 59