Question

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.

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

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

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

I tried that:

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.

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

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

Was it helpful?

Solution

As it says: "SyntaxError: keyword can't be an expression (, line 1)."

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

Check the rpy2 documentation about functions for more details.


OTHER TIPS

to perform ks test with python, in case of a two-sample test, you can

>>> from scipy.stats import ks_2samp
>>> import numpy as np
>>> 

where x, y are two nupmy.array:

>>> ks_2samp(x, y)
(0.022999999999999909, 0.95189016804849658)

first value is the test statistics, and second value is the p-value. if the p-value is less than 95 (for a level of significance of 5%), this means that you cannot reject the Null-Hypothese that the two sample distributions are identical.

for one sample ks test, see for example here: http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kstest.html#scipy.stats.kstest

this test lets you test the goodness of fit of your empirical distribution to a given probability distribution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top