Ruby Curve Fitting(对数回归)包

时间:2010-10-08 18:31:14

标签: ruby math statistics

我正在寻找一个Ruby宝石或库进行对数回归(曲线拟合到对数方程)。我已经尝试了statsample(http://ruby-statsample.rubyforge.org/),但它似乎没有我想要的东西。有人有什么建议吗?

3 个答案:

答案 0 :(得分:11)

尝试使用'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')

# Prints a statistical summary of the regression
print mlr.summary

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

# Bonus: The command below lists the methods contained in the instance variable, so that 
# you can get the R^2, SSE, coefficients, and t-values. I'll leave it commented out for now. 
# p mlr.methods

答案 1 :(得分:4)

我目前正在寻找类似的东西并遇到了answer

与Ruby交互的三个宝石:

Ruby中LR的另一个宝石:

我还没有尝试过任何东西,但我正在研究在Ruby中进行MLR的选择。

答案 2 :(得分:0)

尝试Rumale和Numo :: NArray https://github.com/yoshoku/rumale

enter image description here

  

Rumale(Ruby机器学习)是Ruby中的机器学习库。 Rumale为机器学习算法提供的接口类似于Python中的Scikit-Learn。 Rumale支持线性/内核支持向量机,逻辑回归,线性回归,岭,套索,分解机,朴素贝叶斯,决策树,AdaBoost,梯度树增强,随机森林,额外树,K最近邻分类器,K均值,K-Medoids,高斯混合模型,DBSCAN,功率迭代聚类,多维标度,t-SNE,主成分分析和非负矩阵分解。