rpy2下的立体主义回归:“下标超出范围”错误

时间:2019-05-17 02:18:10

标签: python r rpy2

当我使用rpy2进行Cubist回归时,我遇到了错误:

Error in strsplit(tmp, "\"")[[1]] : subscript out of bounds

我尝试使用as.matrix更改数据格式,但是仍然无法使用。

import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import FloatVector
from rpy2.robjects import pandas2ri
Cubist = importr('Cubist')
lattice = importr('lattice')
r = robjects.r
# 准备样点数据
dt = r('mtcars')
Z = FloatVector(dt[3])
X = FloatVector(dt[5])
X1 = FloatVector(dt[6])
T = r['cbind'](X,X1)

regr = r['cubist'](x=T,y=Z,committees=10)

1 个答案:

答案 0 :(得分:0)

如果是矩阵,则x的{​​{1}}参数似乎需要一个cubist()属性。

R中的设置:

dimnames

现在比较一下(重现您的错误):

library(Cubist)

dt = mtcars
Z = dt[, 4]
X = dt[, 6]
X1 = dt[, 7]

vs。

> T = cbind(dt[, 6], dt[, 7])
> str(T)
 num [1:32, 1:2] 2.62 2.88 2.32 3.21 3.44 ...
> cubist(x=T, y=Z, committees=10)
cubist code called exit with value 1
Error in strsplit(tmp, "\"")[[1]] : subscript out of bounds

有多种方法可确保通过> T = cbind(X, X1) > str(T) num [1:32, 1:2] 2.62 2.88 2.32 3.21 3.44 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:2] "X" "X1" > cubist(x=T, y=Z, committees=10) Call: cubist.default(x = T, y = Z, committees = 10) Number of samples: 32 Number of predictors: 2 Number of committees: 10 Number of rules per committee: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 连接暗号。使用代码的一种简单方法是简单地明确命名变量:

rpy2