SlideShare a Scribd company logo
1 of 30
Download to read offline
Stat310              Simulation


                            Hadley Wickham
Tuesday, 16 February 2010
Test

                    • Don’t forget it’s on Tuesday
                    • All learning objectives, lectures &
                      homework model answers up on site
                    • Help sessions Tuesday & Wednesday
                      4-5 pm



Tuesday, 16 February 2010
1. Valentine’s day & statistics
                2. Normal distribution
                3. Intro to R
                4. Generating random numbers




Tuesday, 16 February 2010
http://xkcd.com/55/
Tuesday, 16 February 2010
You are perfect; I'd make no substitutions
           You remind me of my favorite distributions
           With a shape and a scale that I find reliable
           You’re as comforting as a two parameter Weibull
           When I ask you a question and you answer truly
           You speak as clearly as a draw from a Bernoulli
           Your love of adventure is most influential
           Just like the constant hazard of an exponential.
           With so many moments, all full of fun,
           You always integrate perfectly to one



Tuesday, 16 February 2010
Standard normal
                    For the normal distribution, we can
                    convert any normally distributed rv to a
                    standard normal by subtracting off the
                    mean and dividing by the standard
                    deviation.
                    This means we only need one table for
                    any possible normal distribution.


Tuesday, 16 February 2010
Using the tables
                      Column + row = z
                      Find: Φ(2.94), Φ(-1), Φ(0.01), Φ(4)


                      Can also use in reverse: For what value
                      of z is P(Z < z) = 0.90 ? i.e. What is Φ-1(0.90)?
                      Find: Φ-1(0.1), Φ-1(0.5), Φ-1(0.65), Φ-1(1)



Tuesday, 16 February 2010
Example
                    The time it takes me to drive to school is
                    normally distributed with mean 20 and
                    standard deviation 3.
                    What is the probability it takes me more
                    than 20 minutes to drive to school?
                    What time should I leave so that I have
                    95% chance of getting to class by 1pm?


Tuesday, 16 February 2010
Example

                    What’s the probability I take a negative
                    amount of time to get to school?
                    Can the the distribution of my driving time
                    really be normal? Is that a problem?




Tuesday, 16 February 2010
P (Z < z) = Φ(z)
                            Φ(−z) = 1 − Φ(z)

                      P (−1 < Z < 1) = 0.68
                      P (−2 < Z < 2) = 0.95
                      P (−3 < Z < 3) = 0.998
Tuesday, 16 February 2010
R



Tuesday, 16 February 2010
Intro to R
                    Open-source statistical programming
                    environment
                    http://www.r-project.org
                    Installed on all lab computers
                    We’re going to use it as a calculator and
                    for some basic simulations. Extra info
                    available on the website.


Tuesday, 16 February 2010
> 3 + 7
     [1] 10

     > sqrt(100) + 3 ^ 2
     [1] 19

     > 1:10
      [1] 1                 2   3   4   5    6   7    8   9 10

     > (1:10) ^ 2
      [1]   1   4                   9   16    25     36   49   64   81 100



Tuesday, 16 February 2010
# From now on I'll just show the code
     # <- means that this is a comment and will be
     # ignored by R.

     sin(2 * pi)
     cos(pi)

     exp(1:10)
     log(1:10)
     log(exp(1:10))

     sqrt(2)^2 - 2


Tuesday, 16 February 2010
Your turn


                    Why does sqrt(2)^2 - 2 not equal 0?




Tuesday, 16 February 2010
x <- c(1, 5, 3, 9, 2.4)

     sum(x)
     mean(x)
     var(x)
     sd(x)




Tuesday, 16 February 2010
Probabilities
                    Can use R instead of tables of
                    probabilities. (This of course is what all
                    modern statisticians do)
                    {p, d, q} x {binom, pois, unif, exp, gamma,
                    norm}
                    d = pdf/pmf, p = cdf, q = inverse cdf



Tuesday, 16 February 2010
# X ~ Binom(n = 100, p = 0.35)
     # P(X = 0)
     dbinom(0, size = 100, prob = 0.35)
     # P(X = 35)
     dbinom(35, size = 100, prob = 0.35)

     # P(X < 10)
     pbinom(10, size = 100, prob = 0.35)
     # P(X > 10)
     1 - pbinom(10, size = 100, prob = 0.35)
     # P(X < 80)
     pbinom(80, size = 100, prob = 0.35)
     # P(30 <= X < 40)
     pbinom(40, size = 100, prob = 0.35) -
       pbinom(30, size = 100, prob = 0.35)
