如何在Ruby on Rails App中实现r脚本

时间:2018-10-03 19:36:44

标签: ruby-on-rails r

我需要在RoR应用程序中的一组数据上运行R脚本。我尝试使用yth_filtersee here),但响应时间为15+秒。有谁知道某种转换或实现此R脚本的更好方法?


## Filter macro series
# Filtering parameters

#   A user will need to enter these  
#   values into interactive fields
#   to adjust the trend line.
#   h = number of periods forecasted
#       into the future.
#   p = number of previous values
#       selected as independent
#       variables.

    rm(list=ls())

    # Get the file   
    y <- read.csv('D:/Papers/cato/cmfa_filtering/NGDP.csv', header=TRUE, row.names=1)
    y <- xts(y, order.by = as.Date(rownames(y), format="%m/%d/%Y"))

    # Parameters
    h <- 8
    p <- 4

    # Initialize
    lag <- h

    # Generate the lags
    for (i in 1:p) {    
      # Create the lagged variable
      assign(paste0('y', i), lag.xts(y, k=lag))    
      # Tick ahead
      lag <- lag+1
    }    
    # Do OLS
    ols <- lm(y~y1+y2+y3+y4)

    # Get the trend and cycle components
    out.trend <- fitted(ols)
    out.cycle <- resid(ols)

    # Compare to yth_filter
    out.pack <- yth_filter(y, h=h, p=p, output=c("x", "trend"), family = gaussian)
    compare <- cbind.xts(out.pack,out.trend)

0 个答案:

没有答案