首页 > 解决方案 > R中的P值排列

问题描述

我在 r 中很新

我在 R 中执行置换测试以确定某些 SNP 的出现百分比是否是偶然的 我的数据集一个 52K 值的向量我通过 Loop 进行测试,如下所示:

R1_H <- R1[,12] #extract the vector from a dataframe

niter=100000       #set the number of iterations
out <- rep(0,length(R1_H))

for (i in 1:niter){
out = out + (R1_H <= sample(R1_H)) #compare my ocurrence against a 
                                 #sample of the entire population
}

pvalue=out/niter #determine the pvalue
R1$pvalueF = pvalue #print the pvalue

问题是这种方式极其缓慢且耗费资源。有人认为更有效的方法吗?非常感谢

标签: rloopspermutation

解决方案


我不完全确定你在做什么。但有几点。R 具有replicate专门为此类事物设计的内置功能。您可以使用的一种选择是:

my_vector <- replicate(niter, 
                   expr = (R1_H <= sample(R1_H, replace = T)))

这将重复多次exprniter在这种情况下,它将进行替换采样并返回一个矩阵,其中包含 R1_H 和 10k 列中的数据点数。然后你可以做类似的事情:

mean(colMeans(my_vector))

获取您尝试报告的“pvalue”。


推荐阅读