随机化数据帧的子集并复制函数以保存每个复制的结果

时间:2015-05-07 12:12:49

标签: r permutation plyr dplyr replicate

我有一个像这样的数据帧(df2):

locus transect  fq  d   
Locus_1 A 0.000 20
Locus_1 A 0.000 35    
Locus_1 A 0.000 50
Locus_2 A 0.200 20
Locus_2 A 0.083 35
Locus_2 A 0.125 50
Locus_3 A 0.134 20   
Locus_3 A 0.208 35
Locus_3 A 0.218 50
Locus_4 A 0.000 20
Locus_4 A 0.000 35
Locus_4 A 0.000 50
Locus_5 A 0.100 20
Locus_5 A 0.000 35
Locus_5 A 0.038 50    ...

基本上每个基因座沿着与中心不同距离的样带被采样三次。有成千上万的基因座。 从这个数据集中,我计算出频率和距离之间的相关性。

接下来的步骤是:

  • 随机化每个基因座的位置(因此,前三行,第二组三行等),计算新的相关性。基本上,我想在每个基因座之间改变d值(20-35-50)。 离子
  • 这样做1000次
  • 保存每个复制的结果

我主要使用Plyrdplyr

这是我提出的代码:

df3 <- group_by(df2, transect, locus) #setting up groups to which apply functions


data <- replicate(1000, {
  test <- sample_n(df3, 3, replace=F) #shuffle by group
  Rho <- ddply(test, .(transect, locus), summarise, corr= cor(fq, d, method = "spearman")) #calculate correlation
  Rho[is.na(Rho)] <- 0 #replacing missing values with zero
  Rho_mean_bylocus <- ddply(Rho, .(locus), summarise, mean=mean(corr))  #average correlation over transect
  }, simplify = TRUE)

结果如下:

 [,1]        [,2]        [,3]        [,4]       
locus factor,978  factor,978  factor,978  factor,978 
mean  Numeric,978 Numeric,978 Numeric,978 Numeric,978
       [,5]        [,6]        [,7]        [,8]       
locus factor,978  factor,978  factor,978  factor,978 
mean  Numeric,978 Numeric,978 Numeric,978 Numeric,978
      [,9]        [,10]      
locus factor,978  factor,978 
mean  Numeric,978 Numeric,978

(我有978个位点)。

我尝试在函数

中嵌入replicate()
 rand.rho <- function(x) {  #I have tried also without using a function, but still does not work

  data <- replicate(1000, {
  test <- sample_n(df3, 3, replace=F) #shuffle
  Rho <- ddply(test, .(transect, locus), summarise, corr= cor(fq, d, method = "spearman")) #calculate correlation
  Rho[is.na(Rho)] <- 0 #replacing missing values with zero
  Rho_mean_bylocus <- ddply(Rho, .(locus), summarise, mean=mean(corr)) #average correlation over transect
  }, simplify = TRUE)

df4 <- rand.rho(df3)

但是我收到了错误:

Error in list_to_dataframe(res, attr(.data, "split_labels"), .id, id_as_factor) : 
Results must be all atomic, or all data frames
In addition: There were 50 or more warnings (use warnings() to see the first 50)

我很茫然。

我已经在这里寻找其他答案,并尝试实施该建议,但它仍然无效。

有什么建议吗?

0 个答案:

没有答案