Nonparametric tests

 

Sign Test


@dataclass
class test_sign_Result:
	lower:None|tuple[float, float, float]
	interpolated:None|tuple[float, float]
	upper:None|tuple[float, float, float]
	pvalue:float


def test_sign(
	x:Iterable, 
	md:numbers.Real, 
	y=None, 
	alternative="two.sided", 
	conflevel=0.95)->test_sign_Result:
"""
returns test_sign_Result class.

x: Sample
y: Second sample
md: Median of the population tested by the null hypothesis
alternative: "two.sided", "less", "greater" 
conflevel: Confidence level, [0,1]
"""

Example


from scisuit.stats import test_sign

data1 = {
	"x":[7.8, 6.6, 6.5, 7.4, 7.3, 7., 6.4, 7.1, 6.7, 7.6, 6.8], 
	"md":6.5, "confint":True}

data2 = {
	"x":[24, 28, 28, 31, 32, 33, 38, 39, 41, 43, 48, 49, 49, 51, 52, 56, 56, 58, 62, 63],
	"md": 40}


result = test_sign(**data1)
print(result)

p-value = 0.0215
CI for 93.45703125% = (6.6, 7.4)
CI for interpolated = (6.5712, 7.4574)
CI for 98.828125% = (6.5, 7.6)

 

 

Wilcoxon Signed-Rank Test


@dataclass
class test_wilcox_Result:
	acl: None|float
	ci:None|tuple[float, float]
	pvalue:float


def test_wilcox(x:Iterable, 
	md:numbers.Real,  
	confint=True,
	alternative="two.sided", 
	conflevel=0.95)->test_wilcox_Result:
"""
returns test_wilcox_Result class.  

x: Sample  
md: Median of the population tested by the null hypothesis  
confint: Should compute confidence intervals?  
alternative: "two.sided", "less", "greater"   
conflevel: Confidence level, [0,1]  
"""

Example


from scisuit.stats import test_wilcox

data1 = {
	"x":[7.8, 6.6, 6.5, 7.4, 7.3, 7., 6.4, 7.1, 6.7, 7.6, 6.8], 
	"md":6.5, "confint":True}

data2 = {
	"x":[24, 28, 28, 31, 32, 33, 38, 39, 41, 43, 48, 49, 49, 51, 52, 56, 56, 58, 62, 63],
	"md": 40}


result = test_wilcox(**data1)
print(result)

Wilcoxon Signed Rank Test 

p-value = 0.0093
Achieved Conf (%) = 95.117
CI = (6.699, 7.450)

 

 

Mann-Whitney Test


@dataclass
class test_mannwhitney_Result:
	acl: None|float
	ci:None|tuple[float, float]
	pvalue:float
	U:float
	W:float
	median_xy:tuple[float, float]

	
def test_mannwhitney(x:Iterable, 
	y:Iterable,
	md = 0.0,  
	confint=True,
	alternative="two.sided", 
	conflevel=0.95)->test_mannwhitney_Result:
"""
returns Mann-Whitney test class.  

x, y: Samples  
md: Hypothesized difference between x and y  
confint: Should compute confidence intervals?  
alternative: "two.sided", "less", "greater"   
conflevel:	Confidence level, [0,1]  
"""

Example


from scisuit.stats import test_mannwhitney

experiment = [2, 19, 13, 9, 17, 18, 24, 15, 22, 21]
control = [8, 1, 0, 10, 20, 5, 11, 7, 3, 13, 4]

result = test_mannwhitney(experiment, control)
print(result)

Mann-Whitney Test

Medians: x=17.5, y=7.0
p-value = 0.011217

U-statistics = 91.5
W-statistics = 146.5
Achieved Conf (%) = 95.704
CI = (1.999, 14.999)

 

 

Kruskal-Wallis Test

Available since v1.8.


@dataclass
class test_kruskal_Result:
	pvalue:float
	statistic: float
	zvalues: list[float]
	counts: list[int]
	ranks: list[float]
	uniqueFactors: list[str]


def test_kruskal(
	responses:Iterable, 
	groups:Iterable)->test_kruskal_Result:
"""
responses: All responses (must be numeric entries)
groups : groups to which each response belongs
"""

Example


from scisuit.stats import test_kruskal

responses = [52, 58, 59, 65, 69, 71, 55, 58, 60, 62, 66, 78, 57, 
		66, 70, 77, 79, 81, 67, 72, 81, 84, 91, 95]

groups = ["A", "A", "A", "A", "A", "A", 
	"B", "B", "B", "B", "B", "B", 
	"C", "C", "C", "C", "C", "C", 
	"D", "D", "D", "D", "D", "D"]


result = test_kruskal(responses, groups)
print(result)

Kruskal-Wallis Test 

p-value = 0.0134
Statistic = 10.715

Factor        z-value            Rank           Count 
A              -1.77             8.08              6
B              -1.67             8.33              6
C               0.60            14.00              6
D               2.83            19.58              6

 

 

Friedman Test

Available since v1.8.


@dataclass
class test_friedman_Result:
	pvalue:float
	statistic: float
	
	counts:list[int] #counts of each factors
	medians:list[float]

	quantile_25:list[float] 
	quantile_75:list[float]

	#Note that ranksums are ordered in the order of uniqueFactors
	ranksums:list[float]
	uniqueFactors:list[str]


def test_friedman(
   responses:Iterable, 
   factors: Iterable,
   groups:Iterable)->test_friedman_Result:
"""
returns Friedman test result class.  

responses: All responses
factors: Factor level for the response
groups	: groups to which each response belongs
"""

Example


from scisuit.stats import test_friedman

"""
Data for strength of cotton fibers 
(Larsen & Marx An introduction to mathematical statistics and its applications )

                         Treatments
             36	     54	     72	    108	    144
            -------------------------------------
        1|  7.62    8.14    7.76    7.17    7.46
Blocks	2|  8.00    8.15    7.73    7.57    7.68
        3|  7.93    78.87   7.74    7.80    7.21

"""

#Above shown data table must be entered in the following way
responses = [7.62, 8, 7.93, 8.14, 8.15, 7.87, 7.76, 7.73, 7.74, 7.17, 7.57, 7.8, 7.46, 7.68, 7.21]
factors = ["36", "36", "36", "54", "54", "54", "72", "72", "72", "108", "108", "108", "144", "144", "144"]
groups = ["1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3", "1", "2", "3"]


result = test_friedman(responses, factors, groups)
print(result)

Friedman Test 

p-value = 0.067
Statistic = 8.799

Factor            25%          Median             75%         Ranksum           Count
108              7.37            7.57            7.69            5.00               3
144              7.33            7.46            7.57            5.00               3
36               7.78            7.93            7.96           12.00               3
54               8.01            8.14            8.14           14.00               3
72               7.74            7.74            7.75            9.00               3