1

I am new in RPy, so excuse me, if my question is trivial. I'm trying to write the top solution from this topic: Screening (multi)collinearity in a regression model in Python, but I get following error:

rpy.RPy_RException: Error in x$terms : $ operator is invalid for atomic vectors

Code I wrote:

from rpy import *
r.set_seed(42)
a=r.rnorm(100)
b=r.rnorm(100)
m=r.model_matrix('~a+b')

What am I doing wrong?

EDIT: using reply written by agstudy (thank You for help!) I prepared solution working for rpy2

from rpy2 import robjects
rset_seed = robjects.r('set.seed')
fmla = robjects.Formula('~a+b')
model_matrix = robjects.r('model.matrix')
rnorm = robjects.r('rnorm')
rset_seed(42)
env = fmla.environment
env['a']=rnorm(100)
env['b']=rnorm(100)
m=model_matrix(fmla)
Community
  • 1
  • 1
user1633361
  • 141
  • 9

1 Answers1

0

This should work

fmla = r.Formula('~a+b')
env = fmla.environment
env['a'] = a
env['b'] = b
r.model_matrix(fmla)

In R, you can reproduce the error

set_seed(42)
a=rnorm(100)
b=rnorm(100)
m=model.matrix('~a+b')
Error: $ operator is invalid for atomic vectors
m=model.matrix(formula('~a+b')) ## this works
  (Intercept)          a          b
1           1 -0.1011361  0.4354445
2           1  0.3782215 -1.5322641
3           1  1.4772023  0.3280948
4           1  0.2892421  1.9012016
5           1 -0.2596562  0.2036678
6           1 -0.5585396 -0.1536021
agstudy
  • 119,832
  • 17
  • 199
  • 261