Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use model.matrix through rpy2?

I prefer python over R for my work. From time to time, I need to use R functions, and I start to try Rpy2 for that purpose.

I tried but failed to find out how to replicate following with Rpy2

design <- model.matrix(~Subject+Treat)

I have gone as far as this:

import rpy2.robjects as robjects
fmla = robjects.Formula('~subject+treatment')
env = fmla.environment
env['subject'] = sbj_group
env['treatment'] = trt_group

from what I saw here. But I could not find how to perform model.matrix. I tried a couple of different ways:

robjects.r.model_matrix(fmla)
robjects.r('model.matrix(%s)' %fmla.r_repr())

As you can see none of them is right.

I am new to Rpy2, and fairly inexperienced in R. Any help would be appreciated!

like image 558
xyliu00 Avatar asked Sep 28 '22 20:09

xyliu00


1 Answers

You could evaluate strings as R code:

import numpy as np
import rpy2.robjects as ro
import rpy2.robjects.numpy2ri
ro.numpy2ri.activate() 
R = ro.r

subject = np.repeat([1,2,3], 4)
treatment = np.tile([1,2,3,4], 3)
R.assign('subject', subject)
R.assign('treatment', treatment)
R('subject <- as.factor(subject)')
R('treatment <- as.factor(treatment)')
R('design <- model.matrix(~subject+treatment)')
R('print(design)')

yields

   (Intercept) subject2 subject3 treatment2 treatment3 treatment4
1            1        0        0          0          0          0
2            1        0        0          1          0          0
3            1        0        0          0          1          0
4            1        0        0          0          0          1
5            1        1        0          0          0          0
6            1        1        0          1          0          0
7            1        1        0          0          1          0
8            1        1        0          0          0          1
9            1        0        1          0          0          0
10           1        0        1          1          0          0
11           1        0        1          0          1          0
12           1        0        1          0          0          1
attr(,"assign")
[1] 0 1 1 2 2 2
attr(,"contrasts")
attr(,"contrasts")$subject
[1] "contr.treatment"

attr(,"contrasts")$treatment
[1] "contr.treatment"

R(...) returns objects which you can manipulate on the Python side. For example,

design = R('model.matrix(~subject+treatment)')

assigns a rpy2.robjects.vectors.Matrix to design.

arr = np.array(design)

makes arr the NumPy array

[[ 1.  0.  0.  0.  0.  0.]
 [ 1.  0.  0.  1.  0.  0.]
 [ 1.  0.  0.  0.  1.  0.]
 [ 1.  0.  0.  0.  0.  1.]
 [ 1.  1.  0.  0.  0.  0.]
 [ 1.  1.  0.  1.  0.  0.]
 [ 1.  1.  0.  0.  1.  0.]
 [ 1.  1.  0.  0.  0.  1.]
 [ 1.  0.  1.  0.  0.  0.]
 [ 1.  0.  1.  1.  0.  0.]
 [ 1.  0.  1.  0.  1.  0.]
 [ 1.  0.  1.  0.  0.  1.]]

The column names can be accessed with

np.array(design.colnames)
# array(['(Intercept)', 'subject2', 'subject3', 'treatment2', 'treatment3',
#        'treatment4'], 
#       dtype='|S11')
like image 75
unutbu Avatar answered Oct 05 '22 07:10

unutbu