|
| 1 | +sim_fit = function(n, p_old = 0.10, p_new = 0.25) { |
| 2 | + # create the data |
| 3 | + dead_old = rbinom(n, size = 1, prob = p_old) |
| 4 | + dead_new = rbinom(n, size = 1, prob = p_new) |
| 5 | + # create the predictor variable |
| 6 | + method = rep(c("old", "new"), each = n) |
| 7 | + # create a data.frame to pass to glm |
| 8 | + df = data.frame(dead = c(dead_old, dead_new), method = method) |
| 9 | + # relevel so old is the reference |
| 10 | + df$method = relevel(factor(df$method), ref = "old") |
| 11 | + # fit the model |
| 12 | + fit = glm(dead ~ method, data = df, family = binomial) |
| 13 | + # extract the p-value |
| 14 | + pval = summary(fit)$coef[2,4] |
| 15 | + # determine if it was found to be significant |
| 16 | + sig_pval = pval < 0.05 |
| 17 | + # obtain the estimated mortality rate for the new method |
| 18 | + p_new_est = predict(fit, data.frame(method = c("new")), |
| 19 | + type = "response") |
| 20 | + |
| 21 | + # determine if it is +/- 5% from the true value |
| 22 | + prc_est = p_new_est >= (p_new - 0.05) & p_new_est <= (p_new + 0.05) |
| 23 | + # return a vector with these two elements |
| 24 | + c(sig_pval = sig_pval, prc_est = unname(prc_est)) |
| 25 | +} |
| 26 | + |
| 27 | +# containers: |
| 28 | +out_sig = matrix(NA, I, N) # matrix with I rows and N columns |
| 29 | +out_prc = matrix(NA, I, N) # matrix with I rows and N columns |
| 30 | +for (n in 1:N) { |
| 31 | + for (i in 1:I) { |
| 32 | + tmp = sim_fit(n = n_try[n]) # run sim |
| 33 | + out_sig[i,n] = tmp["sig_pval"] # extract and store significance metric |
| 34 | + out_prc[i,n] = tmp["prc_est"] # extract and store precision metric |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +par(mfrow = c(1,2), mar = c(4,4,1,0)) |
| 39 | +plot(apply(out_sig, 2, mean) ~ n_try, type = "l", |
| 40 | + xlab = "Tagged Fish per Treatment", |
| 41 | + ylab = "Probability of Finding Effect (Power)") |
| 42 | +plot(apply(out_prc, 2, mean) ~ n_try, type = "l", |
| 43 | + xlab = "Tagged Fish per Treatment", |
| 44 | + ylab = "Probability of a Precise Estimate") |
| 45 | + |
0 commit comments