0
votes

I am trying the following code:

from rpy import *
r.library("ltm")

dat= #some data frame or matrix
r.ltm(r('dat~z1'))

error coming is--- RPy_RException: Error in eval(expr, envir, enclos) : object 'dat' not found

Please tell me the right way to call ltm function using rpy library

1

1 Answers

1
votes

I'd try as a general approach using rpy2 and something along the lines of:

from rpy2.robjects import *
r("library('ltm')")
r.assign('r_var_name',py_var_name)
r("r_var_name<-as.desired.data.type(r_var_name)")

Then whatever commands you're doing to 'r_var_name' using 'ltm' package functions inside further r("blah") statements.

E.g. getting the coefficients for one of the ltm package examples:

In [30]: py_obj = r("coef(ltm(Abortion ~ z1, control = list(GHk = 20, iter.em = 20)))")
In [32]: py_obj
Out[32]: 
<Matrix - Python:0x4db0290 / R:0x52f04f0>
[0.188998, -0.256378, -0.367623, ..., 4.542567, 5.840821, 3.243826]