Tuesday, 16 February 2010
# Probability of getting an odd number of heads
     # when flipping a fair coin 100 times
     sum(dbinom(1 + 2 * (0:49), size = 10, prob = 0.5))

     # To figure it out, pull out the code from the
     # outside in
     0:49
     1 + 2 * (0:49)
     dbinom(1 + 2 * (0:49), size = 10, prob = 0.5)
     sum(dbinom(1 + 2 * (0:49), size = 10, prob = 0.5))




Tuesday, 16 February 2010
Suffix     Distribution   Parameters
                   binom      Binomial      size, prob

                     pois     Poisson        lambda

                     unif     Uniform        min, max

                       exp   Exponential       rate

                   gamma       Gamma        shape, rate

                     norm      Normal        mean, sd
Tuesday, 16 February 2010
Your turn

                    X ~ Normal(20, 9)
                    What R code would you type to calculate
                    P(17 < X < 23)?




Tuesday, 16 February 2010
Random number
                              generation


Tuesday, 16 February 2010
Uniform to any rv
                    IF
                    Y ~ Uniform(0, 1)
                    F a cdf
                    THEN
                    X=      F -1(Y)   is a rv with cdf F(x)
                    (Assume F strictly increasing for simplicity)



Tuesday, 16 February 2010
Source of uniform
                               numbers?
      http://www.youtube.com/watch?v=7n8LNxGbZbs



Tuesday, 16 February 2010
Uses

                    (In statistics)
                    Generating samples given a distribution
                    so we can see if the data matches up
                    Approximating transformations
                    numerically



Tuesday, 16 February 2010
# same format as all other distribution related
     # functions, but uses r

     runif(100)
     rpois(10, lambda = 10)
     rbinom(100, n = 10, p = 0.5)

     # useful to display graphically:
     library(ggplot2)
     qplot(runif(100), binwidth = 0.1)




Tuesday, 16 February 2010
#     Example: Lakers offence and defence
     #     O ~ Poisson(109.9)
     #     D ~ Poisson(101.8)
     #     What's the distribution of score differences?

     o <- rpois(108, lambda = 109.9)
     d <- rpois(108, lambda = 101.8)

     qplot(o - d, binwidth = 1)
     # If those assumptions are true, that shows a
     # possible distribution of scores differences for
     # one season.


Tuesday, 16 February 2010
8
                 Actual data


             6
 ..count..




             4




             2




             0

                            −10   0       10     20   30
                                      Tm − Opp
Tuesday, 16 February 2010
#     If we want to see what the theoretical
     #     distribution looks like, we should use big
     #     numbers:
     o     <- rpois(1e5, lambda = 109.9)
     d     <- rpois(1e5, lambda = 101.8)

     qplot(o - d, binwidth = 1)




Tuesday, 16 February 2010
# This is a useful way of exploring transformations
     # of distributions

     x <- runif(1e5)
     qplot(x ^ 2, binwidth = 0.05)
     qplot(sqrt(x), binwidth = 0.05)
     qplot(exp(x), binwidth = 0.05)
     qplot(log(x), binwidth = 0.5)

     x <- rnorm(1e5)
     qplot(x ^ 2, binwidth = 0.5)
     qplot(exp(x), binwidth = 0.5)


Tuesday, 16 February 2010

More Related Content

What's hot

Lesson 23: Antiderivatives (slides)
Lesson 23: Antiderivatives (slides)Lesson 23: Antiderivatives (slides)
Lesson 23: Antiderivatives (slides)Matthew Leingang
 
Introduction to Signal Processing Orfanidis [Solution Manual]
Introduction to Signal Processing Orfanidis [Solution Manual]Introduction to Signal Processing Orfanidis [Solution Manual]
Introduction to Signal Processing Orfanidis [Solution Manual]slyhamm
 
Deep generative model.pdf
Deep generative model.pdfDeep generative model.pdf
Deep generative model.pdfHyungjoo Cho
 
