Installation of R

Installation of Rstudio

Rstudio is an excellent interface for R.

Start Rstudio

Discrete distributions

binomial distribution

\(Binomial(n,p)\)

  • dbinom: \(P(Y=x)\)
  • pbinom: \(P(Y\leq y)\)
  • Examples in the notes
dbinom(3,size = 10,prob = 0.3)
## [1] 0.2668279
pbinom(3,size = 10,prob = 0.3)
## [1] 0.6496107
1 - pbinom(2,10,0.3)
## [1] 0.6172172
pbinom(2,10,0.3)
## [1] 0.3827828
1 - pbinom(3,10,0.3)
## [1] 0.3503893

Geometric distribution

\(Geom(p)\). In R, the random variables is defined as the number of failures before the first success

  • dgeom: \(P(Y=x)\)
  • pgeom: \(P(Y\leq x)\)
  • Examples in the notes
1 - pgeom(2 -1, 0.35)
## [1] 0.4225
dgeom(4-1, 0.35)
## [1] 0.09611875
pgeom(5-1, 0.35)
## [1] 0.8839709

Negative Binomial

\(NB(r,p)\). In R, the random variables is defined as the number of failures before the rth success

  • dnbinom: \(P(Y=y)\)
  • pnbinom: \(P(Y\leq y)\)
  • Examples in the notes
dnbinom(5-3,size = 3,prob = 0.7)
## [1] 0.18522
pnbinom(q = 5-3,size = 3,prob = 0.7)
## [1] 0.83692

Hypergeometric

\(Hyper(N,r,n)\).

  • dhyper: \(P(Y=y)\)
  • phyper: \(P(Y\leq y)\)
  • Examples in the notes.
dhyper(6, 6, 36, 6)
## [1] 1.906292e-07
dhyper(4, 6, 36, 6)
## [1] 0.001801446
1 - phyper(2, 6, 36, 6)
## [1] 0.02906466

Poisson

\(Poisson(\lambda)\)

  • dpois: \(P(X=x)\)
  • ppois: \(P(X\leq x)\)
  • \(P(X=4)\) and \(P(X\leq 4)\) with \(\lambda = 2\)
?dpois
dpois(x = 4,lambda = 2)
## [1] 0.09022352
ppois(q = 4,lambda = 2)
## [1] 0.947347

Continuous distributions

Uniform

\(Uniform(\theta_1, \theta_2)\)

For example, \(\theta_1 = 1\) and \(\theta_2 = 2\)

  • dunif: probability density function
  • punif: \(P(X\leq x)\)
  • qunif: \(P(X\leq q) = p\) pth quantile
dunif(x = 1.5,min = 1,max = 2)
## [1] 1
punif(q = 1.5,min = 1,max = 2)
## [1] 0.5
qunif(p = 0.5,min = 1,max = 2)
## [1] 1.5

Normal

\(Normal(\mu, \sigma^2)\)

For example, \(\mu = 1\) and \(\sigma = 2\)

  • dnorm: probability density function
  • pnorm: \(P(X\leq x)\)
  • qnorm: \(P(X\leq q) = p\) pth quantile
dnorm(x = 1,mean = 1,sd = 2)
## [1] 0.1994711
pnorm(q = 1.5,mean = 1,sd = 2)
## [1] 0.5987063
qnorm(p = 0.5,mean = 1,sd = 2)
## [1] 1
  • N(0,1), N(1,1), N(-1,1)

  • N(0,1), N(0,\(2^2\)), N(0,\(0.5^2\))

Example 4.8: Standard normal distribution \(Z\)

    # P(Z > 2)
    1 - pnorm(2)
## [1] 0.02275013
    # P(Z < 2)
    pnorm(2)
## [1] 0.9772499
    # P(Z < -2)
    pnorm(-2)
## [1] 0.02275013
    # P(Z > -2)
    1 - pnorm(-2)
## [1] 0.9772499
    # P(-2 < Z <2)
    pnorm(2) - pnorm(-2)
## [1] 0.9544997
    2*(1 - pnorm(-2))
## [1] 1.9545
    # P(0 < Z < 1.73)
    pnorm(1.73) - pnorm(0)
## [1] 0.4581849

