本文介绍了从 rpy2 中的 R lme 模型中提取系数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要使用 rpy2 提取在 python 中调用的 R lme 模型的结果.使用 rx2 提取系数如下:
I need to extract the results of an R lme model called in python using rpy2. Extracting the coefficients with rx2 as follows :
model = nlme.lme(fixed=fixed, data=dfr, random=random, method="REML")
print model.rx2("coefficients")
产生如下所有系数:
$fixed
(Intercept) log.var1
12.571692 -2.929928
$random
$random$item
(Intercept) log.var1
12545646546 -5.189606e-16 8.276929e-16
$random$category
(Intercept) log.var1
0001544848484/DLMX -3.1909917 2.3938670
我想在模型的固定部分提取log.var1
的系数.我尝试了以下,但我得到了 NULL
I want to extract the coefficient for log.var1
in the fixed part of the model. I tried the following, but I got NULL
print model.rx2("coefficients$fixed[2]")
#gives NULL
如何获得 log.var1 的系数?
How can I get the coefficient for log.var1 ?
推荐答案
rx2
方法对应于 R 的 [[
,我理解与 相同$
.
The method rx2
corresponds to R's [[
, which I understand to be identical to $
.
考虑到这一点,您可以像访问系数"一样访问系数"中名为固定"的项目:
With that in mind, you can access the item called "fixed" within "coefficients" the same way you are already accessing "coefficients":
# Python sequences are zero-based
model.rx2("coefficients").rx2("fixed")[1]
或
# R vectors are one-based (so 1+1=2)
model.rx2("coefficients").rx2("fixed").rx(2)
这篇关于从 rpy2 中的 R lme 模型中提取系数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!