【问题标题】:Error when using midasr r package function through rpy2 in python在python中通过rpy2使用midasr r包函数时出错
【发布时间】:2016-05-25 18:21:40
【问题描述】:

我正在尝试在 python 中访问 'midasr' r 包中的函数,这是我的代码:

from rpy2.robjects import pandas2ri
pandas2ri.activate()
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import pandas as pd

....
base = importr('base')
stats = importr('stats')
midasr = importr('midasr')

x = np.random.rand(11256)
y = np.random.rand(1407)
eq = midasr.midas_r('y ~ mls(x, 0:15, 8, nealmon)', start = 'list(x = c(0, 0))')

我得到错误:

RRuntimeError: Error in new.env(parent = environment(formula)) : 
  use of NULL environment is defunct

它在 R 中的工作方式为:

eq_r <- midas_r(y ~ mls(x, 0:15, 8, nealmon), start = list(x = c(0,0)))

所以我尝试了另一种方法:

temp = np.empty(len(x))
temp[:] = np.nan
temp[0:1407] = y
dataframe = pd.DataFrame({'x': x, 'y': temp}) 
rdf = pandas2ri.DataFrame(dataframe)
robjects.globalenv['dataframe'] = dataframe
eq = midasr.midas_r('y[1:1407] ~ mls(x, 0:15, 8, nealmon)', data=rdf, start = 'list(x = c(0, 0))')

还是同样的错误。

在收到@Parfeit 的回复后,我尝试了:

formula = robjects.Formula('y_ro ~ mls(x_ro, 0:15, 8, nealmon)')
env = formula.environment
env["y_ro"] = y_ro
env["x_ro"] = x_ro
slist = robjects.ListVector({'x_ro': robjects.IntVector((0, 0))})
eq = midasr.midas_r(formula, start = slist)

但现在我明白了:

RRuntimeError: Error in midas_r.fit(prepmd) : 
  The optimisation algorithm of MIDAS regression failed with the following message:
Error in y - mdsrhs(p) : non-conformable arrays

Please try other starting values or a different optimisation function

但是我可以在 r 中成功运行相同的数据集。

有人知道如何解决这个问题吗?非常感谢!

【问题讨论】:

    标签: python r pandas rpy2


    【解决方案1】:

    考虑构建您的公式,传入当前值和 start 参数以及传入的 Python 对象:

    x = np.random.rand(11256)
    y = np.random.rand(1407)
    
    formula = robjects.Formula('y ~ x')
    env = formula.environment
    env["y"] = y
    env["x"] = midasr.mls(x, robjects.IntVector(range(15)), 8, midasr.nealmon)    # MLS() FCT
    
    slist = robjects.ListVector({'x': robjects.IntVector((0, 0))})                # R LIST: $x [1] 0 0
    
    eq = midasr.midas_r(formula, start = slist)
    

    【讨论】:

    • 感谢您的回复! env["x"] = midasr.mls(x, range(15), 8, nealmon) 应该是 env["x"] = midasr.mls(x, np.array(range(16)), 8, 'nealmon')。但是在我运行这个之后,我得到了Error in as.vector(x$model[, -1] %*% x$midas_coefficients) : error in evaluating the argument 'x' in selecting a method for function 'as.vector': Error in x$model[, -1] %*% x$midas_coefficients : non-conformable arguments
    • nealmon 是从哪里得到的?如果是命名对象,则需要在 Python 中定义。而 R 的 0:15 转换为 Python 的 range(15),而不是 np.array()。实际上,根据docs,它应该是robjects.IntVector(range(15))
    • nealmon 是 midasr 包中的权重函数。 np.array()robjects.IntVector(range(15)) 工作方式相同,都生成正确。
    • @Parfeit 但它会生成Error in as.vector(x$model[, -1] %*% x$midas_coefficients) : error in evaluating the argument 'x' in selecting a method for function 'as.vector': Error in x$model[, -1] %*% x$midas_coefficients : non-conformable arguments
    • 然后你需要限定函数,因为 Python 需要它对每个对象:midasr.nealmon。我希望我有那个 R 包来全面测试。
    【解决方案2】:

    使用与 Parfait 相同的代码,并添加具有正确数量的元素的 start 参数,该数量等于变量的数量

     x = np.random.rand(11256)
     y = np.random.rand(1407)
    
     formula = robjects.Formula('y ~ x')
     env = formula.environment
     env["y"] = y
     env["x"] = midasr.mls(x, robjects.IntVector(range(16)), 8, midasr.nealmon)# MLS() FCT
    
     slist = robjects.ListVector({'x': robjects.IntVector(base.rep(0,16))})# R LIST: $x [1] 0 0
    
     eq = midasr.midas_r(formula, start = slist)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-07
      • 2014-10-19
      • 2013-03-11
      • 2018-06-14
      • 2016-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多