1

I would like to perform a t-test for every row in my matrix. The matrix looks like that:

data <- 
structure(c(NA, NA, 216750, 440450, NA, NA, 597510, 1839055, 
            851820, 1210200, NA, NA, NA, NA, 486720, 602970, 333150, 346532, 
            NA, NA, 421290, 425660, NA, 375440), .Dim = c(6L, 4L), .Dimnames = list(
              c("Gregg", "Mark", "Donnie", 
                "Fred", "Tim", "Gracie"
              ), c("AUC_Rep1", "AUC_Rep2", "AUC_Rep3", "AUC_Rep4")))

As you can see there are two problems with the data. First one is that it contains NAs and second one is that in some rows there is not enough data - just one value in whole row.

Do you know any way to avoid that problem ? I would like to create a function which first of all ignores NAs and if there is only 1 value in the row it should give NA as an output of t-test.

I usually use function from pi0 package - matrix.t.test

Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89
Shaxi Liver
  • 1,052
  • 3
  • 25
  • 47
  • 1
    If your data is a `data.frame` this might work: `apply(dat,1,function(x) ifelse(sum(is.na(x))%in%c(length(x),length(x)-1),"NA",t.test(x,na.rm=TRUE)))` – count Jul 20 '16 at 12:59
  • It does not solve the problem. Gives error `Error in ifelse(sum(is.na(x)) %in% c(length(x), length(x) - 1), "NA", : error in evaluating the argument 'no' in selecting a method for function 'ifelse': Error in t.test.default(x, na.rm = TRUE) : not enough 'x' observations` – Shaxi Liver Jul 21 '16 at 08:01
  • Works for me given your example. Does the error occur with the actual data? – count Jul 21 '16 at 10:18
  • It doesn't work with a example data for me as well. First error which comes is `Called from: t.test.default(x, na.rm = TRUE)` and when I press to continue it gives the error mentioned in previous comment. – Shaxi Liver Jul 21 '16 at 12:45

1 Answers1

0

Adapting the comment from @count to return p-values:

tpval <- function(x) {
  if(sum(!is.na(x)) < 2) {
    NA_real_
  } else {
    t.test(x, na.rm=TRUE)$p.value
  }
}

> apply(data, 1, tpval)
 Gregg       Mark     Donnie       Fred        Tim     Gracie
    NA         NA 0.03350020 0.03600664         NA 0.02547686

I was running in to the same problems a lot. So recently created a package matrixTests that accomplish what you are looking for:

library(matrixTests)
row_t_onesample(data)

And the result is:

> row_t_onesample(data)
       obs    mean          var   stderr df statistic     pvalue  conf.low conf.high alternative mean.null conf.level
Gregg    1  597510          NaN      NaN  0        NA         NA        NA        NA   two.sided         0       0.95
Mark     1 1839055          NaN      NaN  0        NA         NA        NA        NA   two.sided         0       0.95
Donnie   4  494145  70080791100 132363.9  3  3.733231 0.03350020  72904.05  915386.0   two.sided         0       0.95
Fred     4  669820 136234723133 184549.9  3  3.629478 0.03600664  82499.72 1257140.3   two.sided         0       0.95
Tim      1  333150          NaN      NaN  0        NA         NA        NA        NA   two.sided         0       0.95
Gracie   2  360986    417836232  14454.0  1 24.974817 0.02547686 177330.52  544641.5   two.sided         0       0.95

Warning message:
row_t_onesample: 3 of the rows had less than 2 "x" observations.
First occurrence at row 1
Karolis Koncevičius
  • 9,417
  • 9
  • 56
  • 89