matrix - Compute p-values across all columns of (possibly large) matrices in R -


is there more efficient/faster way compare 2 matrices (column columns) , compute p-values using t-test no difference in means (eventually switching chisq.test when necessary)?

here solution:

## generate fake data (e.g., treatment , control data) z0 <- matrix(rnorm(100),10,10) z1 <- matrix(rnorm(100, mean=1.1, sd=2),10,10)  ## function compare columns (bloody loop) compare.matrix <- function(z0, z1){   pval <- numeric(ncol(z0)) ## initialize    for(i in 1:ncol(z0)){ ## compare columns     pval[i] <- t.test(z1[, i], z0[, i])$p.value      ## if var categorical, switch test type     if ( length(unique(z1[,i]))==2){       index <- c(rep(0, nrow(z0)), rep(1, nrow(z1)))       xx <- c(z0[,i], z1[,i])       pval[i] <- chisq.test(table(xx, index), simulate.p.value=true)$p.value           }   }   return(pval)   } compare.matrix(z0, z1) 

here's 1 way using dplyr. better combine first 3 lines single step if you've got large matrices, separated them clarity. think chi-squared case simple extension.

z0_melt = melt(z0, value.name='z0')[,c('var2','z0')] z1_melt = melt(z1, value.name='z1')[,c('var2','z1')] all_df = merge(z0_melt, z1_melt)  library(dplyr)  all_df %>%   group_by(var2) %>%   summarize(p = t.test(z0, z1)$p.value) 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -