gpt4 book ai didi

python - 在 RPy 中获取 nlme.lme() 或 lme4.lmer() 的清晰摘要

转载 作者:太空狗 更新时间:2023-10-30 01:29:00 28 4
gpt4 key购买 nike

我正在通过 RPy 连接 nlme 和 lme4 R 函数,我想从我的 python 控制台访问输出摘要。

我运行以下代码:

test1=nlme.lme(r.formula('Pupil~CoI*Time'), random=r.formula('~1|ID'),data=dfr)
test2=nlme.lme(r.formula('Pupil~CoI*measurement'),random=r.formula('~1|ID'),data=dfr)
test1_sum= r.summary(test1)
test2_sum= r.summary(test2)
print test1_sum
print test2_sum

对于 nlme,对于 lme4:

test1=lme4.lmer(r.formula('Pupil~CoI*Time+(1|ID)'),data=dfr)
test2=lme4.lmer(r.formula('Pupil~CoI*measurement+(1|ID)'),data=dfr)
test1_sum= r.summary(test1)
test2_sum= r.summary(test2)
print test1_sum
print test2_sum

要获取包含数据和显式导入的代码片段,请参阅此 IPython notebook .

在所有情况下,我都会得到大量的打印输出,其中包括一个非常长的部分,如下所示:

Data: structure(list(CoI = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L ......

我想获得更多的总结:

Random effects:
Formula: ~1 | ID
(Intercept) Residual
StdDev: 0.2201214 0.1199874

Fixed effects: Pupil ~ CoI * measurement
Value Std.Error DF t-value p-value
(Intercept) 1.2068660 0.06369911 5769 18.946357 0
CoIhard -0.0394413 0.00629117 5769 -6.269306 0
measurement -0.0002743 0.00003207 5769 -8.554287 0
CoIhard:measurement 0.0005227 0.00004536 5769 11.524511 0
Correlation:
(Intr) CoIhrd msrmnt
CoIhard -0.049
measurement -0.060 0.612
CoIhard:measurement 0.043 -0.865 -0.707

Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-9.86773055 -0.37638950 0.02085029 0.43203795 4.97364143

Number of Observations: 5784
Number of Groups: 12

(包含在我得到的内容中,但仅在上述内容之后出现数千个条目)我该如何实现?

最佳答案

正确的方法是使用.rx2()方法,有多种不同的使用方法:

In [43]:

print test2_sum.names
Unable to unlink tempfile c:\docume~1\x60t\locals~1\temp\tmpnhw4n4
[1] "methTitle" "objClass" "devcomp" "isLmer" "useScale"

[6] "logLik" "family" "link" "ngrps" "coefficients"

[11] "sigma" "vcov" "varcor" "AICtab" "call"

[16] "residuals"

In [44]:

print test2_sum.rx2('vcov') # to access R type print out
Unable to unlink tempfile c:\docume~1\x60t\locals~1\temp\tmpebn3f1
4 x 4 Matrix of class "dpoMatrix"

(Intercept) CoIhard measurement CoIhard:measurement

(Intercept) 93253.4275120 -80.6588422 -0.503069702 0.503069702

CoIhard -80.6588422 161.3176844 0.503069702 -1.006139404

measurement -0.5030697 0.5030697 0.004192248 -0.004192248

CoIhard:measurement 0.5030697 -1.0061394 -0.004192248 0.008384495

In [45]:

print test2_sum.rx2('varcor') # to access R type print out
Unable to unlink tempfile c:\docume~1\x60t\locals~1\temp\tmpcad6ld
Groups Name Std.Dev.

ID (Intercept) 1057.39

Residual 242.24

In [46]:

list(test2_sum.rx2('varcor')) # to get the values
Out[46]:
[<Matrix - Python:0x0782CEB8 / R:0x0E97FB28>
[1118073.223847]]
In [47]:

list(test2_sum.rx2('varcor')[0]) # to get the values
Out[47]:
[1118073.2238471208]

你将通过跳过callsresiduals 来摆脱大部分事情,尝试:

for i, v in enumerate(list(test2_sum.names)):
if v not in ['call', 'residuals']:
print '%s========================================================='%i, v
print test2_sum.rx2(v)

附加编辑:

我认为访问 lme4 结果(使用 rpy2)的 tTable 的最佳方法是将其转换为 Pandas DataFrame:

In [73]:

print com.convert_robj(test2_sum.rx2('tTable'))
Value Std.Error DF t-value p-value
(Intercept) 2480.515542 305.374210 5769 8.122872 5.521357e-16
CoIhard -90.840336 12.701090 5769 -7.152169 9.602962e-13
measurement -0.288709 0.064748 5769 -4.458998 8.390496e-06
CoIhard:measurement 1.049136 0.091567 5769 11.457595 4.546122e-30

[4 rows x 5 columns]

print 输出与R 打印不完全匹配,但很容易完成:

In [87]:

print test2_sum.rx2('tTable').__str__().replace('\r\n\r\n', '\n')

Value Std.Error DF t-value p-value
(Intercept) 2480.5155423 305.37420990 5769 8.122872 5.521357e-16
CoIhard -90.8403359 12.70108989 5769 -7.152169 9.602962e-13
measurement -0.2887093 0.06474757 5769 -4.458998 8.390496e-06
CoIhard:measurement 1.0491363 0.09156689 5769 11.457595 4.546122e-30

关于python - 在 RPy 中获取 nlme.lme() 或 lme4.lmer() 的清晰摘要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24216036/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com