Download R from https://cloud.r-project.org/ Choose different links based on your system
For Windows, click base to download and install
For Mac, click the pkg file to download and install
Rstudio is an excellent interface for R.
You can directly run functions in Rstudio. For example, type \(1+1\) and click enter
A R script file can be created, which can be saved locally and you can use it later.
\(Binomial(n,p)\)
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
\(Geom(p)\). In R, the random variables is defined as the number of failures before the first success
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
\(NB(r,p)\). In R, the random variables is defined as the number of failures before the rth success
dnbinom(5-3,size = 3,prob = 0.7)
## [1] 0.18522
pnbinom(q = 5-3,size = 3,prob = 0.7)
## [1] 0.83692
\(Hyper(N,r,n)\).
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(\lambda)\)
?dpois
dpois(x = 4,lambda = 2)
## [1] 0.09022352
ppois(q = 4,lambda = 2)
## [1] 0.947347
\(Uniform(\theta_1, \theta_2)\)
For example, \(\theta_1 = 1\) and \(\theta_2 = 2\)
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
For example, \(\mu = 1\) and \(\sigma = 2\)
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
# 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
# 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
# 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
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(\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(\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^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\)
\(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\)