Question: Create a function in R that runs a z-test of hypothesis for a proportion. structure of a function in R: myfunction

Create a function in R that runs a z-test of hypothesis for a proportion.

structure of a function in R:

myfunction<-function(argument1, argument2,...){ statements results return(results) }

Structure of an if loop:

if( condition){ } else

How to include an error message: stop("message") The message needs to be in quotations.

Arguments of your function: X (the count, that is the number of successes in the sample) n (the sample size) pnull (the value of the proportion stated in the null hypothesis hypothesis = "notequal" (when you run the test this will take the values: "notequal","larger","less" according to the appropriate alternative hypothesis.

Inside, the function needs to compute: phat (the estimate for the proportion) se (the estandard error corresponding to pnull) z (the statistic of the test) pval (the p-value corresponding the the given alternative hypothesis)

Once you have all of that, you are going to create a vector with your results. Include these lines at the end of your function:

print(sprintf("Test of Hypothesis for a Proportion: H0: p = %g vs Ha: p %s %g ", pnull,hypothesis,pnull)) results<-c(test.statistic=z,p.value=pval) return(results)

Test your function. Make sure that for the following examples gives you the right confidence interval:

1) Test of hypothesis for H0: p=.5 Ha: p .5 with X=35, n=50 should give you:

"Test of Hypothesis for a Proportion: H0: p = 0.5 Ha: p notequal 0.5 " test.statistic p.value 2.828427125 0.004677735

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!