使用 Python 和 Rpy2 进行统计测试(Kolmogorov 和 T-test) [英] Statistics Tests (Kolmogorov and T-test) with Python and Rpy2

查看:65
本文介绍了使用 Python 和 Rpy2 进行统计测试(Kolmogorov 和 T-test)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行了一些算法,想对结果进行一些统计分析.我有两个平均错误率的向量.

I've runned some algorithms and wanted to make some statistics analysis with the results. I have two vectors with the averages of the error rate.

使用 R,使用下面的行我会得到一切.

With R, using the line below I would get everything.

t.test(methodresults1,methodresults2,var.equal=FALSE,paired=FALSE,alternative="less")

因为我使用的是 Python,所以我想使用 Rpy2 项目.

Since I'm using Python, I wanted to use Rpy2 project.

我试过了:

import rpy2.robjects as R

# methodresults1 and methodresults2 are numpy arrays.

# kolmogorov test
normality_res = R.r['ks.test'](R.FloatVector(methodresults1.tolist()),'pnorm',mean=R.FloatVector(methodresults1.mean().tolist()),sd=R.FloatVector(methodresults1.std().tolist())))

# t-test
res = R.r['t.test'](R.FloatVector(methodresults1.tolist()),R.FloatVector(methodresults2.tolist()),alternative='two.sided',var.equal=FALSE,paired=FALSE)

res.rx('p.value')[0][0]
res.rx('statistic')[0][0]
res.rx('parameter')[0][0]

我无法进行这两项测试.

I wasn't able to perform both tests.

我还发现 t-test 的问题在于 var.equal 语句,它给了我一个 * 语法错误:关键字不能是表达式(第 1 行).

I found also that the problem with the t-test is with the var.equal statement and it gives me an * SyntaxError: keyword can't be an expression (, line 1).

额外问题:有没有更好的方法来处理 numpy 和 Rpy2?

Extra question: Is there a better way to work with numpy and Rpy2?

推荐答案

正如它所说:语法错误:关键字不能是表达式(第 1 行)."

在 Python 中,符号不能包含字符.".

In Python, symbols cannot contain the character ".".

from rpy2.robjects.packages import importr
from rpy2.robjects.vectors import StrVector
stats = importr("stats")
stats.t_test(methodresults1, methodresults2,
             **{'var.equal': False,
                'paired': False,
                'alternative': StrVector(("less", ))})

查看 rpy2 函数文档了解更多信息详情.

Check the rpy2 documentation about functions for more details.

这篇关于使用 Python 和 Rpy2 进行统计测试(Kolmogorov 和 T-test)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