Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
26e17f4
DOC: add pairwise_tukeyhsd (instead of tukeyhsd) to docs, some cleanup
josef-pkt Mar 24, 2013
a284b87
STY: test_pairwise reformat
josef-pkt Apr 10, 2013
01a3a95
BUG: multipletest, fix 'h', 'hs' if all rejected, closes #763
josef-pkt Apr 10, 2013
a9126b4
BUG/TST: multipletests fix reject for 'hs', fix weak, strict inequali…
josef-pkt Apr 11, 2013
1e57633
REF: fdr_correction_twostage change definition of pvalscorr,
josef-pkt Apr 11, 2013
a6f6db8
REF/TST: fdr small changes, refactor and add tests,
josef-pkt Apr 12, 2013
8522715
ENH/REF: fdr_correction_twostage: add 'fdr_tsbky', 'fdr_tsbh' separately
josef-pkt Apr 12, 2013
150afe9
TST: activate test for last change, some test cleanup
josef-pkt Apr 12, 2013
6565eb9
BUG: fdrcorrection_twostage fix pval and consistent return if we retu…
josef-pkt Apr 14, 2013
f63e3a6
TST/STY one more test case, some code cleanup (remove old comments)
josef-pkt Apr 14, 2013
c3744f4
REF: multipletesting missed an inequality
josef-pkt Apr 18, 2013
5e946be
ENH: add normal based tests to weightstats (functions only)
josef-pkt Apr 21, 2013
2a14112
ENH: add dictionary for multipletesting methods
josef-pkt Apr 21, 2013
a849521
REF: rename _proportions (un-privatize)
josef-pkt Apr 21, 2013
4cbc0b4
ENH: add tests for proportions (copied from devel script)
josef-pkt Apr 21, 2013
979e1fb
TST/DOC first test and example for proportion tests
josef-pkt Apr 21, 2013
051928e
TST test_pairwise.py put tabs back in (error in string parsing)
josef-pkt Apr 21, 2013
5e736cc
REF: proportions_chisquare_allpairs return Results instance
josef-pkt Apr 21, 2013
3583552
TST more unittests for prop test
josef-pkt Apr 21, 2013
7119436
TST add tests for binom_test
josef-pkt Apr 21, 2013
647806a
ENH/REF/TST add binom_tost, rename power_binom_tost, test for binom_test
josef-pkt Apr 21, 2013
d46d26b
TST: add tests for power_binom_tost
josef-pkt Apr 21, 2013
543e7de
ENH more options for power_ztost_prop, plus unittests (some only for …
josef-pkt Apr 22, 2013
89a66cb
REF/DOC proportions: rename some arguments, docstrings
josef-pkt Apr 22, 2013
713f6e7
REF: start stats.base, move AllPairsResults
josef-pkt Apr 22, 2013
99b02e5
REF fix import after move, currently lazy (watch out for circular imp…
josef-pkt Apr 22, 2013
915a58b
TST: test for ztost, no exact benchmark for p-value
josef-pkt Apr 23, 2013
1916745
TST verify binom_tost against confint_proportion
josef-pkt Apr 23, 2013
491eca9
REF rename n_success -> count
josef-pkt Apr 23, 2013
67c4cf2
DOC partial docstrings for AllPairsResults
josef-pkt Apr 23, 2013
de99c28
ENH: add proportions_ztest 1- and 2-sample
josef-pkt May 1, 2013
e193b78
ENH: add plot method to power class
josef-pkt May 3, 2013
b1010ff
DOC: docstring for plot_power method
josef-pkt May 3, 2013
c776b16
REF change positional arguments in GofChisquarePower.power, add test …
josef-pkt May 4, 2013
cc58621
TST: add smoke test for plot_power (excludes FTestPower) (test runs o…
josef-pkt May 4, 2013
fb94bb3
STY: skip wrong pylint errors
josef-pkt May 4, 2013
930bf82
DOC fix ftest power docstring
josef-pkt May 4, 2013
599946d
BUG: FTestPower ncc not transmitted (no tests for ncc option)
josef-pkt May 4, 2013
61b9087
BUG fix multitest_alias (not much used or tested yet)
josef-pkt May 4, 2013
532a316
BUG: binom_test_reject_interval reverse 1-s alternatives, with tests
josef-pkt May 5, 2013
13d2bcc
REF: fix proportion_effectsize, add unit test
josef-pkt May 5, 2013
548101d
REF: rename tost -> ttost (there are other TOSTs than t)
josef-pkt May 5, 2013
287a6b7
BUG fix import after renames
josef-pkt May 21, 2013
e352289
REF: add kwds to Power, add ddof to NormalIndPower (e.g. for correlat…
josef-pkt May 22, 2013
75e7077
REF/ENH: rename ttost method, add methods and ztost (not sure where I…
josef-pkt May 22, 2013
0c77c4c
REF/TST weightstats: test for 2 methods, proportion: small cleanup (a…
josef-pkt May 22, 2013
7a6d33b
fix ztest incorrect variance for 2sample, add tests
josef-pkt May 22, 2013
1861c11
REF/BUG/TST rename zconfint, add confint methods, fix dof, add tests
josef-pkt May 23, 2013
266f546
ENH: need ddof in z based tests (for proportion, maybe correlation)
josef-pkt May 23, 2013
9e829ae
REF: weightstats usevar rename separate -> unequal, some docstrings
josef-pkt May 23, 2013
af01fca
REF: stats standardize to name "two-sided", alias '2s' in some cases
josef-pkt May 23, 2013
5a7845e
REF: power minor change in non-working class
josef-pkt May 23, 2013
ae1213a
DOC: weightstats docstring cleanup, improvement
josef-pkt May 23, 2013
2ec7c4d
REF: weightstats rename confint_mean confint_diff -> tconfint_mean tc…
josef-pkt May 24, 2013
e89ae63
REF: rename confint_proportion -> proportion_confint, add to stats/a…
josef-pkt May 24, 2013
18346b9
REF rename proportion binom_test_stat -> binom_test, more stats/api.py
josef-pkt May 24, 2013
0447a3f
DOC: update sphinx docs stats.rst
josef-pkt May 24, 2013
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions docs/source/stats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,18 @@ to one way ANOVA, but still in developement

multipletests
fdrcorrection0
tukeyhsd

GroupsStats
MultiComparison

The following functions are not (yet) public (here for my own benefit, JP)
.. currentmodule:: statsmodels.stats.multicomp

.. autosummary::
:toctree: generated/

pairwise_tukeyhsd

The following functions are not (yet) public

.. currentmodule:: statsmodels.sandbox.stats.multicomp

Expand Down Expand Up @@ -235,8 +241,29 @@ two one-sided tests, which have as null hypothesis that the means are not
DescrStatsW
CompareMeans
ttest_ind
tost_ind
tost_paired
ttost_ind
ttost_paired
ztest
ztost
zconfint

weightstats also contains tests and confidence intervals based on summary
data

.. currentmodule:: statsmodels.stats.weightstats

.. autosummary::
:toctree: generated/

_tconfint_generic
_tstat_generic
_zconfint_generic
_zstat_generic
_zstat_generic2


Power and Sample Size Calculations
----------------------------------

The :mod:`power` module currently implements power and sample size calculations
for the t-tests, normal based test, F-tests and Chisquare goodness of fit test.
Expand All @@ -261,16 +288,38 @@ equations.
tt_ind_solve_power
zt_ind_solve_power

Also available are confidence intervals for proportions, and effect size for

Proportion
----------


Also available are hypothesis test, confidence intervals and effect size for
proportions that can be used with NormalIndPower.

.. currentmodule:: statsmodels.stats._proportion
.. currentmodule:: statsmodels.stats.proportion

.. autosummary::
:toctree: generated

confint_proportion
proportion_confint
proportion_effectsize

binom_test
binom_test_reject_interval
binom_tost
binom_tost_reject_interval


proportions_ztest
proportions_ztost
proportions_chisquare
proportions_chisquare_allpairs
proportions_chisquare_pairscontrol

proportion_effectsize
power_binom_tost
power_ztost_prop
samplesize_confint_proportion


Moment Helpers
Expand Down
203 changes: 203 additions & 0 deletions statsmodels/examples/ex_pairwise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
# -*- coding: utf-8 -*-
"""

Created on Sun Mar 24 10:26:39 2013

Author: Josef Perktold
"""

from statsmodels.compatnp.py3k import BytesIO, asbytes
import numpy as np
from numpy.testing import assert_almost_equal, assert_equal


ss = '''\
43.9 1 1
39.0 1 2
46.7 1 3
43.8 1 4
44.2 1 5
47.7 1 6
43.6 1 7
38.9 1 8
43.6 1 9
40.0 1 10
89.8 2 1
87.1 2 2
92.7 2 3
90.6 2 4
87.7 2 5
92.4 2 6
86.1 2 7
88.1 2 8
90.8 2 9
89.1 2 10
68.4 3 1
69.3 3 2
68.5 3 3
66.4 3 4
70.0 3 5
68.1 3 6
70.6 3 7
65.2 3 8
63.8 3 9
69.2 3 10
36.2 4 1
45.2 4 2
40.7 4 3
40.5 4 4
39.3 4 5
40.3 4 6
43.2 4 7
38.7 4 8
40.9 4 9
39.7 4 10'''

#idx Treatment StressReduction
ss2 = '''\
1 mental 2
2 mental 2
3 mental 3
4 mental 4
5 mental 4
6 mental 5
7 mental 3
8 mental 4
9 mental 4
10 mental 4
11 physical 4
12 physical 4
13 physical 3
14 physical 5
15 physical 4
16 physical 1
17 physical 1
18 physical 2
19 physical 3
20 physical 3
21 medical 1
22 medical 2
23 medical 2
24 medical 2
25 medical 3
26 medical 2
27 medical 3
28 medical 1
29 medical 3
30 medical 1'''

ss3 = '''\
1 24.5
1 23.5
1 26.4
1 27.1
1 29.9
2 28.4
2 34.2
2 29.5
2 32.2
2 30.1
3 26.1
3 28.3
3 24.3
3 26.2
3 27.8'''

ss5 = '''\
2 - 3 4.340 0.691 7.989 ***
2 - 1 4.600 0.951 8.249 ***
3 - 2 -4.340 -7.989 -0.691 ***
3 - 1 0.260 -3.389 3.909 -
1 - 2 -4.600 -8.249 -0.951 ***
1 - 3 -0.260 -3.909 3.389 '''

#accommodate recfromtxt for python 3.2, requires bytes
ss = asbytes(ss)
ss2 = asbytes(ss2)
ss3 = asbytes(ss3)
ss5 = asbytes(ss5)

dta = np.recfromtxt(BytesIO(ss), names=("Rust","Brand","Replication"))
dta2 = np.recfromtxt(BytesIO(ss2), names = ("idx", "Treatment", "StressReduction"))
dta3 = np.recfromtxt(BytesIO(ss3), names = ("Brand", "Relief"))
dta5 = np.recfromtxt(BytesIO(ss5), names = ('pair', 'mean', 'lower', 'upper', 'sig'), delimiter='\t')
sas_ = dta5[[1,3,2]]


if __name__ == '__main__':
import statsmodels.stats.multicomp as multi #incomplete refactoring

mc = multi.MultiComparison(dta['Rust'], dta['Brand'])
res = mc.tukeyhsd()
print res[0]

mc2 = multi.MultiComparison(dta2['StressReduction'], dta2['Treatment'])
res2 = mc2.tukeyhsd()
print res2[0]

mc2s = multi.MultiComparison(dta2['StressReduction'][3:29], dta2['Treatment'][3:29])
res2s = mc2s.tukeyhsd()
print res2s[0]
res2s_001 = mc2s.tukeyhsd(alpha=0.01)
#R result
tukeyhsd2s = np.array([1.888889,0.8888889,-1,0.2658549,-0.5908785,-2.587133,3.511923,2.368656,0.5871331,0.002837638,0.150456,0.1266072]).reshape(3,4, order='F')
assert_almost_equal(res2s_001[1][4], tukeyhsd2s[:,1:3], decimal=3)

mc3 = multi.MultiComparison(dta3['Relief'], dta3['Brand'])
res3 = mc3.tukeyhsd()
print res3[0]

# for mci in [mc, mc2, mc3]:
# get_thsd(mci)

from scipy import stats
print mc2.allpairtest(stats.ttest_ind, method='b')[0]

'''same as SAS:
>>> np.var(mci.groupstats.groupdemean(), ddof=3)
4.6773333333333351
>>> var_ = np.var(mci.groupstats.groupdemean(), ddof=3)
>>> tukeyhsd(means, nobs, var_, df=None, alpha=0.05, q_crit=qsturng(0.95, 3, 12))[4]
array([[ 0.95263648, 8.24736352],
[-3.38736352, 3.90736352],
[-7.98736352, -0.69263648]])
>>> tukeyhsd(means, nobs, var_, df=None, alpha=0.05, q_crit=3.77278)[4]
array([[ 0.95098508, 8.24901492],
[-3.38901492, 3.90901492],
[-7.98901492, -0.69098508]])
'''

ss5 = '''\
Comparisons significant at the 0.05 level are indicated by ***.
BRAND
Comparison Difference
Between
Means Simultaneous 95% Confidence Limits Sign.
2 - 3 4.340 0.691 7.989 ***
2 - 1 4.600 0.951 8.249 ***
3 - 2 -4.340 -7.989 -0.691 ***
3 - 1 0.260 -3.389 3.909 -
1 - 2 -4.600 -8.249 -0.951 ***
1 - 3 -0.260 -3.909 3.389 '''

ss5 = '''\
2 - 3 4.340 0.691 7.989 ***
2 - 1 4.600 0.951 8.249 ***
3 - 2 -4.340 -7.989 -0.691 ***
3 - 1 0.260 -3.389 3.909 -
1 - 2 -4.600 -8.249 -0.951 ***
1 - 3 -0.260 -3.909 3.389 '''

import StringIO
dta5 = np.recfromtxt(StringIO.StringIO(ss5), names = ('pair', 'mean', 'lower', 'upper', 'sig'), delimiter='\t')

sas_ = dta5[[1,3,2]]
confint1 = res3[1][4]
confint2 = sas_[['lower','upper']].view(float).reshape((3,2))
assert_almost_equal(confint1, confint2, decimal=2)
reject1 = res3[1][1]
reject2 = sas_['sig'] == '***'
assert_equal(reject1, reject2)
meandiff1 = res3[1][2]
meandiff2 = sas_['mean']
assert_almost_equal(meandiff1, meandiff2, decimal=14)
55 changes: 55 additions & 0 deletions statsmodels/examples/ex_proportion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
"""

Created on Sun Apr 21 07:59:26 2013

Author: Josef Perktold
"""

import numpy as np
import statsmodels.stats.proportion as sms
import statsmodels.stats.weightstats as smw

from numpy.testing import assert_almost_equal


# Region, Eyes, Hair, Count
ss = '''\
1 blue fair 23 1 blue red 7 1 blue medium 24
1 blue dark 11 1 green fair 19 1 green red 7
1 green medium 18 1 green dark 14 1 brown fair 34
1 brown red 5 1 brown medium 41 1 brown dark 40
1 brown black 3 2 blue fair 46 2 blue red 21
2 blue medium 44 2 blue dark 40 2 blue black 6
2 green fair 50 2 green red 31 2 green medium 37
2 green dark 23 2 brown fair 56 2 brown red 42
2 brown medium 53 2 brown dark 54 2 brown black 13'''

dta0 = np.array(ss.split()).reshape(-1,4)
dta = np.array(map(tuple, dta0.tolist()), dtype=[('Region', int), ('Eyes', 'S6'), ('Hair', 'S6'), ('Count', int)])

xfair = np.repeat([1,0], [228, 762-228])

# comparing to SAS last output at
# http://support.sas.com/documentation/cdl/en/procstat/63104/HTML/default/viewer.htm#procstat_freq_sect028.htm
# confidence interval for tost
ci01 = smw.confint_ztest(xfair, alpha=0.1)
assert_almost_equal(ci01, [0.2719, 0.3265], 4)
res = smw.ztost(xfair, 0.18, 0.38)

assert_almost_equal(res[1][0], 7.1865, 4)
assert_almost_equal(res[2][0], -4.8701, 4)

nn = np.arange(200, 351)
pow_z = sms.power_ztost_prop(0.5, 0.72, nn, 0.6, alpha=0.05)
pow_bin = sms.power_ztost_prop(0.5, 0.72, nn, 0.6, alpha=0.05, dist='binom')
import matplotlib.pyplot as plt
plt.plot(nn, pow_z[0], label='normal')
plt.plot(nn, pow_bin[0], label='binomial')
plt.legend(loc='lower right')
plt.title('Proportion Equivalence Test: Power as function of sample size')
plt.xlabel('Number of Observations')
plt.ylabel('Power')

plt.show()

2 changes: 1 addition & 1 deletion statsmodels/examples/try_power.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import numpy as np

import statsmodels.stats.power as smp
import statsmodels.stats._proportion as smpr
import statsmodels.stats.proportion as smpr

sigma=1; d=0.3; nobs=80; alpha=0.05
print smp.normal_power(d, nobs/2, 0.05)
Expand Down
Loading