R Programming

Using probability functions in R

We provide effective and economically affordable training courses for R and Python, click here for more details and course registration !

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))
Standard Normal Probability Density Curve
#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

For getting more knowledge of R and a preview of our training course, you can watch R tutorial videos on our YouTube channel !

wilsonzhang746

Recent Posts

Fine tuning a Hugging Face transformer model for multi-track music generation

To train a PyTorch Transformer for multi-track music generation, the standard paradigm is to convert…

3 weeks ago

what are Transformer, LLM , and GPT

In the field of AI and Natural Language Processing (NLP), The Transformer is the engine…

4 weeks ago

Main framework for PyTorch music generation model

If your goal is purely symbolic melody generation (e.g., generating MIDI files or notes rather…

4 weeks ago

affine_grid() in pytorch

According to PyTorch document, In PyTorch, torch.nn.functional.affine_grid generates a 2D or 3D flow field (sampling…

3 months ago

How to create an Android mobile app with a deep learning AI model ?

Creating an Android mobile app with a deep learning AI model involves several key steps:…

12 months ago

Download source files for R Machine learning

Click here to go to source files for R Machine Learning

1 year ago