Lesson 24: Implicit Differentiation
Lesson 24: Implicit DifferentiationLesson 24: Implicit Differentiation
Lesson 24: Implicit DifferentiationMatthew Leingang
 
Fourier series example
Fourier series exampleFourier series example
Fourier series exampleAbi finni
 
Lesson 18: Maximum and Minimum Values (slides)
Lesson 18: Maximum and Minimum Values (slides)Lesson 18: Maximum and Minimum Values (slides)
Lesson 18: Maximum and Minimum Values (slides)Matthew Leingang
 
Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)
Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)
Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)Matthew Leingang
 
Lesson 15: Exponential Growth and Decay (slides)
Lesson 15: Exponential Growth and Decay (slides)Lesson 15: Exponential Growth and Decay (slides)
Lesson 15: Exponential Growth and Decay (slides)Matthew Leingang
 
Fourier series of odd functions with period 2 l
Fourier series of odd functions with period 2 lFourier series of odd functions with period 2 l
Fourier series of odd functions with period 2 lPepa Vidosa Serradilla
 
Lesson 8: Basic Differentation Rules (slides)
Lesson 8: Basic Differentation Rules (slides)Lesson 8: Basic Differentation Rules (slides)
Lesson 8: Basic Differentation Rules (slides)Matthew Leingang
 
Lesson 20: Derivatives and the Shapes of Curves (slides)
Lesson 20: Derivatives and the Shapes of Curves (slides)Lesson 20: Derivatives and the Shapes of Curves (slides)
Lesson 20: Derivatives and the Shapes of Curves (slides)Matthew Leingang
 
Complex form fourier series
Complex form fourier seriesComplex form fourier series
Complex form fourier seriesderry92
 

What's hot (20)

Lesson 23: Antiderivatives (slides)
Lesson 23: Antiderivatives (slides)Lesson 23: Antiderivatives (slides)
Lesson 23: Antiderivatives (slides)
 
05 Random Variables
05 Random Variables05 Random Variables
05 Random Variables
 
08 Continuous
08 Continuous08 Continuous
08 Continuous
 
Introduction to Signal Processing Orfanidis [Solution Manual]
Introduction to Signal Processing Orfanidis [Solution Manual]Introduction to Signal Processing Orfanidis [Solution Manual]
Introduction to Signal Processing Orfanidis [Solution Manual]
 
Deep generative model.pdf
Deep generative model.pdfDeep generative model.pdf
Deep generative model.pdf
 
Lesson 24: Implicit Differentiation
Lesson 24: Implicit DifferentiationLesson 24: Implicit Differentiation
Lesson 24: Implicit Differentiation
 
Fourier series example
Fourier series exampleFourier series example
Fourier series example
 
Fourier series
Fourier seriesFourier series
Fourier series
 
Lesson 18: Maximum and Minimum Values (slides)
Lesson 18: Maximum and Minimum Values (slides)Lesson 18: Maximum and Minimum Values (slides)
Lesson 18: Maximum and Minimum Values (slides)
 
Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)
Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)
Lesson 14: Derivatives of Logarithmic and Exponential Functions (slides)
 
Lesson 5: Continuity
Lesson 5: ContinuityLesson 5: Continuity
Lesson 5: Continuity
 
Lesson 15: Exponential Growth and Decay (slides)
Lesson 15: Exponential Growth and Decay (slides)Lesson 15: Exponential Growth and Decay (slides)
Lesson 15: Exponential Growth and Decay (slides)
 
Fourier series of odd functions with period 2 l
Fourier series of odd functions with period 2 lFourier series of odd functions with period 2 l
Fourier series of odd functions with period 2 l
 
Prml
PrmlPrml
Prml
 
Lesson 8: Basic Differentation Rules (slides)
Lesson 8: Basic Differentation Rules (slides)Lesson 8: Basic Differentation Rules (slides)
Lesson 8: Basic Differentation Rules (slides)
 
Chapter 2 (maths 3)
Chapter 2 (maths 3)Chapter 2 (maths 3)
Chapter 2 (maths 3)
 
Fourier series
Fourier seriesFourier series
Fourier series
 
Lesson 20: Derivatives and the Shapes of Curves (slides)
Lesson 20: Derivatives and the Shapes of Curves (slides)Lesson 20: Derivatives and the Shapes of Curves (slides)
Lesson 20: Derivatives and the Shapes of Curves (slides)
 
Complex form fourier series
Complex form fourier seriesComplex form fourier series
Complex form fourier series
 
QMC: Operator Splitting Workshop, Stochastic Block-Coordinate Fixed Point Alg...
QMC: Operator Splitting Workshop, Stochastic Block-Coordinate Fixed Point Alg...QMC: Operator Splitting Workshop, Stochastic Block-Coordinate Fixed Point Alg...
QMC: Operator Splitting Workshop, Stochastic Block-Coordinate Fixed Point Alg...
 

Viewers also liked (20)

24 Spam
24 Spam24 Spam
24 Spam
 
14 Bivariate Transformations
14 Bivariate Transformations14 Bivariate Transformations
14 Bivariate Transformations
 
13 Bivariate
13 Bivariate13 Bivariate
13 Bivariate
 
01 Introduction
01 Introduction01 Introduction
01 Introduction
 
21 Ml
21 Ml21 Ml
21 Ml
 
18 Normal Cont
18 Normal Cont18 Normal Cont
18 Normal Cont
 
07 Discrete
07 Discrete07 Discrete
07 Discrete
 
04 Reports
04 Reports04 Reports
04 Reports
 
26 Development
26 Development26 Development
26 Development
 
15 Bivariate Change Of Variables
15 Bivariate Change Of Variables15 Bivariate Change Of Variables
15 Bivariate Change Of Variables
 
09 Simulation
09 Simulation09 Simulation
09 Simulation
 
08 Continuous
08 Continuous08 Continuous
08 Continuous
 
23 Estimation
23 Estimation23 Estimation
23 Estimation
 
25 fin
25 fin25 fin
25 fin
 
18 Sampling Mean Sd
18 Sampling Mean Sd18 Sampling Mean Sd
18 Sampling Mean Sd
 
20 Estimation
20 Estimation20 Estimation
20 Estimation
 
20 Polishing
20 Polishing20 Polishing
20 Polishing
 
14 Ddply
14 Ddply14 Ddply
14 Ddply
 
15 Time Space
15 Time Space15 Time Space
15 Time Space
 
10 simulation
10 simulation10 simulation
10 simulation
 

Similar to 11 Simulation

Applied Business Statistics ,ken black , ch 5
Applied Business Statistics ,ken black , ch 5Applied Business Statistics ,ken black , ch 5
Applied Business Statistics ,ken black , ch 5AbdelmonsifFadl
 
Chap10 hypothesis testing ; additional topics
Chap10 hypothesis testing ; additional topicsChap10 hypothesis testing ; additional topics
Chap10 hypothesis testing ; additional topicsJudianto Nugroho
 
Probability cheatsheet
Probability cheatsheetProbability cheatsheet
Probability cheatsheetSuvrat Mishra
 
Class 16: Making Loops
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making LoopsDavid Evans
 
Statistik 1 5 distribusi probabilitas diskrit
Statistik 1 5 distribusi probabilitas diskritStatistik 1 5 distribusi probabilitas diskrit
Statistik 1 5 distribusi probabilitas diskritSelvin Hadi
 
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean OptimizationEfficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimizationjfrchicanog
 

Similar to 11 Simulation (12)

24 modelling
24 modelling24 modelling
24 modelling
 
22 confidence
22 confidence22 confidence
22 confidence
 
10 Transformations
10 Transformations10 Transformations
10 Transformations
 
10 simulation
10 simulation10 simulation
10 simulation
 
11 Bivariate
11 Bivariate11 Bivariate
11 Bivariate
 
Applied Business Statistics ,ken black , ch 5
Applied Business Statistics ,ken black , ch 5Applied Business Statistics ,ken black , ch 5
Applied Business Statistics ,ken black , ch 5
 
Chap10 hypothesis testing ; additional topics
Chap10 hypothesis testing ; additional topicsChap10 hypothesis testing ; additional topics
Chap10 hypothesis testing ; additional topics
 
Probability cheatsheet
Probability cheatsheetProbability cheatsheet
Probability cheatsheet
 
Class 16: Making Loops
Class 16: Making LoopsClass 16: Making Loops
Class 16: Making Loops
 
Statistik 1 5 distribusi probabilitas diskrit
Statistik 1 5 distribusi probabilitas diskritStatistik 1 5 distribusi probabilitas diskrit
Statistik 1 5 distribusi probabilitas diskrit
 
16 Sums
16 Sums16 Sums
16 Sums
 
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean OptimizationEfficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
Efficient Hill Climber for Multi-Objective Pseudo-Boolean Optimization
 

More from Hadley Wickham (20)

27 development
27 development27 development
27 development
 
27 development
27 development27 development
27 development
 
23 data-structures
23 data-structures23 data-structures
23 data-structures
 
Graphical inference
Graphical inferenceGraphical inference
Graphical inference
 
R packages
R packagesR packages
R packages
 
22 spam
22 spam22 spam
22 spam
 
21 spam
21 spam21 spam
21 spam
 
20 date-times
20 date-times20 date-times
20 date-times
 
19 tables
19 tables19 tables
19 tables
 
18 cleaning
18 cleaning18 cleaning
18 cleaning
 
17 polishing
17 polishing17 polishing
17 polishing
 
16 critique
16 critique16 critique
16 critique
 
15 time-space
15 time-space15 time-space
15 time-space
 
14 case-study
14 case-study14 case-study
14 case-study
 
13 case-study
13 case-study13 case-study
13 case-study
 
12 adv-manip
12 adv-manip12 adv-manip
12 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
11 adv-manip
11 adv-manip11 adv-manip
11 adv-manip
 
09 bootstrapping
09 bootstrapping09 bootstrapping
09 bootstrapping
 
08 functions
08 functions08 functions
08 functions
 

