2011-05-25 107 views
8

是否有Ruby库允许我对一组数据进行线性或非线性最小二乘逼近。用于线性或非线性最小二乘逼近的Ruby库?

我想什么做的是以下几点:

  • 鉴于一系列[X,Y]数据点的
  • 产生对数据的线性或非线性最小二乘逼近
  • 的库不必知道它是否需要进行线性或非线性逼近。图书馆的来电者应该知道他们需要什么类型的回归

我不想尝试移植一些C/C++/Java库来获得这个功能,所以我希望有一些现有的我可以使用的Ruby lib。

+0

你有没有尝试阅读http://stackoverflow.com/questions/703717/anything-like-scipy-in-ruby和http://stackoverflow.com/questions/5416655/ruby-mathematic-gem和http:/ /stackoverflow.com/questions/4775013/scientific-programming-with-ruby? – 2011-05-25 03:03:38

+0

我做了,我通读了这些库,只有linalg库暗示它可以做最小二乘法,但是当我通过源挖掘时找不到实现。 – 2011-05-25 17:57:19

+0

在你的问题中提到这可能是个好主意。 – 2011-05-25 23:17:52

回答

5

我用this snippet来计算一些回归。第一个参数是一个包含x坐标的数组,第二个数组包含y坐标,最后一个是您正在寻找的多项式的次数。不知道这是你在找什么,但希望它有帮助。

7

尝试使用'statsample'宝石。您可以使用下面提供的示例执行对数,指数,权力或任何其他转换。我希望这有帮助。

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).