7

In R I have the following matrix (each row represents a bootstrap 95% confidence interval generated from the same sample data):

       low   high
[1,]   22.2  25.5
[2,]   23.1  25.9
[3,]   23.4  26.1
...

I know the true population mean of the data, it's 23.3. So the first two include the true mean but the third does not.

In R, I want to run a for loop i through nrow(matrix) times, each i checking whether or not the true population mean of the data is in that particular interval, then return a column vector of height nrow(matrix) of TRUE if the interval contains the true mean, and FALSE otherwise.

How could I do this?

dplanet
  • 5,273
  • 9
  • 29
  • 44

4 Answers4

17

You can simply use the inequality operators directly on the matrix columns. So I would have simply done:

> cbind( mat[,1] <= 23.3 & mat[,2] >= 23.3 )

      [,1]
[1,]  TRUE
[2,]  TRUE
[3,] FALSE
Prasad Chalasani
  • 19,912
  • 7
  • 51
  • 73
9

Just for the record, this can also be easily achieved using between from the data.table package.

data.table::between(23.3, mat[, 1], mat[, 2])
## [1]  TRUE  TRUE FALSE
fdetsch
  • 5,239
  • 3
  • 30
  • 58
8
 mat <- matrix(c(22.2,  25.5,
    23.1 , 25.9,
    23.4,  26.1), ncol=2, byrow=TRUE)
 trueval <- 23.3
 apply(mat, 1, findInterval, x=trueval)
#[1] 1 1 0
 which( apply(mat, 1, findInterval, x=trueval) == 1)
#[1] 1 2
  apply(mat, 1, findInterval, x=trueval) == 1
#[1]  TRUE  TRUE FALSE
IRTFM
  • 258,963
  • 21
  • 364
  • 487
1

There is a fast way of doing that if your data are centered on zero,

Zero.included = apply(mat,1,function(x){prod(sign(x))<=0})

if your matrix is not centered around the mean just add new.mat=mat-23.3

TPArrow
  • 1,518
  • 18
  • 25