Example of general \(\mu\) and \(\sigma\)

    # Y ~ N(2,4); P(0 < Y <4) = P(-1 <Z < 1)
    pnorm(1) - pnorm(-1)
## [1] 0.6826895
    pnorm(4,mean = 2,sd = 2) - pnorm(0,mean = 2,sd = 2)
## [1] 0.6826895
    # Y ~ N(5,1); P(Y > 7.32) = P(Z > 2.32)
    1- pnorm(2.32)
## [1] 0.01017044
    1 - pnorm(7.32,mean = 5,sd = 1)
## [1] 0.01017044
    # Y ~ N(0,25); P(Y < 7) = P(Z < 7/5)
    pnorm(7/5)
## [1] 0.9192433
    pnorm(7,mean = 0,sd = 5)
## [1] 0.9192433

quantiles

    # Standard normal distribution
    qnorm(0.95)
## [1] 1.644854
    qnorm(0.1)
## [1] -1.281552
    # Y ~ N(75,100)
    75 + 10 * qnorm(0.97)
## [1] 93.80794
    qnorm(0.97,mean = 75,sd = 10)
## [1] 93.80794

Empirical Rule

library(ggplot2)
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
 stat_function(fun = dnorm, geom = "line") +
  geom_vline(xintercept = c(-1,1),color= "red")

pnorm(1) - pnorm(-1)
## [1] 0.6826895
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
 stat_function(fun = dnorm, geom = "line") +
  geom_vline(xintercept = c(-2,2),color= "blue")

pnorm(2) - pnorm(-2)
## [1] 0.9544997
ggplot(data.frame(x = c(-4, 4)), aes(x)) +
 stat_function(fun = dnorm, geom = "line") +
    geom_vline(xintercept = c(-3,3),color= "purple")

pnorm(3) - pnorm(-3)
## [1] 0.9973002

Gamma

  • dgamma: probability density function
  • pgamma: \(P(X\leq x)\)
  • qgamma: \(P(X\leq q) = p\) pth quantile

\(Gamma(\alpha = 1.5, \beta = 2.5)\)

dgamma(x = 1.5,shape = 1.5,scale = 2.5)
## [1] 0.1918731
pgamma(q = 1.5,shape= 1,scale = 2.5)
## [1] 0.4511884
qgamma(p = 0.5,shape = 1.5, scale = 2.5)
## [1] 2.957467

\(\alpha = 1,2,4\), \(\beta = 1\)

\(\alpha = 1,2,4\), \(\beta = 2\)

\(\alpha = 1\), \(\beta = 1,2,4\)

\(\alpha = 2\), \(\beta = 1,2,4\)

\(\alpha = 4\), \(\beta = 1,2,4\)

Beta

  • dbeta: probability density function
  • pbeta: \(P(X\leq x)\)
  • qbeta: \(P(X\leq q) = p\) pth quantile

\(Beta(\alpha = 1.5, \beta = 2.5)\)

dbeta(x = 0.6,shape1 = 1.5,shape2  = 2.5)
## [1] 0.9980119
pbeta(q = 0.6,shape1 = 1.5,shape2  = 2.5)
## [1] 0.8260723
qbeta(p = 0.5,shape1 = 1.5,shape2  = 2.5)
## [1] 0.3524523

\(\alpha = 1,2,4\), \(\beta = 1,2,4\)

\(\alpha = 2\), \(\beta = 2,4,6\)

\(\alpha = 2,4,6\), \(\beta = 2\)

chi-square

  • dchisq: probability density function
  • pchisq: \(P(X\leq x)\)
  • qchisq: \(P(X\leq q) = p\) pth quantile

\(\chi^2(v = 2)\)

dchisq(x = 2.5,df = 2)
## [1] 0.1432524
pchisq(q = 2.5,df = 2)
## [1] 0.7134952
qchisq(p = 0.5,df = 2)
## [1] 1.386294

\(v = 1,2,4\)

exponential

  • dexp: probability density function
  • pexp: \(P(X\leq x)\)
  • qexp: \(P(X\leq q) = p\) pth quantile

\(Exp(\beta = 2)\)

dexp(x = 2.5,1/2)
## [1] 0.1432524
pexp(q = 2.5,1/2)
## [1] 0.7134952
qexp(p = 0.5,1/2)
## [1] 1.386294

\(\beta = 1,2,4\)