10

データセットの線形または非線形最小二乗近似を実行できるRubyライブラリはありますか?

私がやりたいことは次のとおりです。

  • 一連の[x、y]データポイントが与えられた
  • そのデータに対して線形または非線形の最小二乗近似を生成します
  • ライブラリは、線形近似または非線形近似のどちらを実行する必要があるかを判断する必要はありません。ライブラリの呼び出し元は、必要な回帰のタイプを知っている必要があります

この機能を取得するためにC/C ++ / Javaライブラリを移植する必要がないようにしたいので、使用できる既存のRubyライブラリがあることを期待しています。

4

3 に答える 3

8

「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).
于 2012-04-29T01:02:22.997 に答える
6

このスニペットを使用して、いくつかの回帰を解決しました。最初のパラメーターはx座標を含む配列で、2番目のパラメーターはy座標を含む配列で、最後のパラメーターは探している多項式の次数です。これがあなたが探しているものであるかどうかはわかりませんが、それが役立つことを願っています。

于 2011-05-25T09:40:09.623 に答える
1

Ruby の swig ファイルに付属する非線形最小二乗最小化用の C ライブラリhttp://apps.jcns.fz-juelich.de/lmfitを維持しています。

于 2013-08-08T16:27:23.720 に答える