For example, suppose that we observe x=115 and n=300, and we want to estimate p. Splus finds the confidence interval for p using the command prop.test():
> prop.test(115,300,conf.level=.95)$conf.int [1] 0.3285190 0.4411865 attr(, "conf.level"): [1] 0.95 > prop.test(115,300,conf.level=.95) 1-sample proportions test with continuity correction data: 115 out of 300, null probability 0.5 X-square = 15.87, df = 1, p-value = 0.0001 alternative hypothesis: true P(success) in Group 1 is not equal to 0.5 95 percent confidence interval: 0.3285190 0.4411865 sample estimates: prop'n in Group 1 0.3833333The function prop.test() uses the normal approximation to the binomial distribution with continuity correction. We obtain that the confidence interval for p is (0.3285190, 0.4411865).
However, other methods are possible. The following program finds the Clopper-Pearson confidence interval for a proportion p:
**********2a*************** x_115 n_300 alpha_.95 p_x/n dfr1_2*(n+1-x) dfr2_2*x q1_qf(.975,df1=dfr1,df2=dfr2) c2_(x/(x+((n+1-x)*q1))) dfr1_2*(x+1) dfr2_2*(n-x) q2_qf(.975,df1=dfr1,df2=dfr2) c2[2]_((x+1)*q2)/(n-x+((x+1)*q2)) ************************* > source("2a") > conf [1] 0.3280441 0.4409513 **************We obtain that the confidence interval for p is (0.3280441,0.4409513).
We can do a binomial test using the command:
binom.test(x, n, p=0.5, alternative="two.sided")
This commands test hypothesis about the parameter p in a Binomial(n,p) model given x, the number of successes out of n trials. The argument alternative has 3 options: "two.sided" (not equal to p), "less" (less than p) or "greater" (greater than p).
For example, if we do:
> binom.test(x,n,p=0.35, alternative="two.sided") Exact binomial test data: x out of n number of successes = 115, n = 300, p-value = 0.2501 alternative hypothesis: true p is not equal to 0.35We get that the p-value of the test is 0.2501. So, we conclude that the null hypothesis is true at the level of significance alpha=0.05.
Splus supplies the command binomial.sample.size() to find the sample size needed to attained a determined significance level and power.
>n1_binomial.sample.size(p=.3,p.alt=.4,power=.8,alpha=.05,alternative="greater") > n1 p.null p.alt delta alpha power n1 1 0.3 0.4 0.1 0.05 0.8 156However, better estimations are possible.