Ruby 曲线拟合(对数回归)包

标签 ruby math statistics

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

最佳答案

尝试使用“statsample”gem。您可以使用类似的方法执行指数、对数、幂、正弦或任何其他变换。我希望这有帮助。

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

关于Ruby 曲线拟合(对数回归)包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3893306/

相关文章:

ruby-on-rails - 更新方法的参数数量错误(1 对 2)

javascript - 从度数中获取方向

有序分区集的算法

python函数确定一个数字属于给定数字列表的统计可能性

r - 有什么方法可以在对图中标记点吗?

ruby - Rails 3 观察者和 current_user

ruby - 删除 `puts()` 参数中变量后的换行符

ruby-on-rails - 使用 ruby​​ 从字符串创建 json 对象

python - 使用 Pandas 库将日期/时间转换为月份后获取 float 而不是整数

c# - 谷歌分析类似工具