Skip to content

Commit 034ad9a

Browse files
committed
improve error-catching for paired samples
better processing of the returned list and replacing NA when test fails
1 parent 634a49a commit 034ad9a

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

R/ttest_class.R

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ setMethod(f="model.apply",
101101
stop('must have exactly two levels for this implmentation of t-statistic')
102102
}
103103

104+
estimate_name='estimate'
104105
if (M$paired){
105106
# check that we have a pair for each sample,
106107
# if not then remove
@@ -150,19 +151,44 @@ setMethod(f="model.apply",
150151
D=predicted(FF)
151152

152153
# check equal numbers per class. if not equal then exclude.
153-
out=FF$count[,1]!=FF$count[,2]
154-
D$data=D$data[,!out]
154+
IN=rownames(FF$count)[(FF$count[,1]==FF$count[,2]) & (FF$count[,1]>2) & (FF$count[,2]>2)]
155+
D$data=D$data[,IN]
156+
D$variable_meta=D$variable_meta[IN,]
157+
158+
estimate_name='estimate.mean of the differences'
155159
}
156160

157161

158162

159163
X=D$data
160164
y=D$sample_meta[[M$factor_names]]
161165

162-
output=apply(X,2,function(x) {
163-
a=unlist(t.test(x[y==L[1]],x[y==L[2]],paired = M$paired)[c("statistic","p.value","parameter",'conf.int','estimate')])
166+
output=lapply(X,function(x) {
167+
a=tryCatch({
168+
g=unlist(t.test(x[y==L[1]],x[y==L[2]],paired = M$paired)[c("statistic","p.value","parameter",'conf.int','estimate')])
169+
return(g)
170+
},warning=function(w) {
171+
g = NA
172+
return(g)
173+
}, error=function(e) {
174+
g = NA
175+
return(g)
176+
}
177+
)
164178
})
165179

180+
# replace na with vector of na the correct length/names
181+
na=which(is.na(output))
182+
183+
if (length(na)>0) {
184+
notna=which(!is.na(output))
185+
torep=output[[notna[1]]] # the first output that worked as expected
186+
torep[1:length(torep)]=NA # populate with NA
187+
output[na]=rep(list(torep),length(na))
188+
189+
}
190+
191+
output=as.data.frame(output)
166192
temp=data.frame(row.names=CN) # make sure we get result for all features, even if NA
167193
output=merge(temp,as.data.frame(t(output),stringsAsFactors = FALSE),by=0,all=TRUE,sort=FALSE)
168194
rownames(output)=output$Row.names

0 commit comments

Comments
 (0)