R provides rich availability of using probability functions. Probability functions in R takes the form
[dpqr][prob],
Where
[dpqr] represent which kind of variates the function works on
d – probability density
p – cumulative probabilty
q – quantile value
r – random number generation
[prob] denotes which probability distribution is taken on. The following example code illustrate the situation when we deal with normal distributions.
#load library
library(ggplot2)
#create a vector
x <- seq(from = -10, to = 10, by = 0.2)
#generate density values from standard normal distribution
y <- dnorm(x)
#show first 10 values of result
y[1:10]
#output
[1] 7.694599e-23 5.573000e-22 3.878112e-21 2.592865e-20
[5] 1.665588e-19 1.027977e-18 6.095758e-18 3.472963e-17
[9] 1.901082e-16 9.998379e-16
#create a data frame
data <- data.frame(x = x, y = y)
#plot relationship using ggplot2
ggplot(data, aes(x, y)) +
geom_line() +
labs(x = "sequential numbers",
y = "Normal variate") +
scale_x_continuous(breaks = seq(-10, 10, 1))
#the area under the standard normal curve to the left of z=1.25?
pnorm(1.25)
#output
[1] 0.8943502
#the value of the 60th percentile of a normal distribution
#with a mean of 30 and a standard deviation of 8
qnorm(.6, mean=30, sd=8)
#output
[1] 32.02678
#Generate 20 random normal variates with a mean of 30 and a
#standard deviation of 8
rnorm(20, mean=30, sd=8)
#output
[1] 18.03202 41.01986 25.47644 16.87644 11.62907 35.38060 21.98076
[8] 35.86054 25.21310 17.53319 31.04676 35.65328 31.70905 22.68345
[15] 18.20250 34.46939 34.46720 42.65455 14.22156 27.36327
0 Comments