多次运行一次流行音乐模拟,并将每个结果存储在数据框中的新列中

时间:2019-03-21 18:50:58

标签: r loops dataframe nested-loops

我对两个等位基因进行了基本的Wright-Fisher模拟,效果很好,并生成了一个美观的图,显示了等位基因固定或死亡的情况与预期的一样。它将计算出的每一代导出到数据帧d中,因此我要掌握每一代的值。我要做的是将整个模拟运行20次,并将每个完整的模拟自动存储在新列中,因此我可以将它们全部绘制在带有颜色和所有好东西的ggplot图表上。我最感兴趣的是获得一个整洁的框架来为项目绘制漂亮的图,而不是极高的效率。

#Wright Fisher model Mk1

#Simulation Parameters
# n = pop.size
# f = frequency of focal allele
# x = number of focal allele, do not set by hand
# y = number of the other allele, do not set by hand
# g = generations desired
n = 200
f = 0.6
x = (n*f)
y = (n-x)
g = 200

#This creates a data frame of the correct size to store each generation

d = data.frame(f = rep(0,g))

#Creates the graph.
plot(1,0, type = "n", xlim = c(1,200), ylim = c(0,n),
     xlab = "Generation", ylab = "Frequency A")

#Creates the population, this model is limited to only two alleles, and can only plot one
alleles<- c(rep("A",x), rep("a",y))

#this is the loop that actually simulates the population
#It has code for plotting each generation on the graph as a point 
#Exports the number of focal allele A to the data frame
for (i in 1:g){ 
  alleles <- sample(alleles, n, replace = TRUE)
points(i, length(alleles[alleles=="A"]), pch = 19, col= "red")
F = sum(alleles == "A")
d[i, ] = c(F)
}

所以我想最后一次运行多次,并以某种方式存储每个完整的迭代。我知道我可以通过嵌套它来循环该函数,尽管这既快速又肮脏,但是这样做只能存储外循环最后一次迭代的值。

1 个答案:

答案 0 :(得分:0)

这里有很多改进的机会,但这应该可以帮助您前进。我仅显示五个模拟,但是您应该可以扩展。本质上,将大部分代码放在一个函数中,然后可以使用map包中的purrr函数,也可以使用replicate做一些事情:

library(tidyverse)

n = 200
f = 0.6
x = (n*f)
y = (n-x)
g = 200

d = data.frame(f = rep(0,g))

run_sim <- function() {
  alleles <- c(rep("A", x), rep("a", y))

  for (i in 1:g) { 
    alleles <- sample(alleles, n, replace = TRUE)
    cnt_A = sum(alleles == "A")
    d[i, ] = c(cnt_A)
  }

  return(d)
}

sims <- paste0("sim_", 1:5)

set.seed(4) # for reproducibility

sims %>%
  map_dfc(~ run_sim()) %>%
  set_names(sims) %>%
  gather(simulation, results) %>%
  group_by(simulation) %>%
  mutate(period = row_number()) %>%
  ggplot(., aes(x = period, y = results, group = simulation, color = simulation)) +
  geom_line()

reprex package(v0.2.1)于2019-03-21创建

注意:您也可以为run_simx(即y)的run_sim <- function(x, y) { ... }函数添加参数,这将使您探索其他可能性。