给定一个向量,将函数应用于另一个向量的每个元素

时间:2017-03-07 03:41:52

标签: r

问题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

我正在尝试使用nppaste0()的值放入图中,以跟踪哪个图块属于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)

1 个答案:

答案 0 :(得分:0)

df<-expand.grid(p,n)
mapply(Fun = ci.auto, df$Var1,df$Var2,alpha)