extract coefficients from R lme model in rpy2

谁说我不能喝 提交于 2019-12-12 01:45:04

问题


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")

Yields all the coefficients as follows:

$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

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 

How can I get the coefficient for log.var1 ?


回答1:


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]

or

# R vectors are one-based (so 1+1=2)
model.rx2("coefficients").rx2("fixed").rx(2)


来源:https://stackoverflow.com/questions/35395476/extract-coefficients-from-r-lme-model-in-rpy2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!