【问题标题】:Given a vector, apply a function to each element of another vector给定一个向量,对另一个向量的每个元素应用一个函数
【发布时间】:2017-03-07 12:30:16
【问题描述】:

问题 1

我有一个接收pn 的函数。我需要绘制p=c(0.05,0.1,0.25,0.5,0.9,0.95)n=c(5,10,30,50,100) 的每个组合。所以总共 6*5=30 个地块。我尝试使用mapply,它返回如下所示的图(编辑:上传 img 的声誉太低,但我显示我只得到 6 个图)。根据我的ablines 所在的位置,我认为它正在尝试p 的所有值而不是n 的单个值!

问题 2

我正在尝试使用paste0()np 的值放入绘图中,以跟踪哪个绘图属于np 的哪个组合,但是当我使用mapply(它适用于我手动绘制的单个图表)

ci.auto <- function(p,n,alpha){

    # Repeat the process nsim times
    nsim <- 10000
    ci.mat <- replicate(nsim, conf.int(p, n, alpha))

    # Graphing the first 100 intervals

    matplot(rbind(1:100, 1:100), ci.mat[, 1:100], type = "l", lty = 1, 
            xlab = paste0("sample number n=",n), ylab = "confidence interval")
    abline(h = p)
    text(2.5, 0.8, paste0("p=",p), col = "red")

    # Proportion of times the interval is correct

    mean( (p >= ci.mat[1,])*(p <= ci.mat[2,]) )
}

par(mfrow = c(5,6))
mapply(ci.auto, p = c(0.05,0.1,0.25,0.5,0.9,0.95), n = c(5,10,30,50,100), alpha)

【问题讨论】:

  • 0.05 只是一个奇异值。
  • mapply 并行迭代,因此不会创建其他组合。使用expand.gridrep 生成它们,然后将组合传递给mapply

标签: r


【解决方案1】:
df<-expand.grid(p,n)
mapply(Fun = ci.auto, df$Var1,df$Var2,alpha)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多