开发者

How to pass a ts object to R via RSRuby

开发者 https://www.devze.com 2023-03-15 17:20 出处:网络
I\'ve had absolutely no luck working with the HoltWinters function via RSRuby and R.How exactly do you 1) create a time series object through RSRuby and 2) successfully pass that to HoltWinters to get

I've had absolutely no luck working with the HoltWinters function via RSRuby and R. How exactly do you 1) create a time series object through RSRuby and 2) successfully pass that to HoltWinters to get output?

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 version 2.12.2 (2011-02-25)

Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

:edit: a similar example inside R only... if I could get any output from HoltWinters via RSRuby (other 开发者_运维知识库than an error) I'd be very happy

> 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


If I understand your question correctly, you are really looking for guidance on using a specific interface. Up front, let me tell you that I don't use RSRuby, but I do use a different utility for R and Ruby integration.

Upon further examination, I see you really want a time series option... Which I believe... Yes, that can easily be produced from a dataframe by using the as.ts(data_frame) fxn.
http://stat.ethz.ch/R-manual/R-patched/library/stats/html/ts.html

What I've used is simple enough. If this is a major issue, maybe this can be of use to you.

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

What I do next is to take a Hash where the keys correspond to dataframe columns, and the values are arrays, and I run the Rserve::Simpler fxn Hash.to_dataframe on them so that they are ready to be converted.

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

This uses straight R code in the converse block and handles everything nicely. I haven't tried this with your particular issue, but I know that this worked for me to import time data (Ruby Datetime class) from an array into R so that I could graph it. Good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消