You may be able to figure out everything you need to know from my answer here: Simulation of logistic regression power analysis - designed experiments, which is quite comprehensive.
The basic procedure for simulating a power analysis is to:
- Simulate data according to your preferred scenario (the alternative hypothesis).
- Run the test you intend to use.
- Do this many times, and see what percentage of the time your results are significant.
- If you want to solve for the required $N$ to achieve a given power, do the above with different $N$s and find the one that yields the power you want.
In your case, you need to apply the binomial test with the null proportion set to $.01$.
Here's an example in R (note that your code had errors):
set.seed(8063)
p = 0.3
# from sample size 1:100, I generated 1000 binomial random variable
k = matrix(NA, nrow=1000, ncol=100)
for(n in 1:100){
k[,n] = rbinom(1000, n, p)
}
p.mat = matrix(NA, nrow=1000, ncol=100)
for(i in 1:1000){
for(n in 1:100){
p.mat[i,n] = binom.test(x=k[i,n], n=n, p=0.01)$p.value
}
}
power = c()
power = apply(p.mat, 2, FUN=function(x){ mean(x<.05) })
min(which(power>.8))
# [1] 5
power[4]
# [1] 0.763
power[5]
# [1] 0.82
Brute force search:
tl = NULL
tl[paste("n", 1:10, sep="")] = list(NULL)
pl = tl
for(n in 1:10){
tl[[n]] = 0:n
for(h in 0:n){
pl[[n]][h+1] = binom.test(tl[[n]][h+1], n, 0.01, "g")$p.value
}
}
msig = lapply(pl, function(x){ min(which(x<.05))-1 })
pow = c()
for(n in 1:10){
pow[n] = 1-sum(dbinom(0:(msig[[n]]-1), n, .3))
}
names(pow) = paste("n", 1:10, sep=" = ")
min(which(pow>.8))
# [1] 5
pow[4:5]
# n = 4 n = 5
# 0.75990 0.83193