11 Simulation

  • 1. Stat310 Simulation Hadley Wickham Tuesday, 16 February 2010
  • 2. Test • Don’t forget it’s on Tuesday • All learning objectives, lectures & homework model answers up on site • Help sessions Tuesday & Wednesday 4-5 pm Tuesday, 16 February 2010
  • 3. 1. Valentine’s day & statistics 2. Normal distribution 3. Intro to R 4. Generating random numbers Tuesday, 16 February 2010
  • 5. You are perfect; I'd make no substitutions You remind me of my favorite distributions With a shape and a scale that I find reliable You’re as comforting as a two parameter Weibull When I ask you a question and you answer truly You speak as clearly as a draw from a Bernoulli Your love of adventure is most influential Just like the constant hazard of an exponential. With so many moments, all full of fun, You always integrate perfectly to one Tuesday, 16 February 2010
  • 6. Standard normal For the normal distribution, we can convert any normally distributed rv to a standard normal by subtracting off the mean and dividing by the standard deviation. This means we only need one table for any possible normal distribution. Tuesday, 16 February 2010
  • 7. Using the tables Column + row = z Find: Φ(2.94), Φ(-1), Φ(0.01), Φ(4) Can also use in reverse: For what value of z is P(Z < z) = 0.90 ? i.e. What is Φ-1(0.90)? Find: Φ-1(0.1), Φ-1(0.5), Φ-1(0.65), Φ-1(1) Tuesday, 16 February 2010
  • 8. Example The time it takes me to drive to school is normally distributed with mean 20 and standard deviation 3. What is the probability it takes me more than 20 minutes to drive to school? What time should I leave so that I have 95% chance of getting to class by 1pm? Tuesday, 16 February 2010
  • 9. Example What’s the probability I take a negative amount of time to get to school? Can the the distribution of my driving time really be normal? Is that a problem? Tuesday, 16 February 2010
  • 10. P (Z < z) = Φ(z) Φ(−z) = 1 − Φ(z) P (−1 < Z < 1) = 0.68 P (−2 < Z < 2) = 0.95 P (−3 < Z < 3) = 0.998 Tuesday, 16 February 2010
  • 12. Intro to R Open-source statistical programming environment http://www.r-project.org Installed on all lab computers We’re going to use it as a calculator and for some basic simulations. Extra info available on the website. Tuesday, 16 February 2010
  • 13. > 3 + 7 [1] 10 > sqrt(100) + 3 ^ 2 [1] 19 > 1:10 [1] 1 2 3 4 5 6 7 8 9 10 > (1:10) ^ 2 [1] 1 4 9 16 25 36 49 64 81 100 Tuesday, 16 February 2010
  • 14. # From now on I'll just show the code # <- means that this is a comment and will be # ignored by R. sin(2 * pi) cos(pi) exp(1:10) log(1:10) log(exp(1:10)) sqrt(2)^2 - 2 Tuesday, 16 February 2010
  • 15. Your turn Why does sqrt(2)^2 - 2 not equal 0? Tuesday, 16 February 2010
  • 16. x <- c(1, 5, 3, 9, 2.4) sum(x) mean(x) var(x) sd(x) Tuesday, 16 February 2010
  • 17. Probabilities Can use R instead of tables of probabilities. (This of course is what all modern statisticians do) {p, d, q} x {binom, pois, unif, exp, gamma, norm} d = pdf/pmf, p = cdf, q = inverse cdf Tuesday, 16 February 2010
  • 18. # X ~ Binom(n = 100, p = 0.35) # P(X = 0) dbinom(0, size = 100, prob = 0.35) # P(X = 35) dbinom(35, size = 100, prob = 0.35) # P(X < 10) pbinom(10, size = 100, prob = 0.35) # P(X > 10) 1 - pbinom(10, size = 100, prob = 0.35) # P(X < 80) pbinom(80, size = 100, prob = 0.35) # P(30 <= X < 40) pbinom(40, size = 100, prob = 0.35) - pbinom(30, size = 100, prob = 0.35) Tuesday, 16 February 2010
  • 19. # Probability of getting an odd number of heads # when flipping a fair coin 100 times sum(dbinom(1 + 2 * (0:49), size = 10, prob = 0.5)) # To figure it out, pull out the code from the # outside in 0:49 1 + 2 * (0:49) dbinom(1 + 2 * (0:49), size = 10, prob = 0.5) sum(dbinom(1 + 2 * (0:49), size = 10, prob = 0.5)) Tuesday, 16 February 2010
  • 20. Suffix Distribution Parameters binom Binomial size, prob pois Poisson lambda unif Uniform min, max exp Exponential rate gamma Gamma shape, rate norm Normal mean, sd Tuesday, 16 February 2010
  • 21. Your turn X ~ Normal(20, 9) What R code would you type to calculate P(17 < X < 23)? Tuesday, 16 February 2010
  • 22. Random number generation Tuesday, 16 February 2010
  • 23. Uniform to any rv IF Y ~ Uniform(0, 1) F a cdf THEN X= F -1(Y) is a rv with cdf F(x) (Assume F strictly increasing for simplicity) Tuesday, 16 February 2010
  • 24. Source of uniform numbers? http://www.youtube.com/watch?v=7n8LNxGbZbs Tuesday, 16 February 2010
  • 25. Uses (In statistics) Generating samples given a distribution so we can see if the data matches up Approximating transformations numerically Tuesday, 16 February 2010
  • 26. # same format as all other distribution related # functions, but uses r runif(100) rpois(10, lambda = 10) rbinom(100, n = 10, p = 0.5) # useful to display graphically: library(ggplot2) qplot(runif(100), binwidth = 0.1) Tuesday, 16 February 2010
  • 27. # Example: Lakers offence and defence # O ~ Poisson(109.9) # D ~ Poisson(101.8) # What's the distribution of score differences? o <- rpois(108, lambda = 109.9) d <- rpois(108, lambda = 101.8) qplot(o - d, binwidth = 1) # If those assumptions are true, that shows a # possible distribution of scores differences for # one season. Tuesday, 16 February 2010
  • 28. 8 Actual data 6 ..count.. 4 2 0 −10 0 10 20 30 Tm − Opp Tuesday, 16 February 2010
  • 29. # If we want to see what the theoretical # distribution looks like, we should use big # numbers: o <- rpois(1e5, lambda = 109.9) d <- rpois(1e5, lambda = 101.8) qplot(o - d, binwidth = 1) Tuesday, 16 February 2010
  • 30. # This is a useful way of exploring transformations # of distributions x <- runif(1e5) qplot(x ^ 2, binwidth = 0.05) qplot(sqrt(x), binwidth = 0.05) qplot(exp(x), binwidth = 0.05) qplot(log(x), binwidth = 0.5) x <- rnorm(1e5) qplot(x ^ 2, binwidth = 0.5) qplot(exp(x), binwidth = 0.5) Tuesday, 16 February 2010