The post How to calculate Continuous Uniform distribution probabilities in R appeared first on We provide R, Python, Statistics Online-Learning Course.
]]>Continuous Uniform distribution is used to model a flat probability existence with a range (A,B). Its probability density can be depicted as

The follow figure shows probability density curve for a Continuous Uniform distribution defined with range (1,3).

In R programming, we can use following functions in computation of Continuous Uniform distribution.
dunif() – for probability density ,
punif() – for cumulative probability,
qunif() – for quantile value ,
dunif() – for random number generation .
Following code snippet shows how to use these functions in RStudio.
# generating a vector of numbers
x_vec <- seq(0, 10 , by = 0.2)
x_vec
#output
[1] 0.0 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4
[14] 2.6 2.8 3.0 3.2 3.4 3.6 3.8 4.0 4.2 4.4 4.6 4.8 5.0
[27] 5.2 5.4 5.6 5.8 6.0 6.2 6.4 6.6 6.8 7.0 7.2 7.4 7.6
[40] 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2 9.4 9.6 9.8 10.0# calculating #calculate probability density for these numbers in a Continuous Uniform
#probability distribution, with range defined on (3,8)
dunif(x_vec, min = 3, max = 8)
#output
[1] 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.2
[17] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2
[33] 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.2 0.0 0.0 0.0 0.0 0.0 0.0 0.0
[49] 0.0 0.0 0.0
#plot probability density
plot(dunif(x_vec, min = 3, max = 8))

#calculate cumulative probabilities
punif (x_vec , min =3 , max = 8)
#plot cumulative probabilities
plot(punif (x_vec , min =3 , max = 8))

#create a vector for probalities
q_vec <- seq(0, 1 , by = 0.1)
q_vec
#output
0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0plot(qunif(q, min = 3, max = 8))
#quantile of these probability values
#quantile means the associated cumulative probability of values less than
#this quantile value
qunif(q_vec, min = 3, max = 8)
#output
3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0 7.5 8.0
#random number generation, 10 such random numbers from Continuous Uniform
#distribution
runif(10, min=3, max=8)
#output
[1] 4.892670 3.914989 7.944297 3.637653 4.492242 7.196020 4.513473
[8] 7.857784 6.017247 6.964857
The post How to calculate Continuous Uniform distribution probabilities in R appeared first on We provide R, Python, Statistics Online-Learning Course.
]]>The post How to generate random numbers from Normal, Uniform and Poisson distribution in R appeared first on We provide R, Python, Statistics Online-Learning Course.
]]>Doing statistics using R is perfect for many data analysts. Dealing with various statistical distributions and generating random numbers from some widely used distributions are mandatory for data science. In this post, we show how to generate random numbers from Normal, Uniform and Poisson distributions in R.
Normal variates can be generated by using rnorm() function in R. The basic form of the function is:
rnorm(N, mean, sd) ,
Where N is for how many such random numbers to generate, mean and sd for the mean and standard deviation of the distribution and default values are 0 and 1 for these two parameters. Next code block show several examples of generating random numbers from specified Normal distributions.
#generate 10 random numbers from standard normal distribution
#standard normal distribution (mean=0, sd=1)
vec_norm1 <- rnorm(10)
vec_norm1
#output
[1] -0.10300454 -0.49992423 -0.04867705 -0.20479786 0.64047272
[6] 0.80908181 2.63997308 0.54729061 -1.53948859 -0.56861347
#10 random numbers from normal variates with mean 32
#and standard deviation 2
vec_norm2 <- rnorm(10, mean=32, sd=2)
vec_norm2
#output
[1] 31.77717 29.00409 33.75556 32.12455 29.60058 33.79212 32.04183
[8] 33.66872 31.60234 31.13245
# a matrix of 50 normal variates with mean 32 sd 2
mat_norm <- matrix(rnorm(50,mean=32, sd=2),nrow=10)
mat_norm
#output
[,1] [,2] [,3] [,4] [,5]
[1,] 32.17958 27.05001 36.68007 30.66535 33.00489
[2,] 31.97359 33.11925 32.96932 30.13305 33.49547
[3,] 32.30013 32.74311 33.43902 32.04489 32.22167
[4,] 35.18345 28.34490 32.31010 30.84051 29.25815
[5,] 28.83627 34.10811 29.39567 30.01753 34.64026
[6,] 28.89909 27.29548 32.78904 32.68282 32.00039
[7,] 33.32865 31.11026 32.56352 34.01731 34.44393
[8,] 32.18550 35.94802 33.88321 30.73226 32.15024
[9,] 29.06265 30.57460 30.49779 31.30553 30.00966
[10,] 29.73152 30.07022 29.59840 32.81636 35.68608
2. Uniform distribution
Uniform distribution states the situation where a constant probability density exists in a range. The function runif(N, min, max) in R generates Uniform variates, Where
N represents sample size, min and max parameters specify the range. The following code block illustrate examples about Uniform distribution.
#25 uniform variates between 0 and 1
#default range is 0 and 1
vec_uni_1 <- runif(25)
vec_uni_1
#output
[1] 0.96642510 0.38370536 0.99770052 0.99907261 0.38873183
[6] 0.56486749 0.31835318 0.33375417 0.91052761 0.81353888
[11] 0.19505230 0.06403500 0.96022244 0.40504310 0.02559239
[16] 0.66514033 0.96932741 0.55033963 0.52867588 0.13435604
[21] 0.49814712 0.51983895 0.80709462 0.53736817 0.16027540
#10 uniform variates between 3 and 8
vec_uni_2 <- runif(10, min=3, max=8)
vec_uni_2
#output
[1] 5.264467 5.507854 5.859587 5.014579 3.052218 7.628316 3.571432
[8] 4.471559 4.501750 7.780484
3. Poisson distribution
Poisson distribution is modeling event occurrence with a time interval, in which a constant average occurrence rate exists. For example, With a constant event occurring rate 5 telephone calls coming in a call center per hour, the mean of coming calls in a 2 hour period is 10 times. The following code example show the Poisson random numbers (integers) are generated from function rpois(N, mean).
#a matrix of 100 poisson variates with mean 10
matrix(rpois(100, 10),nrow=20)
#output
[,1] [,2] [,3] [,4] [,5]
[1,] 10 9 14 8 8
[2,] 19 14 19 12 11
[3,] 6 10 9 5 7
[4,] 12 9 11 12 12
[5,] 7 15 15 9 6
[6,] 12 14 10 7 3
[7,] 6 14 10 9 13
[8,] 7 7 17 7 15
[9,] 12 7 7 12 8
[10,] 11 10 14 10 12
[11,] 10 5 9 6 11
[12,] 9 7 9 7 4
[13,] 13 5 10 6 10
[14,] 7 8 8 6 11
[15,] 11 9 9 7 9
[16,] 8 9 8 13 10
[17,] 12 5 13 11 7
[18,] 14 11 11 6 14
[19,] 7 12 11 9 12
[20,] 16 14 7 12 15
>
The post How to generate random numbers from Normal, Uniform and Poisson distribution in R appeared first on We provide R, Python, Statistics Online-Learning Course.
]]>