Cheap Wholesale NFL Jerseys - Z Testing in R

By Joseph Schmuller

An R function called z.test() would be great for doing the kind of testing in which you use z-scores in the hypothesis test. One problem: That function does not exist in base R. Although you can find one in other packages, it’s easy enough to create one and learn a bit about R programming in the process.

The function will work like this:

> <- c(100,101,104,109,125,116,105,108,110)

> z.test(,100,15)

z = 1.733

one-tailed probability = 0.042

two-tailed probability = 0.084

Begin by creating the function name and its arguments:

z.test = function(x,mu,popvar){

The first argument is the vector of data, the second is the population mean, and the third is the population variance. The left curly bracket signifies that the remainder of the code is what happens inside the function.

Next, create a vector that will hold the one-tailed probability of the z-score you’ll calculate:

one.tail.p <- NULL

Then you calculate the z-score and round it to three decimal places:

z.score <- round((mean(x)-mu)/(popvar/sqrt(length(x))),3)

Without the rounding, R might calculate many decimal places, and the output would look messy.

Finally, you calculate the one-tailed probability (the proportion of area beyond the calculated z-score), and again round to three decimal places:

one.tail.p <- round(pnorm(abs(z.score),lower.tail = FALSE),3)

Why put abs() (absolute value) in the argument to pnorm? Remember that an alternative hypothesis can specify a value below the mean, and the data might result in a negative z-score.

The next order of business is to set up the output display. For this, you use the cat() function. The name cat is short for concatenate and print, which is exactly what you want you to do here: Concatenate (put together) strings (like one-tailed probability =) with expressions (like one.tail.p), and then show that whole thing onscreen. You also want you to start a new line for each concatenation, and \n is R’s way of making that happen.

Here’s the cat statement:

cat(" z =",z.score,"\n",
"one-tailed probability =", one.tail.p,"\n",
"two-tailed probability =", 2*one.tail.p )}

The space between the left quote and z lines up the first line with the next two onscreen. The right curly bracket closes off the function.

Here it is, all together:

z.test = function(x,mu,popvar){

one.tail.p <- NULL

z.score <- round((mean(x)-mu)/(popvar/sqrt(length(x))),3)

one.tail.p <- round(pnorm(abs(z.score),lower.tail = FALSE),3)

cat(" z =",z.score,"\n",

"one-tailed probability =", one.tail.p,"\n",

"two-tailed probability =", 2*one.tail.p )}

<tr id="WQFImOv"></tr>
<tr id="WQFImOv"><optgroup id="WQFImOv"></optgroup></tr>
<acronym id="WQFImOv"></acronym><acronym id="WQFImOv"></acronym>
<acronym id="WQFImOv"><small id="WQFImOv"></small></acronym>
<sup id="WQFImOv"><center id="WQFImOv"></center></sup>
<rt id="WQFImOv"><optgroup id="WQFImOv"></optgroup></rt>
<rt id="WQFImOv"></rt>
<acronym id="WQFImOv"></acronym><rt id="WQFImOv"><optgroup id="WQFImOv"></optgroup></rt><acronym id="WQFImOv"></acronym>
<acronym id="WQFImOv"><optgroup id="WQFImOv"></optgroup></acronym>
  • 6308961962 2018-02-25
  • 3055971961 2018-02-25
  • 7451961960 2018-02-25
  • 2458921959 2018-02-25
  • 1955001958 2018-02-25
  • 6433701957 2018-02-25
  • 744321956 2018-02-25
  • 3545931955 2018-02-24
  • 4244531954 2018-02-24
  • 7574311953 2018-02-24
  • 5343791952 2018-02-24
  • 3464581951 2018-02-24
  • 5317271950 2018-02-24
  • 7054671949 2018-02-24
  • 2122831948 2018-02-24
  • 9863161947 2018-02-24
  • 3003541946 2018-02-24
  • 700921945 2018-02-24
  • 635621944 2018-02-23
  • 501801943 2018-02-23
  • cheap jerseys | wholesale jerseys |