Skip to content

Instantly share code, notes, and snippets.

@phobson
Created March 9, 2013 00:14
Show Gist options
  • Save phobson/5121607 to your computer and use it in GitHub Desktop.
Save phobson/5121607 to your computer and use it in GitHub Desktop.
comparing p-values from independence tests between R and scipy
import numpy as np
import scipy.stats as stats
import rpy
import matplotlib.pyplot as plt
spy_p_paired = []
spy_p_nopair = []
rpy_p_paired = []
rpy_p_nopair = []
for n in range(1000):
# fake data
y1 = np.random.lognormal(mean=0.57, sigma=1.35, size=37)
y2 = np.random.lognormal(mean=0.57, sigma=1.35, size=45)
x = np.random.lognormal(mean=0.51, sigma=1.15, size=37)
# indep. tests in R
rpy_paired = rpy.r.wilcox_test(x, y1, paired=True)
rpy_notpaired = rpy.r.wilcox_test(x, y2, paired=False)
# inped. tests in scipy
U, Up = stats.mannwhitneyu(x,y2)
W, Wp = stats.wilcoxon(x, y1)
spy_p_paired.append(Wp)
spy_p_nopair.append(Up*2)
rpy_p_paired.append(rpy_paired['p.value'])
rpy_p_nopair.append(rpy_notpaired['p.value'])
fig, ax = plt.subplots()
ax.loglog(spy_p_paired, rpy_p_paired, 'k.')
ax.xaxis.grid(False, which='minor')
ax.yaxis.grid(False, which='minor')
ax.set_xlabel('Scipy p-val')
ax.set_ylabel('R p-val')
ax.set_xlim([0.0001, 1])
ax.set_ylim([0.0001, 1])
ax.set_aspect('equal')
plt.show()
fig, ax = plt.subplots()
ax.loglog(spy_p_nopair, rpy_p_nopair, 'k.')
ax.xaxis.grid(False, which='minor')
ax.yaxis.grid(False, which='minor')
ax.set_xlabel('Scipy p-val')
ax.set_ylabel('R p-val')
ax.set_xlim([0.0001, 1])
ax.set_ylim([0.0001, 1])
ax.set_aspect('equal')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment