模拟硬币袋,使用ggplot计算假阳性和假阴性的平均数

时间:2020-03-14 17:58:25

标签: r

问题

您好,我正在模拟一袋硬币,其中50%是公平的,而另50%在p = 0.5 + q的情况下对于q的某些值有偏差。所以我正在使用

之类的不同方法

a)没有更正

b)霍尔姆校正

c)BH校正以计算假阳性(FP)和 真阳性率(TP)

问题

我使用的代码将在下面发布,问题是当我运行绘图时,我的校正未在图形上显示数据,但是我的其他两种方法Holm校正和BH校正均未显示任何数据点。您能告诉我我要去哪里哪里以及如何解决吗?

我有一条错误消息说

NAUnknown or uninitialised column: 'rej_holm'.argument is not numeric or logical: returning

使用的代码

# simulating a mix of fair and biased coins function runs a 
# simulation of Ntosses of Nfair fair coins and Nbiased biased coins where 
# biased coins have success probability 0.5+ returns a dataframe containing the 
#true positive rate and false positive rate for each simulation

simulate_test_mixed_bag <- function(Nfair,Nbiased,q,Ntosses,alpha=0.05) {
    # simulate coin tosses
    faircoins <- rbinom(Nfair,Ntosses,0.5) #0.5 ensures 50% of coins being fair and 50% coins being biased
    biasedcoins <- rbinom(Nbiased,Ntosses,0.5+q)
    coins <- c(faircoins,biasedcoins)
    groundtruth <- c( rep(FALSE,Nfair), rep(TRUE,Nbiased) )
    cointests <- map_df(coins, function(x) tidy(binom.test(x,Ntosses,p=0.5)) ) %>%
        mutate(rej_uncorrected = p.value<alpha, 
        rej_holm = p.adjust(p.value,method='holm')<alpha,
        rej_BH = p.adjust(p.value,method='BH')<alpha)
    # return the mean of false positives and true positives
    df = cointests %>%
        mutate(groundtruth = if_else(groundtruth,'True Positive','False Positive')) %>%
        group_by(groundtruth) %>%
        summarise(uncorrected = mean(rej_uncorrected),
                  holm = mean(rej_holm),
                      BH = mean(rej_BH )) %>%
        mutate( q=q )
      return(df)
    }

    # run the simulation many times
    Nreps <-200
    Nfair <- 20
    Nbiased <- 20
    Ntosses <- 100
    qs <- c(0.025,0.05,0.075,0.1,0.15,0.2,0.25) #Simulation done to run different values of q
    alpha = 0.05
    # we use map_df to run simulations for each q (inner map_df), and repeat this Nreps time (outer map_df)
    reps <- map_df(1:Nreps, ~ map_df(qs, ~ simulate_test_mixed_bag(Nfair, Nbiased, .x, Ntosses, alpha)))
    # we want the mean TPR and FPR over repetitions, separately for each value of q
    results <- reps %>% group_by(groundtruth,q) %>% 
    summarise( uncorrected=mean(uncorrected), holm = mean(cointests$rej_holm), BH = mean(cointests$rej_BH ) )
    ggplot(results, aes(q, uncorrected)) +
      geom_line() +
      facet_wrap(~groundtruth, scales = "free")
    ggplot(results, aes(q, holm)) +
      geom_line() +
      facet_wrap(~groundtruth, scales = "free")
    ggplot(results, aes(q, BH)) +
      geom_line() +
      facet_wrap(~groundtruth, scales = "free") 

0 个答案:

没有答案