I am trying to pass a numpy array to the GAMLSS package in R.
import numpy as np
import rpy2.robjects as robjects
from rpy2.robjects import numpy2ri
numpy2ri.activate()
r = robjects.r
r.library("gamlss")
r.library("gamlss.mx")
L = r['data.frame'](np.array(np.random.normal(size=1000),
dtype=([('x', np.float), ('y', np.float), ('z', np.float)])))
r.gamlssMX(robjects.Formula('z~1'), data=L)
Running this returns
Error in y0 - f0 : non-conformable arrays
Yet I can pass the data frame to the linear model R function.
lm = r.lm(robjects.Formula('x~y'), data=L)
print r.summary(lm.rx())
I have got a load of code that reads a binary file in Python but would like to use the R package, hence the need for rpy2.
-- EDIT --
As an example in R:
x <- data.frame(z=c(rnorm(1000), rnorm(1000, mean=4)))
gamlssMX(z~1, K=1, data=x)
Looks like it is a bug, if I use the now depreciated
pandas.rpy.common.convert_to_r_dataframe
, it works fine:But the currently preferred method raises error:
There is only one line of difference: use
com.convert_to_r_dataframe
orpandas2ri.pandas2ri
. Looks like the current version has a bug.The newer
pandas2ri.pandas2ri
method results inrpy2.robjects.vectors.Array
and the oldercom.convert_to_r_dataframe
results inrpy2.robjects.vectors.FloatVector
.Looks like
gamlss
raise an exception when the data vector isArray
instead ofFloatVector
.