How to generate a probability density distribution from a set of observations in R?

R ProgrammingServer Side ProgrammingProgramming

The probability density distribution is the synonym of probability density function. It is a function that defines the density of a continuous random variable. In R, we can use density function to create a probability density distribution from a set of observations.

Example

 Live Demo

x1<-c(1,5,2,7,8,2,5)
pdf_x1<-density(x1,from=1,to=8)
pdf_x1

Output

Call:
   density.default(x = x1, from = 1, to = 8)
Data: x1 (7 obs.); Bandwidth 'bw' = 1.641
      x          y
Min. :1.00    Min. :0.07678
1st Qu.:2.75  1st Qu.:0.10308
Median :4.50  Median :0.10660
Mean :4.50    Mean :0.10421
3rd Qu.:6.25 3rd Qu.:0.10809
Max. :8.00   Max. :0.11238

Example

 Live Demo

x2<-sample(0:9,150,replace=TRUE)
x2

Output

[1] 5 0 5 1 7 4 2 0 1 1 4 6 4 9 8 5 3 1 5 7 7 0 6 3 2 0 9 6 1 0 1 3 2 1 7 0 3
[38] 4 2 0 7 1 9 2 0 7 6 7 2 2 2 2 7 0 5 3 8 9 4 0 5 7 2 3 9 7 1 3 5 8 0 1 9 9
[75] 4 1 0 5 2 8 5 8 8 6 0 1 2 6 5 0 5 3 2 0 3 4 9 5 2 4 4 0 7 0 1 1 0 0 8 1 4
[112] 0 8 0 1 4 5 2 1 1 3 3 0 2 5 7 1 6 9 9 8 5 4 1 7 7 9 3 3 2 0 4 2 7 1 6 1 9
[149] 7 4

Example

pdf_x2<-density(x2,from=0,to=9)
pdf_x2

Output

Call:
   density.default(x = x2, from = 0, to = 9)
Data: x2 (150 obs.); Bandwidth 'bw' = 0.968
      x          y
Min. :0.00    Min. :0.05290
1st Qu.:2.25  1st Qu.:0.08010
Median :4.50  Median :0.09063
Mean :4.50    Mean :0.09423
3rd Qu.:6.75  3rd Qu.:0.11063
Max. :9.00    Max. :0.13117

Example

 Live Demo

x3<-sample(1:100,150,replace=TRUE)
x3

Output

[1] 82 59 78 79 52 1 79 15 53 29 54 65 85 26 41 62 8 33
[19] 91 2 66 59 32 63 84 67 82 13 46 5 11 52 53 12 52 38
[37] 73 10 33 84 67 67 55 92 45 48 54 38 96 69 25 22 64 73
[55] 19 68 87 71 62 91 99 56 81 83 3 32 49 77 14 26 73 60
[73] 80 13 18 97 100 75 45 4 46 33 86 96 23 47 57 48 64 34
[91] 9 90 49 79 20 13 99 7 76 37 91 91 7 98 52 95 17 97
[109] 30 50 63 100 5 47 48 90 3 74 94 78 49 9 19 100 60 13
[127] 27 6 51 8 69 92 28 91 55 89 85 72 17 77 16 52 72 56
[145] 60 70 84 84 12 4

Example

pdf_x3<-density(x3,from=1,to=99)
pdf_x3

Output

Call:
   density.default(x = x3, from = 1, to = 99)
Data: x3 (150 obs.); Bandwidth 'bw' = 9.781
      x          y
Min. : 1.0    Min. :0.005490
1st Qu.:25.5  1st Qu.:0.007753
Median :50.0  Median :0.008883
Mean :50.0    Mean :0.009197
3rd Qu.:74.5  3rd Qu.:0.010991
Max. :99.0    Max. :0.011267

Example

 Live Demo

x4<-sample(111:999,150,replace=TRUE)
x4

Output

[1] 393 314 643 304 241 512 146 497 563 982 891 732 372 749 718 813 304 994
[19] 760 937 835 182 357 676 111 139 574 293 248 973 700 173 676 636 760 217
[37] 962 265 421 200 245 665 901 294 433 958 277 771 229 306 361 208 346 466
[55] 114 921 816 278 701 543 563 806 531 335 588 838 961 206 956 608 670 924
[73] 361 236 722 897 734 927 485 817 701 550 492 524 909 566 400 950 385 500
[91] 176 192 576 951 634 801 813 137 889 221 747 354 974 973 315 444 317 569
[109] 408 878 950 475 418 187 300 570 380 730 415 328 490 274 289 674 576 440
[127] 473 565 864 552 303 232 242 115 611 732 880 460 981 288 198 494 137 995
[145] 736 493 555 920 948 686

Example

pdf_x4<-density(x4,from=111,to=999)
pdf_x4

Output

Call:
   density.default(x = x4, from = 111, to = 999)
Data: x4 (150 obs.); Bandwidth 'bw' = 87.18
         x       y
Min. :111   Min. :0.0005505
1st Qu.:333 1st Qu.:0.0009550
Median :555 Median :0.0010272
Mean :555   Mean :0.0010283
3rd Qu.:777 3rd Qu.:0.0011339
Max. :999   Max. :0.0012438

Example

 Live Demo

x5<-sample(1:1000,50)
x5

Output

[1] 602 716 178 950 55 730 956 916 778 702 49 20 888 259 153 894 150 979 735
[20] 455 543 424 296 697 495 505 957 710 493 725 125 947 379 401 752 502 421 664
[39] 102 620 116 345 202 760 193 868 97 459 703 32

Example

pdf_x5<-density(x5,from=1,to=1000)
pdf_x5

Output

Call:
   density.default(x = x5, from = 1, to = 1000)
Data: x5 (50 obs.); Bandwidth 'bw' = 123.9
         x          y
Min. : 1.0       Min. :0.0005498
1st Qu.: 250.8   1st Qu.:0.0008195
Median : 500.5   Median :0.0008665
Mean : 500.5     Mean :0.0008907
3rd Qu.: 750.2   3rd Qu.:0.0009993
Max. :1000.0    Max. :0.0010921
raja
Published on 07-Dec-2020 05:42:47
Advertisements