R 计算&;在模拟中绘制每一代的自举置信区间

R 计算&;在模拟中绘制每一代的自举置信区间,r,simulation,confidence-interval,R,Simulation,Confidence Interval,我在R # Population size N<-5000 # Number with focal allele X1<-(N/2) # Number of generations ngens<-(2000) # Number of replicates nreps<-10 # Drift function drift <- function(N, X1, ngens, nreps) { # Makes a matrix of NA's of nreps colu

我在R

# Population size
N<-5000
# Number with focal allele
X1<-(N/2)
# Number of generations
ngens<-(2000)
# Number of replicates
nreps<-10

# Drift function
drift <- function(N, X1, ngens, nreps) {
# Makes a matrix of NA's of nreps columns, and ngen rows
       p <- matrix(NA, nrow=ngens, ncol=nreps)
  # Set base population
       p[1,] <- X1/N
  # Repetitive sampling function, each generation sample 10 times from the generation before (gen-1)  
       for(gen in 2:ngens)
         p[gen,] <- rbinom(n=nreps, size=N, prob=p[gen-1,]) / N
       p
}
# Run function "drift" & output as data frame
p <- data.frame(drift(N, X1, ngens, nreps))
# Plot
matplot(p, type="l", ylim=c(0,1), lty=1, xlab="Generations", ylab="Proportion Focal",col="grey")
# Mean value
p$mean<-apply(p[,c(1:10)],1,mean)
matplot(p$mean, type="l", ylim=c(0,1), lty=1, xlab="Generations", ylab="Proportion Focal",col="black",add=T)
#人口规模

如果你已经解决了你的问题,这可以作为答案。 启动功能:

    library(boot)

    myBootFunction<-function(x){
        b <- boot(x, function(u,i) mean(u[i]), R = 999)
        boot.ci(b, type = c("norm", "basic", "perc"))
    }
库(启动)

MyBootFunction确实有帮助吗?对于引导,是的,但不是像我所描述的那样实际实现它-即,一个循环,它对每一行执行一次(每一代模拟在我的df中创建一行),然后将上下估计值粘贴为columnsok。这是一个粗略的想法。将引导代码写入函数中。然后在
sapply
语句中调用该函数。例如,
last\u output当我复制您的数据帧
p
时,所有列的第一行都是0.5。这是第一行的警告。剩下的应该计算一下(至少对我来说是有效的)。由于所有的值都是相同的,所以第一行没有置信区间。我在上面的脚本中添加了一点,但仍然会出现错误/如上所述运行失败。
    library(boot)

    myBootFunction<-function(x){
        b <- boot(x, function(u,i) mean(u[i]), R = 999)
        boot.ci(b, type = c("norm", "basic", "perc"))
    }
meanList<-apply(p[,c(1:10)],1,function(x)myBootFunction(x))