I am trying to create a function that performs the sign test. The function should take in vector x and median m and returns a p-value defined by min(1, 2*min(P{N ≤ i}, P{N ≥ i})), where N~Bin(n, 0.5), n is the amount of values which are not equal to m, and i is the amount of values in n which are smaller than m.
Here's what I've gotten so far:
test<-function(x,m) {
n<-length(x[x!=m])
i<-length(n[n<m])
min(1,c(pbinom(q=i,size=n,p=0.5),1-pbinom(q=i,size=n,p=0.5)))
}
However, when I test it out with given values, it gives the wrong answer:
test(x=1:3, m=2)
[1] 0.25
test(x = 1:5, m = 2)
[1] 0.0625
The right answers should be 1 and 0.625. I don't know which step I have done wrong.