Skip to content

Commit cf264e5

Browse files
committed
add to tests for paired data
ensures there is a paired sample for each class. removes any feature where this is not true. ensures NA is reported for any feature without pairs.
1 parent d90be4c commit cf264e5

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

R/ttest_class.R

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ setMethod(f="method.apply",
8585
definition=function(M,D)
8686
{
8787
X=dataset.data(D)
88+
CN=colnames(X) # keep a copy of the original colnames
8889
y=dataset.sample_meta(D)[[M$factor_names]]
8990
L=levels(y)
9091
if (length(L)!=2) {
@@ -112,11 +113,12 @@ setMethod(f="method.apply",
112113
}
113114

114115
}
115-
X=X[!(D$sample_meta[[M$paired_factor]] %in% out),]
116-
y=y[!(D$sample_meta[[M$paired_factor]] %in% out)]
116+
D$data=D$data[!(D$sample_meta[[M$paired_factor]] %in% out),]
117+
D$sample_meta=D$sample_meta[!(D$sample_meta[[M$paired_factor]] %in% out),]
118+
y=dataset.sample_meta(D)[[M$factor_names]]
117119

118120
# sort the data by sample id so that theyre in the right order for paired ttest
119-
X=apply(X,2,function(x) {
121+
X=apply(D$data,2,function(x) {
120122
a=x[y==L[1]]
121123
b=x[y==L[2]]
122124
ay=y[y==L[1]]
@@ -125,27 +127,37 @@ setMethod(f="method.apply",
125127
b=b[order(by)]
126128
return(c(a,b))
127129
})
128-
# also make sure the meta data is in the right order
129-
y=y[order(D$sample_meta[[M$paired_factor]])]
130130

131131
# put back into dataset object
132132
D$data=as.data.frame(X)
133-
D$sample_meta[[M$paired_factor]]=y
133+
D$sample_meta[[M$factor_names]]=D$sample_meta[[M$factor_names]][order(D$sample_meta[[M$paired_factor]])]
134+
D$sample_meta[[M$paired_factor]]=D$sample_meta[[M$paired_factor]][order(D$sample_meta[[M$paired_factor]])]
135+
y=dataset.sample_meta(D)[[M$factor_names]]
136+
137+
# check number per class
138+
# if less then 2 then remove
139+
FF=filter_na_count(threshold=2,factor_name=M$factor_names)
140+
FF=method.apply(FF,D)
141+
D=predicted(FF)
142+
143+
# check equal numbers per class. if not equal then exclude.
144+
out=FF$count[,1]!=FF$count[,2]
145+
D$data=D$data[,!out]
134146
}
135147

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)
148+
149+
141150
X=D$data
142-
y=D$sample_meta[[M$paired_factor]]
143-
print(FF$count)
151+
y=D$sample_meta[[M$factor_names]]
144152

145153
output=apply(X,2,function(x) {
146154
a=unlist(t.test(x[y==L[1]],x[y==L[2]],paired = M$paired)[c("statistic","p.value","parameter",'conf.int','estimate')])
147155
})
148-
output=as.data.frame(t(output),stringsAsFactors = FALSE)
156+
157+
temp=data.frame(row.names=CN) # make sure we get result for all features, even if NA
158+
output=merge(temp,as.data.frame(t(output),stringsAsFactors = FALSE),by=0,all=TRUE)
159+
rownames(output)=output$Row.names
160+
output=output[,-1]
149161
output$p.value=p.adjust(output$p.value,method = param.value(M,'mtc'))
150162
output.value(M,'t_statistic')=output$statistic.t
151163
output.value(M,'p_value')=output$p.value

0 commit comments

Comments
 (0)