R Packages

Packages

Perhaps the coolest thing about R is the packages. So what are packages? To understand, let’s suppose your name is Greg and you start using R because it’s the cool thing to do. As you begin to test its functionality, you’re quite surprised that, in order to do a simple Z-test, you have to do a lot of programming:

  #### here's my sample data--Greg
x = c(7,11,9,12,13,8,12,12,10,9)  
  #### this is the value of mu. 
  #### Is my sample different from 7?--Greg
mu = 7 
sigma = 2 
xbar = mean(x)
     ###### compute the z score --Greg
z.score = (xbar-mu)/(sigma/sqrt(length(x))) 
    ##### look up p value
p.value = pnorm(z.score, lower.tail=FALSE)
    ##### make a conclusion
if (p.value < .05) {
    cat("Reject the Null")
}

Notice how Greg has been wise with his generous use of comments (recall comments are noted by anything that follows one or more pound signs). If Greg runs the code, he gets the following:

    #### here's my sample data--Greg
x = c(7,11,9,12,13,8,12,12,10,9)  
    #### this is the value of mu/stdev. 
    #### Is my sample different from 7?--Greg
mu = 7 
stdev = 2 
xbar = mean(x)
     ###### compute the z score --Greg
z.score = (xbar-mu)/(stdev/sqrt(length(x))) 
    ##### look up p value
p.value = pnorm(z.score, lower.tail=FALSE)
    ##### make a conclusion
if (p.value < .05) {
    cat("Reject the Null")
} else {
    cat("Fail to Reject the Null")
}
## Reject the Null

But that seems like a whole lotta work just to make a simple conclusion. He decides to write his own function (which we’ll get to later) so that he doesn’t have to write so many lines of code every time he wants to do a z-test. Now, all Greg has to do is two lines of code:

## Loading required package: TeachingDemos
x = c(7,11,9,12,13,8,12,12,10,9)
z.test(x, mu=9, stdev=2)
## 
##  One Sample z-test
## 
## data:  x
## z = 2.055, n = 10.000, Std. Dev. = 2.000, Std. Dev. of the sample
## mean = 0.632, p-value = 0.03983
## alternative hypothesis: true mean is not equal to 9
## 95 percent confidence interval:
##   9.06 11.54
## sample estimates:
## mean of x 
##      10.3

(Note: many other features have been added beyond a simple “reject the null.”) After writing the function, he’s quite proud of it and thinks, “I can’t be the first person who wanted to do a z-test in R. There’s got to be others out there. I bet they’d appreciate it if I made my package available to them!”

So Greg decides to make his z score function available online. When someone makes their function (or set of functions) available through R’s website, it is called a package. The cool thing about packages is that there are thousands upon thousands of R users, many of which write packages and publish them online. That means that R has a lot of added functionality that it otherwise would not because somebody somewhere decided to publish a package.

Installing a Package

As I said, there are lots of R packages available online. But the good news is that you don’t have to go searching for them to download them. Instead, you can download them right from R’s interface. Let’s go ahead and download a z score function from a package called . The author of this package is Greg Snow (no relation to our fictitious Greg of earlier). To download the package type the following

install.packages("TeachingDemos")

A new window will pop up asking you to pick a repository. A “repository” is just a fancy word for “a place to download the package from.” Then it will download the package and make it available for you to call. To call the package (i.e., to make it available for use), you simply type

require(TeachingDemos)

Now you’re ready to use the z.test function by Greg Snow. Thanks Greg!

Next, Practice using R.

One thought on “R Packages

  1. Pingback: Basic R Commands | Dustin Fife

Leave a Reply