Skip to content

Commit 01c8807

Browse files
committed
add test for confounding factors
1 parent 2b9b43a commit 01c8807

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

R/confounders_clsq_class.R

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ confounders_clsq<-setClass(
2929

3030
params.threshold=entity(name='Confounding factor threshold',
3131
type='character',
32-
description='threshold for accepting a factor as confounding (0 < threshold < 1)'
32+
description='threshold for accepting a factor as confounding (0 < threshold < 1)',
33+
value=0.15
3334
),
3435

3536
params.confounding_factors=entity(name='Confounding factors',
@@ -74,7 +75,7 @@ setMethod(f="method.apply",
7475
nm=character(length(factor_names))
7576
for (i in 1:length(factor_names)) {
7677
fn=unique(c(factor_names[1],factor_names[i]))
77-
78+
7879
# for each factor name check the na count
7980
FF=filter_na_count(threshold=2)
8081
excl=matrix(NA,nrow=ncol(D$data),ncol=length(fn))
@@ -88,15 +89,15 @@ setMethod(f="method.apply",
8889
excl[,k]=FALSE
8990
}
9091
}
91-
92+
9293
if (!all(!excl)) {
9394
excl=apply(excl,1,function(x) {
9495
fn[!x]
9596
})
9697
} else {
97-
excl=fn #
98+
excl=fn #
9899
}
99-
100+
100101
clsq$factor_names=excl
101102
clsq=method.apply(clsq,D)
102103

@@ -121,13 +122,13 @@ setMethod(f="method.apply",
121122
# redo regression including all potential confounders for each feature
122123
conf=M$percent_change>M$threshold
123124
factor_names=M$confounding_factors
124-
L=apply(conf[,2:ncol(conf)],1,function(x) c(M$factor_name,factor_names[x]))
125+
L=apply(conf[,2:ncol(conf),drop=FALSE],1,function(x) c(M$factor_name,factor_names[x]))
125126
M2=classical_lsq(intercept=TRUE,alpha=M$alpha,mtc=M$mtc,factor_names=L)
126127
M2=method.apply(M2,D)
127128

128129
M$p_value=data.frame('ttest.p'=pvals[,1],'corrected.p'=M2$p_value[,2]) # MTC already applied
129130
names(L)=colnames(D$data)
130-
M$potential_confounders=L
131+
M$potential_confounders=as.list(L)
131132
M$significant=data.frame('sig'=M$p_value<M$alpha)
132133

133134
return(M)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# test confounders lsq
2+
test_that('confounders lsq',{
3+
set.seed('57475')
4+
# dataset
5+
D=iris_dataset()
6+
D$sample_meta$Rnd=sample(D$sample_meta$Species,150) # random effect
7+
# method
8+
ME=confounders_clsq(factor_name='Species',confounding_factors='Rnd',threshold=0.15)
9+
ME=method.apply(ME,D)
10+
# expect all true
11+
expect_true(all(as.matrix(ME$significant)))
12+
13+
})
14+
15+
# confounders_lsq plots
16+
# test confounders lsq
17+
test_that('confounders lsq barchart',{
18+
set.seed('57475')
19+
# dataset
20+
D=iris_dataset()
21+
D$sample_meta$Rnd=sample(D$sample_meta$Species,150) # random effect
22+
# method
23+
ME=confounders_clsq(factor_name='Species',confounding_factors='Rnd',threshold=0.15)
24+
ME=method.apply(ME,D)
25+
# chart
26+
C=confounders_lsq.barchart(feature_to_plot='Petal.Length')
27+
gg=chart.plot(C,ME)
28+
# expect all true
29+
expect_true(is(gg,'ggplot'))
30+
31+
})
32+
33+
# test confounders lsq
34+
test_that('confounders lsq boxplot',{
35+
set.seed('57475')
36+
# dataset
37+
D=iris_dataset()
38+
D$sample_meta$Rnd=sample(D$sample_meta$Species,150) # random effect
39+
# method
40+
ME=confounders_clsq(factor_name='Species',confounding_factors='Rnd',threshold=0.15)
41+
ME=method.apply(ME,D)
42+
# chart
43+
C=confounders_lsq.boxplot()
44+
gg=chart.plot(C,ME)
45+
# expect all true
46+
expect_true(is(gg,'ggplot'))
47+
48+
})
49+
50+
# test confounders lsq
51+
test_that('confounders lsq image',{
52+
set.seed('57475')
53+
# dataset
54+
D=iris_dataset()
55+
D$sample_meta$Rnd=sample(D$sample_meta$Species,150) # random effect
56+
# method
57+
ME=confounders_clsq(factor_name='Species',confounding_factors='Rnd',threshold=0.15)
58+
ME=method.apply(ME,D)
59+
# chart
60+
C=confounders_lsq.image()
61+
gg=chart.plot(C,ME)
62+
# expect all true
63+
expect_true(is(gg,'ggplot'))
64+
65+
})

0 commit comments

Comments
 (0)