Lab1: Introduction to R


Getting Started with R

Click on the RStudio icon to get started! There are lots of tutorials available

For R HELP, for example to get help about the function rnorm ,

> help(rnorm)

RStudio has a help window!


Example 1: Let the games begin.

Generate a sample of 100 N(0,1) random variables.

> help(rnorm)

> rnorm(100)

What happened?

Now, let's try:

> temp <- rnorm(100)

What is in the object temp?

What is the length of the oject temp?

Make a informative plot of temp?

If this were part of a homework problem, you might want to copy and paste the R code and plot into a Word Document?
Say what? Let's try Sweave today after we go through R Lab.

Try it!


Example 2: The Binomial Distribution

For the binomial distribution, these functions are pbinom, qbinom, dbinom, and rbinom.

For help, use the help function on one of the four functions above.

Problem: Dr. Dribble has a probability of .8 of making free throws each time he shoots. What is the probability of him making at least 8 out of 10 free throws?

Assume his shots are independent of each other.

Let X be the number of free throws made. X is has a Binomial(10, 0.8) distribution. What does this look like?

Let's generate a large number of Binomial(n,p) random variables and look at the histogram.

> set.seed(1)
> bindat <- rbinom(n=10000, size=10, prob=0.8)
> hist(bindat, breaks=seq(2, 10, 1), freq=F)
We want to find the probability of making at least 8 out of 10 free throws.

Let's calculate P(X <= 7) when X is has the Binomial(10, 0.8) distribution using the pbinom function.

Now, to answer the question: P(X >= 8) = 1 - P(X <= 7) = 1-0.3222005 = 0.6777995


Example 3: Binomial Test

> help(binom.test)

Class example: Binom(25,0.4). If B=15, what do you conclude?

> binom.test(15,25,0.4, alternative="greater")

Calculate P(B >= 15) in R. For the signficance level, P(B >= 14)?

What about CI's for this test? (see help function)



NOW FOR SWEAVE!

Intro to Sweave:

Let's look at Example/Problem from above Dr. Dribble:
Suppose the HW problem wants you calculate the probabilty AND also include a hi\ stogram.

Here is the code that we will need to put in a Sweave document: drdribble

Note: Sweave documents have a .Rnw extenstion!

We will need to select R Sweave under the File, then New File options.

This automatically adds the lines:
\SweaveOpts{concordance=TRUE}

Note: When you compile the pdf in the Lab, you will probably get an error!
Your pdf document should look like: STAT672HW0.pdf

Markdown or LaTeX?


Due to popular demand: R Markdown

Here is an Example of a .Rmd file Lab1.Rmd Thanks Jonathan!



Intro to R and for more practice with R

Try going through the following Intro. Lessons. The # sign is a comment, so you can copy and paste.

  • R vectors, lists and functions
  • R arithmetic and logical operators
  • R simple functions
  • R univariate random variable generators



    EXTRA EXAMPLES - BELOW!

    Example: Let's make some more plots in R!

    Let's look at plotting different distributions

    In order to run the function, you will need:

    demo.gamma2.r

    There are 2 ways to get these functions for your very own.

    1. Copy and paste the code into the R command window each function.
    2. Use the R source command which sources the code in the file:

    > source("https://edoras.sdsu.edu/~babailey/stat575/demo.gamma2.r") \

    Now, to run the demo:

    > demo.gamma2()