Skip to content

Commit b4e61a2

Browse files
committed
add tests for paired t-test
1 parent 6c6dae1 commit b4e61a2

File tree

1 file changed

+111
-1
lines changed

1 file changed

+111
-1
lines changed

tests/testthat/test-ttest.R

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,116 @@ test_that('ttest',{
88
ttest(factor_names='Species')
99
# apply
1010
M = model_apply(M,D)
11-
expect_equal(M[2]$t_statistic[1],-15.386,tolerance=0.0005)
11+
expect_equal(M[2]$t_statistic[1,1],-15.386,tolerance=0.0005)
1212
})
1313

14+
test_that('paired-ttest',{
15+
set.seed('57475')
16+
# DatasetExperiment
17+
D=iris_DatasetExperiment()
18+
19+
# reduce to two classes
20+
M=filter_smeta(mode='exclude',levels='setosa',factor_name='Species')
21+
M=model_apply(M,D)
22+
D=predicted(M)
23+
24+
# set up paired sampling
25+
D$sample_meta$pair_id=c(1:50,1:50)
26+
27+
# remove one row so we can check it works for unequal pairs
28+
D=D[1:99,]
29+
30+
# set one value to NA so we can check the corresponding sample is excluded for that pair
31+
D$data[20,1]=NA
32+
33+
# reorder to check we're not order dependent
34+
D2=D[sample(1:99,99),]
35+
36+
# method
37+
M = ttest(factor_names='Species',paired=TRUE,paired_factor="pair_id")
38+
# apply
39+
M = model_apply(M,D)
40+
M2= model_apply(M,D2)
41+
42+
# 50 pairs - 1 without pair - 1 with an NA - 1 for ttest
43+
expect_equal(M$dof[1],47)
44+
expect_equal(M$t_statistic[1,1],-5.195,tolerance=0.0005)
45+
46+
# 50 pairs - 1 without pair - 1 for ttest
47+
expect_equal(M$dof[2],48)
48+
expect_equal(M$t_statistic[2,1],-3.014,tolerance=0.0005)
49+
50+
# check order dependence
51+
expect_equal(M$t_statistic[1,1],M2$t_statistic[1,1],tolerance=0.00005)
52+
53+
# reduce to less than three pairs to check we get NA
54+
D=D[c(1:3,51:53),]
55+
D$data[2,2]=NA
56+
57+
# should get NA for second feature as not enough pairs
58+
M3=model_apply(M,D)
59+
expect_true(is.na(M3$t_statistic[2,1]))
60+
})
61+
62+
# ttest
63+
test_that('wilcox',{
64+
set.seed('57475')
65+
# DatasetExperiment
66+
D=iris_DatasetExperiment()
67+
68+
# need two groups
69+
M = filter_smeta(mode='exclude',levels='versicolor',factor_name='Species')
70+
M=model_apply(M,D) # need two groups
71+
D = predicted(M)
72+
73+
# apply
74+
M = wilcox_test(factor_names='Species')
75+
M = model_apply(M,D)
76+
77+
expect_equal(M$statistic[1,1],38.5,tolerance=0.1)
78+
})
79+
80+
test_that('paired-wilcox',{
81+
set.seed('57475')
82+
# DatasetExperiment
83+
D=iris_DatasetExperiment()
84+
85+
# reduce to two classes
86+
M=filter_smeta(mode='exclude',levels='setosa',factor_name='Species')
87+
M=model_apply(M,D)
88+
D=predicted(M)
89+
90+
# set up paired sampling
91+
D$sample_meta$pair_id=c(1:50,1:50)
92+
93+
# remove one row so we can check it works for unequal pairs
94+
D=D[1:99,]
95+
96+
# set one value to NA so we can check the corresponding sample is excluded for that pair
97+
D$data[20,1]=NA
98+
99+
# reorder to check we're not order dependent
100+
D2=D[sample(1:99,99),]
101+
102+
# method
103+
M = wilcox_test(factor_names='Species',paired=TRUE,paired_factor="pair_id")
104+
# apply
105+
M = model_apply(M,D)
106+
M2= model_apply(M,D2)
107+
108+
# 50 pairs - 1 without pair - 1 with an NA - 1 for ttest
109+
expect_equal(M$statistic[1,1],155.5,tolerance=0.0005)
110+
111+
# 50 pairs - 1 without pair - 1 for ttest
112+
expect_equal(M$statistic[2,1],238,tolerance=0.0005)
113+
114+
# check order dependence
115+
expect_equal(M$statistic[1,1],M2$statistic[1,1],tolerance=0.00005)
116+
117+
# reduce to less than three pairs to check we get NA
118+
D$data[53:99,2]=NA
119+
120+
# should get NA for second feature as not enough pairs
121+
M3=model_apply(M,D)
122+
expect_true(is.na(M3$statistic[2,1]))
123+
})

0 commit comments

Comments
 (0)