Skip to content

Commit 33a0ec9

Browse files
committed
improve NA handling
1 parent 0edba93 commit 33a0ec9

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: structToolbox
22
Type: Package
33
Title: Data processing & analysis tools for Metabolomics and other omics
4-
Version: 1.5.6
4+
Version: 1.5.7
55
Authors@R: c(
66
person(
77
c("Gavin","Rhys"),

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
Changes 1.5.7
2+
+ improve NA handling in fold_change computations
3+
14
Changes 1.5.5
25
+ change t-test outputs to data.frame
36
+ fix NA bug in feature_boxplot chart

R/fold_change_class.R

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,19 @@ setMethod(f="model_apply",
245245
out=lapply(D2$data,function(x) {
246246
y1=x[D2$sample_meta[[M$factor_name]]==L[A]]
247247
y2=x[D2$sample_meta[[M$factor_name]]==L[B]]
248-
val=ci_delta_nu(na.omit(y1),na.omit(y2),alpha=1-M$conf_level,paired=M$paired)
248+
249+
if (M$paired) {
250+
# if one of pair is NA, set both to NA
251+
y1[is.na(y2)]=NA
252+
y2[is.na(y1)]=NA
253+
}
254+
255+
if (all(is.na(y1)) | all(is.na(y2))) {
256+
val=rep(NA,3) # report NA if a group is missing
257+
} else {
258+
val=ci_delta_nu(y1,y2,alpha=1-M$conf_level,paired=M$paired)
259+
}
260+
249261
val=matrix(val,nrow=1,ncol=3)
250262
colnames(val)=c('median','lci','uci')
251263
return(val)
@@ -284,9 +296,22 @@ setMethod(f="model_apply",
284296

285297
# calculate means and confidence intervals for all features
286298
out=lapply(D2$data,function(x) {
299+
287300
y1=x[D2$sample_meta[[M$factor_name]]==L[A]]
288301
y2=x[D2$sample_meta[[M$factor_name]]==L[B]]
289-
ret=ci.mean.paired(1-M$conf_level,y1,y2)
302+
303+
if (M$paired) {
304+
# if one of pair is NA, set both to NA
305+
y1[is.na(y2)]=NA
306+
y2[is.na(y1)]=NA
307+
}
308+
309+
if (all(is.na(y1)) | all(is.na(y2))) {
310+
ret=matrix(NA,nrow=1,ncol=3) # report NA if a group is missing
311+
colnames(ret)=c('fold_change','lower_ci','upper_ci')
312+
} else {
313+
ret=ci.mean.paired(1-M$conf_level,y1,y2)
314+
}
290315
return(ret)
291316
})
292317

@@ -296,7 +321,13 @@ setMethod(f="model_apply",
296321
out=lapply(D2$data,function(x) {
297322
y1=x[D2$sample_meta[[M$factor_name]]==L[A]]
298323
y2=x[D2$sample_meta[[M$factor_name]]==L[B]]
299-
ret=ci.mean.bs(1-M$conf_level,na.omit(y1),na.omit(y2))
324+
325+
if (all(is.na(y1)) | all(is.na(y2))) {
326+
ret=matrix(NA,nrow=1,ncol=8) # report NA if a group is missing
327+
colnames(ret)=c("Mean1", "Mean2", "df", " Mean1/Mean2", "LL", "UL", " Log-ratio", "SE")
328+
} else {
329+
ret=ci.mean.bs(1-M$conf_level,na.exclude(y1),na.exclude(y2))
330+
}
300331
return(ret)
301332
})
302333

@@ -472,7 +503,7 @@ ci_delta_nu = function(y1,y2,alpha=0.05,paired=FALSE) {
472503
return(out)
473504
} else {
474505
if (length(y1) != length(y2)) {
475-
stop('all samples must be present in all groups for a paired comparison')
506+
stop('the same number of samples must be present in all groups for a paired comparison')
476507
}
477508
r = y1/y2
478509
mr=mean(r)

0 commit comments

Comments
 (0)