The article contains a brief introdcution to the R package ‘clttools’, demonstrating some simple examples as well.
Some experiments related to discrete sample point, for exampLe, to find theoretical probability and plot of the mean of rolling 4 fair/unfair dice:
library(clttools)
#theoretical probability of the mean of rolling 4 fair/unfair dice
dice(4)
## MEAN_VALUE PROBABILITY
## 1 1.00 0.0007716049
## 2 1.25 0.0030864198
## 3 1.50 0.0077160494
## 4 1.75 0.0154320988
## 5 2.00 0.0270061728
## 6 2.25 0.0432098765
## 7 2.50 0.0617283951
## 8 2.75 0.0802469136
## 9 3.00 0.0964506173
## 10 3.25 0.1080246914
## 11 3.50 0.1126543210
## 12 3.75 0.1080246914
## 13 4.00 0.0964506173
## 14 4.25 0.0802469136
## 15 4.50 0.0617283951
## 16 4.75 0.0432098765
## 17 5.00 0.0270061728
## 18 5.25 0.0154320988
## 19 5.50 0.0077160494
## 20 5.75 0.0030864198
## 21 6.00 0.0007716049
dice(4, prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
## MEAN_VALUE PROBABILITY
## 1 1.00 0.0001
## 2 1.25 0.0004
## 3 1.50 0.0010
## 4 1.75 0.0020
## 5 2.00 0.0047
## 6 2.25 0.0096
## 7 2.50 0.0164
## 8 2.75 0.0248
## 9 3.00 0.0399
## 10 3.25 0.0596
## 11 3.50 0.0770
## 12 3.75 0.0900
## 13 4.00 0.1097
## 14 4.25 0.1256
## 15 4.50 0.1200
## 16 4.75 0.1008
## 17 5.00 0.0888
## 18 5.25 0.0736
## 19 5.50 0.0416
## 20 5.75 0.0128
## 21 6.00 0.0016
#corresponding plot
par(mfrow=c(1,2))
dice.plot(4)
dice.plot(4, prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
To find simulated probability and plot of the mean of rolling 4 fair/unfair dice:
#simulated probability of the mean of rolling 4 fair/unfair dice, simulate 1000 times
dice.simu(4,times = 1000)
## MEAN_VALUE PROBABILITY
## [1,] 1.00 0.000
## [2,] 1.25 0.004
## [3,] 1.50 0.012
## [4,] 1.75 0.023
## [5,] 2.00 0.030
## [6,] 2.25 0.052
## [7,] 2.50 0.053
## [8,] 2.75 0.084
## [9,] 3.00 0.081
## [10,] 3.25 0.102
## [11,] 3.50 0.116
## [12,] 3.75 0.100
## [13,] 4.00 0.107
## [14,] 4.25 0.069
## [15,] 4.50 0.075
## [16,] 4.75 0.044
## [17,] 5.00 0.014
## [18,] 5.25 0.021
## [19,] 5.50 0.006
## [20,] 5.75 0.005
## [21,] 6.00 0.002
dice.simu(4, times = 1000,prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
## MEAN_VALUE PROBABILITY
## [1,] 1.00 0.000
## [2,] 1.25 0.000
## [3,] 1.50 0.000
## [4,] 1.75 0.004
## [5,] 2.00 0.002
## [6,] 2.25 0.018
## [7,] 2.50 0.021
## [8,] 2.75 0.027
## [9,] 3.00 0.034
## [10,] 3.25 0.071
## [11,] 3.50 0.066
## [12,] 3.75 0.101
## [13,] 4.00 0.104
## [14,] 4.25 0.140
## [15,] 4.50 0.119
## [16,] 4.75 0.096
## [17,] 5.00 0.081
## [18,] 5.25 0.068
## [19,] 5.50 0.034
## [20,] 5.75 0.014
## [21,] 6.00 0.000
#corresponding plot
par(mfrow=c(1,2))
dice.simu.plot(4,times = 1000)
dice.simu.plot(4, times = 1000,prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
We may also use function coin, expt to do similar experiments:
expt(c(1,2,5),n=3, prob=c(0.1,0.1,0.8))
## MEAN_VALUE PROBABILITY
## 1 1.000000 0.001
## 2 1.333333 0.003
## 3 1.666667 0.003
## 4 2.000000 0.001
## 5 2.333333 0.024
## 6 2.666667 0.048
## 7 3.000000 0.024
## 8 3.666667 0.192
## 9 4.000000 0.192
## 10 5.000000 0.512
expt.simu.plot(c(1,2,3),n=3, times=1000)
Explore central limit theorem by generating experiments from other distribution, Binomial, Exponential, Gamma, etc.
expo.plot(2, rate=1, times=1000, qqplot = TRUE)
expo.plot(4, rate=1, times=1000, qqplot = TRUE)
expo.plot(10, rate=1, times=1000, qqplot = TRUE)
expo.plot(25, rate=1, times=1000, qqplot = TRUE)
par(mfrow=c(2,2))
pois.plot(2, lambda = 2, times= 1000)
pois.plot(4, lambda = 2, times= 1000)
pois.plot(10, lambda = 2, times= 1000)
pois.plot(25, lambda = 2, times= 1000)
From the plots above, we can know that as the number of sample(n) in each trial increase, the sampling distribution is getting closer and closer to Normal distribution.
#all the codes:
library(clttools)
#theoretical probability of the mean of rolling 4 fair/unfair dice
dice(4)
dice(4, prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
#corresponding plot
par(mfrow=c(1,2))
dice.plot(4)
dice.plot(4, prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
#simulated probability of the mean of rolling 4 fair/unfair dice, simulate 1000 times
dice.simu(4,times = 1000)
dice.simu(4, times = 1000,prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
#corresponding plot
par(mfrow=c(1,2))
dice.simu.plot(4,times = 1000)
dice.simu.plot(4, times = 1000,prob = c(0.1,0.1,0.1,0.1,0.4,0.2))
#some other similar experiments
expt(c(1,2,5),prob=c(0.1,0.1,0.8))
expt.simu.plot(c(1,2,3),times=1000)
#explore central limit theorem by generating experiments from other distribution, Binomial, Exponential, Gamma, etc.
expo.plot(2, rate=1, times=1000, qqplot = TRUE)
expo.plot(4, rate=1, times=1000, qqplot = TRUE)
expo.plot(10, rate=1, times=1000, qqplot = TRUE)
expo.plot(25, rate=1, times=1000, qqplot = TRUE)
par(mfrow=c(2,2))
pois.plot(2, lambda = 2, times= 1000)
pois.plot(4, lambda = 2, times= 1000)
pois.plot(10, lambda = 2, times= 1000)
pois.plot(25, lambda = 2, times= 1000)