Вопрос
Здравствуйте, я имитирую мешок с монетами, где 50% справедливо, а другие 50% смещены с p = 0,5 + q для некоторых значений q. Поэтому я использую различные методы, такие как
а) без коррекции
б) коррекция Холма
c) коррекция ЧД для расчета среднего ложноположительного значения (FP) и среднее значение истинного положительного (TP)
Задача
Я использую код, который я опубликую ниже, проблема в том, что когда я запускаю графики, мои исправления не показывают данные на графике, но мои два других метода коррекции Холма и коррекции ЧД, не показывают никаких точек данных. Не могли бы вы сказать мне, где я иду не так и как это исправить?
У меня есть одно сообщение об ошибке, которое говорит
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")
![](https://i.stack.imgur.com/SfKgZ.png)