Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2 Lab2: Probability Sampling and Basic Plotting M. Geor...
1 downloads 2 Views 229KB Size
Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

Lab2: Probability Sampling and Basic Plotting M. George Akritas

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

Outline Review of Probability Sampling A Special PMF Combinations in R The Binomial Theorem and PMF Sampling from the Binomial PMF Basic Plotting in R The Lab Assignment # 2

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

Definition The type of random sampling where different outcomes have different probability of occurring is referred to as probability sampling. Probability sampling is always sampling with replacement.

Example Consider rolling a die twice and record the number of times 6 occurs. Describe an equivalent probability sampling experiment. Solution. The experiment that records the number of 6s can be thought of as a probability sampling experiment from the sample space population S = {0, 1, 2}, with probabilities P(0) = 25/36, P(1) = 10/36 and P(2) = 1/36. M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

Definition A list of the numerical outcomes in the sample space population and their associated probabilities is be called a probability mass function, or pmf for short. The pmf of the previous example is x prob.

0 25/36

1 10/36

2 . 1/36

Probability sampling is also called sampling from a pmf.

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

I

Factorials in R: I I

I

I

Combinations in R The Binomial Theorem and PMF Sampling from the Binomial PMF

x! can be given by ”factorial(x)” or ”gamma(x+1)” Try ”factorial(3)” and ”gamma(4)”. Also ”factorial(4)” and ”gamma(5)”. Try also ”factorial(3.5)” and ”gamma(3.5)”.

Combinations in R: I

I

I

 For the number of combinations kn try  ”factorial(5)/(factorial(3)*factorial(5-3))” for 53 , or  ”factorial(10)/(factorial(3)*factorial(10-3))” for 10 3 For the actual combinations (though this is not needed as often) try ”combn(1:5,3)” or ”combn(1:10,3)” Note: ”1:5” gives the same result as ”seq(1:5)”, or ”seq(1,5)” or ”seq(1,5,1)”  Can also do ”length(combn(1:5,3))/3” for 53 .

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

Combinations in R The Binomial Theorem and PMF Sampling from the Binomial PMF

Theorem (The Binomial Theorem) (p + q)

n

      n n 0 n n−1 1 n 0 n = p q + p q + ··· + p q 0 1 n n   n   X n n−k k X n k n−k = p q = p q k k k=0

I

I

k=0

If p + q = 1, and p, q > 0, we get a p.m.f. on S = {0, 1, . . . , n} with P(k) = kn p k q n−k . This is called the Binomial p.m.f.  If p = q = 0.5, P(k) = kn 0.5n is the probability of having k heads in n flips of a fair coin. M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

I

The Binomial p.m.f. I

I

I

Combinations in R The Binomial Theorem and PMF Sampling from the Binomial PMF

Try ”dbinom(0:10, 10, 0.5)” for all probabilities P(k), k = 0, . . . 10. Try also ”dbinom(4, 10, 0.5)” for P(4). Verify that P(4) obtained above can also be obtained from: ”factorial(10)/(factorial(4)*factorial(10-4))*0.5**10”

Probability sampling using the Binomial p.m.f. I

I

I

Try ”sample(0:10,1000,TRUE,dbinom(0:10, 10, 0.5))” or ”p=dbinom(0:10, 10, 0.5), sample(0:10,1000,TRUE,p)”. Try it again using object x to store the sample: ”x=sample(0:10,1000,TRUE,dbinom(0:10, 10, 0.5))” For a summary of the sample do: ”table(x)”, or ”table(x)/1000” Compare with ”dbinom(0:10, 10, 0.5)”

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

I

Plotting a p.m.f. I

I I

I

I

Try ”plot(0:10,dbinom(0:10, 10, 0.5))” (which is the same as ”plot(0:10,dbinom(0:10, 10, 0.5), pch=1)”, or ”plot(0:10,dbinom(0:10, 10, 0.5), pch=4)”. Add color: Try plot(0:10,dbinom(0:10, 10, 0.5), pch=4,col=2) Customize labels: plot(0:10,dbinom(0:10, 10, 0.5), pch=1,col=2, xlab=”Sample Space”, ylab=”Binomial Probabilities”) Connecting the dots: Try ”lines(0:10,dbinom(0:10, 10, 0.5))” and also ”lines(0:10,dbinom(0:10, 10, 0.5), col=3)”

Bar graph of a p.m.f.: I I I

Try ”p=dbinom(0:10, 10, 0.5), barplot(p)” Customize the axes: barplot(p,xlim=c(0,12),ylim=c(0,0.25)) Add color: barplot(p,xlim=c(0,12),ylim=c(0,0.25),col=”green”)

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

I

Histogram of Empirical Probabilities: I I

I

Try ”hist(x,seq(-0.5,10.5,1))” Add color ”hist(x,seq(-0.5,10.5,1),col=5)”

Check out the site: http://www.harding.edu/fmccown/r/ for additional (still basic) plotting examples.

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

Do computer activities 1,2,3 in Section 2.8. Email your results, together with the plots (you can save them as pdf’s for example), to the TA by Friday, 16th of September.

M. George Akritas

Lab2: Probability Sampling and Basic Plotting

Outline Review of Probability Sampling A Special PMF Basic Plotting in R The Lab Assignment # 2

I

Go to previous lab http://www.stat.psu.edu/~mga/401/ course.info/b.lab-Ch1.pdf

I

Go to next lab http://www.stat.psu.edu/~mga/401/ course.info/b.lab-Ch3.pdf

I

Go to the Stat 401 home page http://www.stat.psu.edu/~mga/401/course.info/

I

http://www.stat.psu.edu/~mga http://www.google.com

I

M. George Akritas

Lab2: Probability Sampling and Basic Plotting