Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the probability Distribution function in R language

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "how to use the probability distribution function in R language". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

There are many probability functions in R, including probability density function and probability distribution function, which can be called or random numbers can be generated. The rules are as follows:

[dpqr] distribution_abbreviation ()

The preceding letter is a function type, which means as follows:

D = density function (density)

P = distribution function (distributionfunction)

Q = quantile function (quantilefunction)

R = generate random number (random deviation)

Distribution_abbreviation is an abbreviation for the name of the probability distribution, and the type of probability distribution in R is as follows:

For probability density function and distribution function, the use methods are as follows: for example, the probability density function of normal distribution is dnorm (), the probability distribution function pnorm (), and generate random number rnorm () which accords with normal distribution.

X=pretty (c (- 3), 100) y=dnorm (x) plot (x, y)

The result of the drawing is as follows:

For generating random numbers, examples of how to use them are as follows:

Rnorm (n, mean=0, sd=1) # produces n normal distribution numbers, mena average, sd standard deviation runif (n, min=0, max=1) # produces n uniformly distributed numbers, min minimum, max maximum

Next, we generate random numbers that conform to normal distribution and draw graphs:

Library (ggplot2) y=rnorm (10000, mean=0, sd=1) x=1:10000data=data.frame (x, y) ggplot (data, aes (x)) + geom_histogram (position='identity', alpha=0.5, binwidth=0.1, aes (density..), fill= "cyan") + stat_density (geom='line', position='identity', color= "cyan4")

The result of the drawing is as follows:

You can see that the data distribution image is basically the same as the normal function image. R can also generate multi-dimensional random variables. For example, the mvrnorm () function in the MASS package can generate random variables with one-dimensional or multi-dimensional normal distribution. The usage is as follows:

Mvrnorm (nasty 1, mu, Sigma...)

Where n is the number of random numbers, mu is the numerical vector, the mean value is given, and the Sigma is a symmetric numerical matrix. The covariance matrix is given.

When multiple random variables obey normal distribution, it is multivariate normal. Next, we generate two-dimensional positive random variables with different correlation coefficients (1, 0.75, 0.25, 0), and draw their density maps to observe the differences, as follows:

Library (MASS) # correlation coefficient 1Sigma1=matrix (c (1,1,1,1), 2,2) X1=mvrnorm (nude 1000, c (0,0), Sigma1) # a total of 1000 points K1=kde2d (X1 [, 1], X1 [, 2], nim100) # n is the number of points used to estimate the density Cannot be too large col.palette=colorRampPalette (c ("white", "red")) # Custom Color filled.contour (K1, color=col.palette) # correlation coefficient 0.75Sigma2=matrix (c (1,0.75,0.75,1), 2,2) X2=mvrnorm (nasty 1000, c (0,0), Sigma2) K2=kde2d (X2 [, 1], X2 [, 2], Numb100) filled.contour (K2, color=col.palette) # correlation coefficient 0.25Sigma3=matrix (c (1,0.25,0.25,1), 2 2) X3=mvrnorm (nasty 1000, c (0,0), Sigma3) K3=kde2d (X3 [, 1], X3 [, 2], nasty 100) filled.contour (K3, color=col.palette) # correlation coefficient 0Sigma4=matrix (c (1,0,0,1), 2,2) X4=mvrnorm (nasty 1000, c (0,0), Sigma4) K4=kde2d (X4 [, 1], X4 [, 2], nasty 100) filled.contour (K4, color=col.palette)

The result of the drawing is as follows:

In the above program, the kde2d () function estimates the distribution density of data points according to two-dimensional coordinates and draws isodensity lines (you can use contour (K4, lwd=1,add=T, xlim, ylim...). Function to add boundary lines and mark the data scale), then customize the color and fill it in to form an image, which actually shows three-dimensional information. But we can still use the persp () function to show the 3D picture:

Persp (K1, col= "orange", theta=95, phi=30, dumb2)

This is the end of the content of "how to use the probability distribution function in R language". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report