如何通过 RSRuby 将 ts 对象传递给 R

2024-01-19

我完全没有运气通过 RSRuby 和 R 使用 HoltWinters 函数。您到底是如何 1) 通过 RSRuby 创建时间序列对象以及 2) 成功地将其传递给 HoltWinters 以获得输出?

Example:

@r = RSRuby.instance
=> #<RSRuby:0x106bfe6c0 @proc_table={}, @class_table={}, @default_mode=-1, @cache={"get"=>#<RObj:0x106bfe580>, "helpfun"=>#<RObj:0x106bfd3d8>, "help"=>#<RObj:0x106bfd3d8>, "NaN"=>NaN, "FALSE"=>false, "TRUE"=>true, "F"=>false, "NA"=>-2147483648, "eval"=>#<RObj:0x106bfdf18>, "T"=>true, "parse"=>#<RObj:0x106bfe0d0>}, @caching=true>
@r.assign('mytime',@r.ts(:data => [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34], :frequency => 12, :start => [1993,3], :end => [1995,3]))
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]
@r.HoltWinters(@r.mytime)
    RException: Error in decompose(ts(x[1L:wind], start = start(x), frequency = f), seasonal) : 
    time series has no or less than 2 periods

rsruby (0.5.1.1)

R版本2.12.2 (2011-02-25)

平台:x86_64-apple-darwin9.8.0/x86_64(64位)

:edit:仅 R 内的类似示例...如果我可以通过 RSRuby 从 HoltWinters 获得任何输出(错误除外),我会非常高兴

> z <- ts(1:34, frequency = 12, start = c(1993,3), end = c(1995,3))
> z
     Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
1993           1   2   3   4   5   6   7   8   9  10
1994  11  12  13  14  15  16  17  18  19  20  21  22
1995  23  24  25                                    
> HoltWinters(z)
Holt-Winters exponential smoothing with trend and additive seasonal component.

Call:
 HoltWinters(x = z) 

Smoothing parameters:
 alpha:  1 
 beta :  0 
 gamma:  0 

Coefficients:
             [,1]
a    2.500000e+01
b    1.000000e+00
s1  -8.141636e-16
s2  -8.141636e-16
s3   9.621933e-16
s4   2.738550e-15
s5  -8.141636e-16
s6  -8.141636e-16
s7   7.401487e-17
s8  -8.141636e-16
s9   9.621933e-16
s10 -8.141636e-16
s11 -8.141636e-16
s12  9.621933e-16

如果我正确理解您的问题,那么您确实正在寻找有关使用特定界面的指导。首先,让我告诉您,我不使用 RSRuby,但我确实使用不同的实用程序来进行 R 和 Ruby 集成。

经过进一步检查,我发现您确实想要一个时间序列选项...我相信...是的,可以通过使用数据框轻松生成它as.ts(data_frame) fxn.
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/ts.html http://stat.ethz.ch/R-manual/R-patched/library/stats/html/ts.html

我用过的很简单。如果这是一个主要问题,也许这对您有用。

require 'rserve/simpler'
r_object = Rserve::Simpler.new 

接下来我要做的是获取一个哈希,其中键对应于数据帧列,值是数组,然后运行Rserve::Simpler fxn Hash.to_dataframe以便他们做好皈依的准备。

data = Hash.new()#insert data here
datafr = data.to_dataframe
r_object.converse(df: datafr) do 
  %Q{df$time <- strptime(as.character(df$time), "%Y-%m-%d %X")
     df$name <- factor(df$name)
  }
end

这在相反的块中使用直接的 R 代码并很好地处理一切。我还没有尝试过解决您的特定问题,但我知道这对我导入时间数据有用(RubyDatetimeclass)从数组转换到 R 中,以便我可以绘制它的图表。祝你好运!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何通过 RSRuby 将 ts 对象传递给 R 的相关文章

随机推荐