使用python和rpy2绘制拟合模型的预测值

时间:2015-08-07 10:18:37

标签: rpy2

我无法使用rpy2绘制观察到的预测值和预测值。下面的R代码有效:

                                                 #==============================#
                                                 # Set up the data.             #
                                                 #==============================#
yTmp <- seq(1,20)
y <- NULL
x1Tmp <- seq(1,20)
x1 <- NULL
x2Tmp <- seq(1,20)
x2 <- NULL
for (i in 1:20)
{
    y[i] = yTmp[i] + runif(1, 0, 1)
    x1[i] = x1Tmp[i] + runif(1, 0, 1)
    x2[i] = x2Tmp[i] + runif(1, 0, 1)
}
                                                #==============================#
                                                # Fit the model.               #
                                                #==============================#
fittedModel <- lm(y ~ x1 + x2)
                                                #==============================#
                                                # Plot the observed vs predicted
                                                #==============================#
png(filename="fittedModel_R.png")
plot(fittedModel$fitted.values, y)
dev.off()

这会产生一个很好的观察与预测的情节(我会发布,但我需要至少10个声望来发布图像)。

我曾尝试使用rpy2重现这一点,但我无法弄清楚如何使拟合值很好地发挥作用。下面的代码与我上面的R代码相同,但不起作用:

#!/usr/bin/env python
                                                #==============================#
                                                # Set up packages.             #
                                                #==============================#
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
import random
import string
stats = importr('stats')
from rpy2.robjects import Formula
lattice = importr('lattice')
rprint = robjects.globalenv.get("print")
grdevices = importr('grDevices')
                                                #==============================#
                                                # Set up the data.             #
                                                #==============================#
y = robjects.FloatVector(())
x1 = robjects.FloatVector(())
x2 = robjects.FloatVector(())
for i in range(1,20):
    yValue = i + random.random()
    y.rx[i] = yValue
    x1Value = i + random.random()
    x1.rx[i] = x1Value
    x2Value = i + random.random()
    x2.rx[i] = x2Value
robjects.globalenv["y"] = y
robjects.globalenv["x1"] = x1
robjects.globalenv["x2"] = x2
                                                #==============================#
                                                # Fit the model.               #
                                                #==============================#
fittedModel = stats.lm("y ~ x1 + x2")
                                                #==============================#
                                                # Attempt to extract the fitted 
                                                # values from the model and put
                                                # on a vector.                 #
                                                #==============================#
robjects.globalenv['predicted'] = robjects.Vector(fittedModel.rx('fitted.values'))
                                                #==============================#
                                                # Plot the observed vs predicted
                                                #==============================#
grdevices.png(file = "fittedModel_RPY2.png", width = 512, height = 512)
formula = Formula('y ~ predicted')
p = lattice.xyplot(formula)
rprint(p)
grdevices.dev_off()

这会产生错误:

  

/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py:106:   UserWarning:顺序错误(as.numeric(x)):( list)对象不能   被胁迫输入'double'

     

res = super(功能,自我)。致电(* new_args,** new_kwargs)   回溯(最近一次调用最后一次):文件“./testRPY2.py”,第45行,中          p = lattice.xyplot(formula)File“/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py”,   第178行,在通话中       return super(SignatureTranslatedFunction,self)。调用(* args,** kwargs)文件“/usr/local/lib/python2.7/dist-packages/rpy2/robjects/functions.py”,   第106行,在电话中       res = super(函数,自我)。调用(* new_args,** new_kwargs)rpy2.rinterface.RRuntimeError:顺序错误(as.numeric(x)):
  (list)对象不能被强制输入'double'

问题肯定在于预测值,因为将公式更改为绘制y与y的关系会产生一个很好的情节:

formula = Formula('y ~ y')

我已经多次尝试将python中的数据强制转换为可绘制的格式,包括在python中转换为字符串,操作并作为浮动向量发送回rpy2。但我真的不明白为什么它不起作用,必须有更好的方法。非常感谢任何见解和对我的问题的帮助。

1 个答案:

答案 0 :(得分:0)

如果对predicted是什么有疑问,可以尝试

print(type(robjects.globalenv['predicted']))

或更长的输出:

print(robjects.globalenv['predicted'])

你会看到你在R中没有所谓的原子向量,这几乎可以肯定来自创建predicted的一行:

robjects.globalenv['predicted'] = robjects.Vector(fittedModel.rx('fitted.values'))

方法.rx()对应于R的[,而.rx2()对应于R的[[。你会想要后者。

文档中的介绍有一个简短的例子,其中有R lm()http://rpy.sourceforge.net/rpy2/doc-2.6/html/introduction.html#linear-models