xyl*_*u00 5 python r rpy2 model.matrix
在我的工作中,我更喜欢 Python 而不是 R。有时,我需要使用 R 函数,为此我开始尝试 Rpy2。
我尝试过但未能找到如何使用 Rpy2 复制以下内容
design <- model.matrix(~Subject+Treat)
Run Code Online (Sandbox Code Playgroud)
我已经走了这么远:
import rpy2.robjects as robjects
fmla = robjects.Formula('~subject+treatment')
env = fmla.environment
env['subject'] = sbj_group
env['treatment'] = trt_group
Run Code Online (Sandbox Code Playgroud)
从我在这里看到的。但我找不到如何表演model.matrix。我尝试了几种不同的方法:
robjects.r.model_matrix(fmla)
robjects.r('model.matrix(%s)' %fmla.r_repr())
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,没有一个是正确的。
我是 Rpy2 的新手,并且在 R 方面经验不足。任何帮助将不胜感激!
您可以将字符串评估为 R 代码:
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)')
Run Code Online (Sandbox Code Playgroud)
产量
(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"
Run Code Online (Sandbox Code Playgroud)
R(...)返回可以在 Python 端操作的对象。例如,
design = R('model.matrix(~subject+treatment)')
Run Code Online (Sandbox Code Playgroud)
将 a 赋值rpy2.robjects.vectors.Matrix给design.
arr = np.array(design)
Run Code Online (Sandbox Code Playgroud)
制作arrNumPy 数组
[[ 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.]]
Run Code Online (Sandbox Code Playgroud)
列名可以通过以下方式访问
np.array(design.colnames)
# array(['(Intercept)', 'subject2', 'subject3', 'treatment2', 'treatment3',
# 'treatment4'],
# dtype='|S11')
Run Code Online (Sandbox Code Playgroud)