Skip to content

Commit d90be4c

Browse files
committed
add additional checks to ttest for paired samples
1 parent 34fbb31 commit d90be4c

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

R/ttest_class.R

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,54 @@ setMethod(f="method.apply",
9797
u=unique(D$sample_meta[[M$paired_factor]])
9898
out=character(0) # list of sample_id to remove
9999
for (k in u) {
100-
n=sum(D$sample_meta[[M$paired_factor]]==k)
100+
n=sum(D$sample_meta[[M$paired_factor]]==k) # number of samples (could be same class)
101101
if (n<2) {
102102
out=c(out,k)
103103
}
104+
# if we have more than 2 then we need an even number.
105+
if (n%%2 != 0) {
106+
out=c(out,k)
107+
}
108+
# check we have enough groups (must be two for ttest)
109+
ng=length(unique(D$sample_meta[[M$factor_names]][D$sample_meta[[M$paired_factor]]==k]))
110+
if (ng != 2) {
111+
out=c(out,k)
112+
}
104113

105114
}
106115
X=X[!(D$sample_meta[[M$paired_factor]] %in% out),]
107116
y=y[!(D$sample_meta[[M$paired_factor]] %in% out)]
108117

109-
# sort the data by sample id so that theyre in the right order for paired ttest
110-
X=apply(X,2,function(x) {
111-
a=x[y==L[1]]
112-
b=x[y==L[2]]
113-
ay=y[y==L[1]]
114-
by=y[y==L[2]]
115-
a=a[order(ay)]
116-
b=b[order(by)]
117-
return(c(a,b))
118-
})
119-
# also make sure the meta data is in the right order
120-
y=y[order(D$sample_meta[[M$paired_factor]])]
118+
# sort the data by sample id so that theyre in the right order for paired ttest
119+
X=apply(X,2,function(x) {
120+
a=x[y==L[1]]
121+
b=x[y==L[2]]
122+
ay=y[y==L[1]]
123+
by=y[y==L[2]]
124+
a=a[order(ay)]
125+
b=b[order(by)]
126+
return(c(a,b))
127+
})
128+
# also make sure the meta data is in the right order
129+
y=y[order(D$sample_meta[[M$paired_factor]])]
130+
131+
# put back into dataset object
132+
D$data=as.data.frame(X)
133+
D$sample_meta[[M$paired_factor]]=y
121134
}
122135

123-
output=apply(X,2,function(x) {a=unlist(t.test(x[y==L[1]],x[y==L[2]],paired = M$paired)[c("statistic","p.value","parameter",'conf.int','estimate')])})
136+
# check number per class
137+
# if less then 2 then remove
138+
FF=filter_na_count(threshold=2,factor_name=M$factor_names)
139+
FF=method.apply(FF,D)
140+
D=predicted(FF)
141+
X=D$data
142+
y=D$sample_meta[[M$paired_factor]]
143+
print(FF$count)
144+
145+
output=apply(X,2,function(x) {
146+
a=unlist(t.test(x[y==L[1]],x[y==L[2]],paired = M$paired)[c("statistic","p.value","parameter",'conf.int','estimate')])
147+
})
124148
output=as.data.frame(t(output),stringsAsFactors = FALSE)
125149
output$p.value=p.adjust(output$p.value,method = param.value(M,'mtc'))
126150
output.value(M,'t_statistic')=output$statistic.t

0 commit comments

Comments
 (0)