 # How to compare vector values to matrix values

Hi,
I have a matrix with 4 rows (different groups), and 10,000 columns. Each cell contains a p-value.
In addition, I have a vector named alpha, with 1000 values from 0 to 1.
I need to compare each alpha value, to the 10,000 p-values of the first matrix raw.
I need to count how many p-values are smaller than the alpha.
This is my code:

``````alpha<-seq(from=0,to=1,by=0.001)

for (i in 1:length(alpha)) {
treshold<-alpha[i]
for (j in 1:N) {
my.true<-p.values[1,j]<treshold
results<-matrix(my.true, nrow = 1000, ncol = N, byrow = TRUE)[i,j]
}
}
``````

Mi idea was to get TRUE or FALSE for each value, and then count how many TRUEs I got for each alpha. Finally divide these values by 10,000 in order to get the percentage of the p-values smaller than alpha.

My code doesn't work. It seems like it runs for the first alpha and doesn't continue to the following one.

Please assist heres how I would do it for just the first row, and then generalise it to repeat for all rows

``````#example made up data
bigm <- matrix (runif(40000), nrow = 4, ncol = 10000)

alpha<-seq(from=0,to=1,by=0.001)

just_the_first_row <- map_dbl(alpha,
~sum(bigm[1,] < .)/10000)

myf <- function(x) {map_dbl(alpha,
~sum(bigm[x,] < .)/10000)}

all_4_rows <-
map(1:4,
~myf(.))

> str(all_4_rows)
List of 4
\$ : num [1:1001] 0 0.0009 0.0018 0.0031 0.0046 0.0054 0.0061 0.0072 0.008 0.0091 ...
\$ : num [1:1001] 0 0.001 0.0023 0.0034 0.0045 0.0052 0.0066 0.0076 0.0082 0.0093 ...
\$ : num [1:1001] 0 0.0006 0.0012 0.0024 0.0036 0.0043 0.0056 0.0064 0.0065 0.0073 ...
\$ : num [1:1001] 0 0.0009 0.0019 0.0033 0.0045 0.0055 0.0073 0.0081 0.0091 0.0099 ...``````

I suggest using the ecdf function to calculate the empirical cumulative distribution of each row.

``````set.seed(1)
bigm <- matrix (runif(40000), nrow = 4, ncol = 10000)

ECDFs <- apply(bigm, MARGIN = 1, ecdf) #get an ecdf for each row, saved in a list.
plot(ECDFs[])
`````` ``````#what fraction of the values are <= 0.05 in row 1?
ECDFs[](0.05)
#>  0.0519

#Make a distribution peaked near 0.05
bigm2 <- matrix(rbeta(40000, shape1 = 5, shape2 = 95), nrow = 4)
hist(bigm2)
`````` ``````ECDFs2 <- apply(bigm2, MARGIN = 1, ecdf)
plot(ECDFs2[])
`````` ``````#what fraction of the values are <= 0.05 in row 1?
ECDFs2[](0.05)
#>  0.5548
``````

Created on 2020-06-18 by the reprex package (v0.3.0)

1 Like

Thank you! If I got it right, following your suggested code I get the values of raw 1 which are smaller than 0.05. I was requested to find for each alpha (0:1, 1000 values) how many values from the first matrix raw are smaller than the alpha. That's where I get stuck, I'm new in R and I'm not sure how to write a code to run over the values of the first raw for each alpha value.
At the end, what I need to get is a fraction of how many values from the 10,000 (first raw) are smaller than the specific alpha X 1,000 (one fraction for each alpha).

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.