Data-Distribution
April James Palermo
2023-06-
# Mindanao State University
# General Santos City
# Mathematics Department
# June 5, 2023
# Probability Distributions
# Submitted by: John Michael H. Macawili
# Submitted t0: Prof. Carlito Daarol
# for BS Math
# 1. Normal Probability Distributions
# Four functions:
# dnorm for density function of the distribution
# pnorm,for cumulative density of the distribution
# qnorm for the quantile values from 0 to 100%
# rnorm for random samples generation
# Specify x-values for dnorm function
x_dnorm <- seq(- 5, 5, by = 0.005)
y_dnorm <- dnorm(x_dnorm) # Apply dnorm function
y_pnorm <- pnorm(x_dnorm) # apply pnorm function
x_qnorm <- seq(0, 1, by = 0.005)
y_qnorm <- qnorm(x_qnorm) # apply qnorm function
set.seed(123) # mark reference point for random sampling
N = 500 # Number of random samples needed
y_rnorm <- rnorm(N) # Draw N normally distributed values
y_rnorm
## [1] -0.560475647 -0.230177489 1.558708314 0.070508391 0.129287735
## [6] 1.715064987 0.460916206 -1.265061235 -0.686852852 -0.445661970
## [11] 1.224081797 0.359813827 0.400771451 0.110682716 -0.555841135
## [16] 1.786913137 0.497850478 -1.966617157 0.701355902 -0.472791408
## [21] -1.067823706 -0.217974915 -1.026004448 -0.728891229 -0.625039268
## [26] -1.686693311 0.837787044 0.153373118 -1.138136937 1.253814921
## [31] 0.426464221 -0.295071483 0.895125661 0.878133488 0.821581082
## [36] 0.688640254 0.553917654 -0.061911711 -0.305962664 -0.380471001
## [41] -0.694706979 -0.207917278 -1.265396352 2.168955965 1.207961998
## [46] -1.123108583 -0.402884835 -0.466655354 0.779965118 -0.083369066
## [51] 0.253318514 -0.028546755 -0.042870457 1.368602284 -0.225770986
## [56] 1.516470604 -1.548752804 0.584613750 0.123854244 0.215941569
## [61] 0.379639483 -0.502323453 -0.333207384 -1.018575383 -1.071791226
## [66] 0.303528641 0.448209779 0.053004227 0.922267468 2.050084686
## [71] -0.491031166 -2.309168876 1.005738524 -0.709200763 -0.688008616
## [76] 1.025571370 -0.284773007 -1.220717712 0.181303480 -0.138891362
## [81] 0.005764186 0.385280401 -0.370660032 0.644376549 -0.220486562
## [86] 0.331781964 1.096839013 0.435181491 -0.325931586 1.148807618
## [91] 0.993503856 0.548396960 0.238731735 -0.627906076 1.360652449
## [96] -0.600259587 2.187332993 1.532610626 -0.235700359 -1.026420900
## [101] -0.710406564 0.256883709 -0.246691878 -0.347542599 -0.951618567
## [106] -0.045027725 -0.784904469 -1.667941937 -0.380226520 0.918996609
## [111] -0.575346963 0.607964322 -1.617882708 -0.055561966 0.519407204
## [116] 0.301153362 0.105676194 -0.640706008 -0.849704346 -1.024128791
## [121] 0.117646597 -0.947474614 -0.490557444 -0.256092192 1.843862005
## [126] -0.651949902 0.235386572 0.077960850 -0.961856634 -0.071308086
## [131] 1.444550858 0.451504053 0.041232922 -0.422496832 -2.053247222
## [136] 1.131337213 -1.460640071 0.739947511 1.909103569 -1.443893161
## [141] 0.701784335 -0.262197489 -1.572144159 -1.514667654 -1.601536174
## [146] -0.530906522 -1.461755585 0.687916773 2.100108941 -1.287030476
## [151] 0.787738847 0.769042241 0.332202579 -1.008376608 -0.119452607
## [156] -0.280395335 0.562989533 -0.372438756 0.976973387 -0.374580858
## [161] 1.052711466 -1.049177007 -1.260155245 3.241039935 -0.416857588
## [166] 0.298227592 0.636569674 -0.483780626 0.516862044 0.368964527
## [171] -0.215380508 0.065293034 -0.034067254 2.128451899 -0.741336096
## [176] -1.095996267 0.037788399 0.310480749 0.436523479 -0.458365333
## [181] -1.063326134 1.263185176 -0.349650388 -0.865512863 -0.236279569
## [186] -0.197175894 1.109920290 0.084737292 0.754053785 -0.499292017
## [191] 0.214445310 -0.324685911 0.094583528 -0.895363358 -1.310801533
## [196] 1.997213385 0.600708824 -1.251271362 -0.611165917 -1.185480085
## [201] 2.198810349 1.312412976 -0.265145057 0.543194059 -0.414339948
## [206] -0.476246895 -0.788602838 -0.594617267 1.650907467 -0.054028125
## [211] 0.119245236 0.243687430 1.232475878 -0.516063831 -0.992507150
## [216] 1.675696932 -0.441163217 -0.723065970 -1.236273119 -1.284715722
## [221] -0.573973479 0.617985817 1.109848139 0.707588354 -0.363657297
## [226] 0.059749937 -0.704596464 -0.717218162 0.884650499 -1.015592579
## [231] 1.955293965 -0.090319594 0.214538827 -0.738527705 -0.574388690
## [236] -1.317016132 -0.182925388 0.418982405 0.324304344 -0.781536487
## [241] -0.788621971 -0.502198718 1.496060670 -1.137303621 -0.179051594
## [246] 1.902361822 -0.100974885 -1.359840704 -0.664769435 0.485459979
## [251] -0.375602872 -0.561876364 -0.343917234 0.090496647 1.598508771
## [256] -0.088565112 1.080799496 0.630754116 -0.113639896 -1.532902003
## [261] -0.521117318 -0.489870453 0.047154433 1.300198678 2.293078974
## [266] 1.547581059 -0.133150964 -1.756527396 -0.388779864 0.089207223
## [271] 0.845013004 0.962527968 0.684309429 -1.395274350 0.849643046
## [276] -0.446557216 0.174802700 0.074551177 0.428166765 0.024674983
## [281] -1.667475098 0.736495965 0.386026568 -0.265651625 0.118144511
## [286] 0.134038645 0.221019469 1.640846166 -0.219050379 0.168065384
## [291] 1.168383873 1.054181023 1.145263110 -0.577468001 2.002482730
## [296] 0.066700871 1.866851845 -1.350902686 0.020983586 1.249914571
## [301] -0.715242187 -0.752688968 -0.938538704 -1.052513279 -0.437159533
## [306] 0.331179173 -2.014210498 0.211980433 1.236675046 2.037574018
## [311] 1.301175992 0.756774764 -1.726730399 -0.601506708 -0.352046457
## [316] 0.703523903 -0.105671334 -1.258648628 1.684435708 0.911391292
## [321] 0.237430272 1.218108610 -1.338774287 0.660820298 -0.522912376
## [326] 0.683745522 -0.060821955 0.632960713 1.335517615 0.007290090
## [331] 1.017558637 -1.188434035 -0.721604440 1.519217711 0.377387973
## [336] -2.052222820 -1.364037452 -0.200781016 0.865779404 -0.101883256
## [341] 0.624187472 0.959005378 1.671054829 0.056016733 -0.051981906
## [346] -1.753237359 0.099327594 -0.571850058 -0.974009583 -0.179906231
## [351] 1.014943173 -1.992748489 -0.427279287 0.116637284 -0.893207570
## [356] 0.333902942 0.411429921 -0.033036159 -2.465898194 2.571458146
## [361] -0.205299257 0.651193282 0.273766491 1.024673235 0.817659446
## [366] -0.209793171 0.378167772 -0.945408831 0.856923011 -0.461038339
## [371] 2.416773354 -1.651048896 -0.463987243 0.825379863 0.510132547
## [376] -0.589481039 -0.996780742 0.144475705 -0.014307413 -1.790281237
## [381] 0.034551067 0.190230316 0.174726397 -1.055017043 0.476133278
## [386] 1.378570137 0.456236403 -1.135588470 -0.435645470 0.346103620
## [391] -0.647045631 -2.157646335 0.884250820 -0.829477612 -0.573560271
## [396] 1.503900609 -0.774144930 0.845731540 -1.260682879 -0.354542403
## [401] -0.073556019 -1.168651424 -0.634748265 -0.028841553 0.670695969
## [406] -1.650546543 -0.349754239 0.756406439 -0.538809160 0.227291922
## [411] 0.492228570 0.267835015 0.653257679 -0.122708661 -0.413676514
## [416] -2.643148952 -0.092941018 0.430284696 0.535398841 -0.555278351
## [421] 1.779502910 0.286424420 0.126315858 1.272266779 -0.718466221
## [426] -0.450338624 2.397452480 0.011129187 1.633568421 -1.438506645
## [431] -0.190516802 0.378423904 0.300038545 -1.005636260 0.019259275
## [436] -1.077420653 0.712703325 1.084775090 -2.224987696 1.235693462
## [441] -1.241044497 0.454769269 0.659902638 -0.199889828 -0.645113957
## [446] 0.165321021 0.438818701 0.883302820 -2.052336984 -1.636379268
## [451] 1.430402341 1.046628847 0.435288949 0.715178407 0.917174918
## [456] -2.660922798 1.110277097 -0.484987597 0.230616831 -0.295157801
## [461] 0.871964954 -0.348472449 0.518503766 -0.390684979 -1.092787209
## [466] 1.210010510 0.740900011 1.724262239 0.065153933 1.125002746
## [471] 1.975419054 -0.281482115 -1.322951113 -0.239351567 -0.214041240
## [476] 0.151680505 1.712304977 -0.326143893 0.373004656 -0.227684065
## [481] 0.020450709 0.314057664 1.328214696 0.121318377 0.712842320
## [486] 0.778860030 0.914773271 -0.574394552 1.626881214 -0.380956739
## [491] -0.105784168 1.404050268 1.294083906 -1.089991872 -0.873071000
## [496] -1.358079059 0.181847193 0.164840868 0.364114687 0.552157714
# combine plots
par(mfrow=c(2,2))
plot(y_dnorm, main="Density plot")
plot(y_pnorm, main="Cumulative Density plot")
plot(y_qnorm, main="Quantile plot")
plot(y_rnorm, main="Scatter Plot Random Samples")
par(mfrow=c(1,2))
plot(density(y_rnorm),main = "Density plot")
hist(y_rnorm, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the mean and sd
y_rnorm2 <- rnorm(N, mean = 2) # Modify mean
y_rnorm3 <- rnorm(N, mean = 2, sd = 3) # Modify standard deviation
plot(density(y_rnorm), # Plot default density
xlim = c(- 10, 10),
main = "Normal Distribution in R with different Mean and SD",lwd=3)
lines(density(y_rnorm2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rnorm3), col = "green3",lwd=3) # Plot density with higher sd
legend("topleft", # Add legend to density
legend = c("Mean = 0; SD = 1",
"Mean = 2; SD = 1",
"Mean = 2; SD = 3"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 2. Exponential Probability Distributions
# Four functions:
# dexp for density function of the distribution
# pexp,for cumulative density of the distribution
# qexp for the quantile values from 0 to 100%
# rexp for random samples generation
# Specify x-values for dnorm function
x_dexp <- seq(0, 1, by = 0.02)
y_dexp <- dexp(x_dexp, rate = 5) # Apply dexp function
y_pexp <- pexp(x_dexp, rate = 5) # Apply pexp function
x_qexp <- seq(0, 1, by = 0.02) # Specify x-values for qexp function
y_qexp <- qexp(x_qexp, rate = 5) # Apply qexp function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rexp <- rexp(N, rate = 5) # Draw N exp distributed values
y_rexp # Print values to RStudio console
## [1] 0.4304628488 0.0448754426 0.4188465442 0.0153726040 0.0827383894
## [6] 0.0298834610 0.1230255631 0.3210040148 0.0847058907 0.2575912395
## [11] 0.3739378393 0.1088725767 0.1160947043 0.0725033843 0.2358778002
## [16] 0.3173701042 0.1272207452 0.1935283599 0.0141272598 0.1684437008
## [21] 0.0155475004 0.0055278600 0.3883418362 0.2278105611 0.0981377993
## [26] 0.3726578541 0.1501526580 0.6399115041 0.2175315399 0.1263600907
## [31] 0.6171591162 0.1058328426 0.1398303663 0.1693609228 0.1670840686
## [36] 0.0674714735 0.5650798767 0.2251611600 0.0885720395 0.0052224816
## [41] 0.2611994296 0.0389628253 0.0197845028 0.4255702410 0.3049375232
## [46] 0.1177449713 0.0862239982 0.8296917688 0.0812133478 0.4898280773
## [51] 0.0613636166 0.2585077999 0.0240652268 0.1646993317 0.2178937102
## [56] 0.1532683572 0.0720650758 0.5923673778 0.0096594410 0.1028167995
## [61] 0.6824885294 0.3997993845 0.3409431320 0.0307949640 0.0164071033
## [66] 0.0322857643 0.3055676296 0.2016660478 0.0736791726 0.2058191683
## [71] 0.0768022887 0.1196684984 0.1184952311 0.0310586396 0.0014292382
## [76] 0.6211629823 0.0087189434 0.0785796145 0.9568131245 0.8276564609
## [81] 0.1102485528 0.6944493156 0.0722134339 0.6586548239 0.1733822353
## [86] 0.0199734278 0.0616167603 0.4070798065 0.0483530850 0.0013432059
## [91] 0.1061864716 0.0255281447 0.0076782957 0.0878149213 0.1487272373
## [96] 0.3076300926 0.3635651979 0.2607039383 0.4049626168 0.0023850450
## [101] 0.0275327469 0.0255780491 0.0873389206 0.1771615244 0.4525101351
## [106] 0.1938755628 0.1764767898 0.0768817023 0.2603782331 0.0317894939
## [111] 0.4251929324 0.0137899341 0.0239018593 0.5288688708 0.1166718173
## [116] 0.1080313025 0.1214896392 0.8292544853 0.2958068255 0.1918629710
## [121] 0.1108254925 0.2515561925 0.3556356992 0.0568493435 0.1171209813
## [126] 0.2426554870 0.1413106811 0.1930961001 0.5164853055 0.5473405216
## [131] 0.3302129131 0.1646470405 0.0113127667 0.0951108197 0.0307912551
## [136] 0.5057907682 0.7068698906 0.0889587999 0.1508297691 0.0272709093
## [141] 0.1021028167 0.2102280924 0.7050123673 0.4989409018 0.1324037685
## [146] 0.0664685049 0.1862607503 0.0184189227 0.0827031768 0.0450254944
## [151] 0.1140153416 0.2472450382 0.3070011210 0.2283963766 0.1318053074
## [156] 0.0830450808 0.2227569375 0.0899205279 0.5591009885 0.5272008944
## [161] 0.1388426943 0.0256807393 0.1056487754 0.7169774395 0.3098292805
## [166] 0.0673184106 0.1414918849 0.2599173700 0.1424148330 0.0522114006
## [171] 0.0530554689 0.6248315889 0.0849715661 0.0140833135 0.0224374436
## [176] 0.0902417049 0.3273684759 0.2853232305 0.1732028663 0.4075901169
## [181] 0.1213969490 0.3029242510 0.0596386860 0.4061955150 0.1829868693
## [186] 0.2544053281 0.4420034920 0.2161032721 0.0030373452 0.0543097503
## [191] 0.6199684396 0.2549603272 0.0638505688 0.1145902527 0.0838142803
## [196] 0.4868816558 0.1511204811 0.1144174033 0.1328482389 0.1773398299
## [201] 0.0545331534 0.4169562129 0.1652882449 0.1310331495 0.1592402477
## [206] 0.2527422102 0.1412797225 0.0487022399 0.1960320292 0.0314396822
## [211] 0.0460019010 0.2307577960 0.2426191565 0.1061809766 0.0730929984
## [216] 0.0842513132 0.1267093855 0.8635762847 0.0773268241 0.2499718424
## [221] 0.0487732232 0.2539918855 0.3645360473 0.0421084581 0.1432923434
## [226] 0.1201757259 0.2137594227 0.1288114905 0.1385845669 0.3111416597
## [231] 0.0897289120 0.2427038202 0.0431473328 0.0469002625 0.0645122139
## [236] 0.2143995274 0.0570541609 0.7066055374 0.1569837265 0.1284756387
## [241] 0.1202070106 0.0723165593 0.2149657880 0.0782336540 0.1797724459
## [246] 0.0813856773 0.0340854152 0.4005367316 0.0563217903 0.5371036935
## [251] 0.0325744237 0.3562796313 0.4937017219 0.3850665290 0.1567942000
## [256] 0.2483538860 0.0858322757 0.2985452903 0.0901317216 0.2196495898
## [261] 0.0091673187 0.1352015890 0.0546016017 0.6348074357 0.1006552963
## [266] 0.1852296788 0.0288328023 0.2937624822 0.0544150706 0.5330004137
## [271] 0.6725561797 0.4207927339 0.3294540550 0.0772444182 0.2236726273
## [276] 0.0398876030 0.2496276176 0.1532954302 0.3509468425 0.0742886413
## [281] 0.0570498028 0.0098567866 0.1011184495 0.2833714213 0.1426737024
## [286] 0.0063347727 0.3137424398 0.1673803572 0.8097143363 0.2124193327
## [291] 0.2337052032 0.2439626664 0.6029629754 0.1745762298 0.0106267033
## [296] 0.0073621755 0.6655705362 0.0049850661 0.0777947573 0.0289100790
## [301] 0.0206048008 0.3506586507 0.3353626208 0.4485305410 0.0477521324
## [306] 0.0415110793 0.0246349973 0.2625822289 0.3586899824 0.0042128112
## [311] 0.2647775626 0.1379126545 1.1543510645 0.1590243380 0.2156649040
## [316] 0.7798075585 0.1162836884 0.0333814253 0.2202161438 0.1887542143
## [321] 0.0646329265 0.0833530828 0.1246361816 0.1979742471 0.7795213263
## [326] 0.1683855561 0.1560781082 0.0474177793 0.1464220785 0.3462393716
## [331] 0.4283343385 0.0026381479 0.1895566607 0.5723638177 0.3467193618
## [336] 0.0135015411 0.3337987690 0.0809423300 0.2371012144 0.2314168027
## [341] 0.2882200815 0.0773507793 0.4079233546 0.0111489639 0.2925952103
## [346] 0.2054514628 0.2164098593 0.3069865256 0.8378651179 0.3977851588
## [351] 0.0336689595 0.5142843146 0.0387177444 0.4913256865 0.1369044920
## [356] 0.0893550012 0.2803781373 0.1058670356 0.0732210807 0.5868808247
## [361] 0.1732304206 0.1102985973 0.0421838904 0.2418177389 0.0267825000
## [366] 0.0344193194 0.1095962471 0.2068268100 0.0618211612 0.3348801382
## [371] 0.0772066904 0.2542398667 0.1329377643 0.1184467050 0.1542118026
## [376] 0.0547203029 0.2514023287 0.2472443636 0.0754429908 0.0420750115
## [381] 0.1008910955 0.0170832227 0.0168763004 0.0004918388 0.0042977575
## [386] 0.2026334289 0.0586680545 0.0103612226 0.2579171026 0.1733188508
## [391] 0.1836545003 0.0821446788 0.0846461438 0.4030356083 1.0693119976
## [396] 0.1023346175 0.2257530831 0.5329443607 0.2831235550 0.2028801538
## [401] 0.0663843918 0.6568965494 0.0282981419 0.2977480639 0.3474905591
## [406] 0.0304886659 0.1754238224 1.0238484833 0.1354234412 0.1455202297
## [411] 0.1030377412 0.2173267210 0.0089897444 0.0674196985 0.3920407969
## [416] 0.5834699750 0.2719552169 0.1441609248 0.0608470227 0.7518897602
## [421] 0.1790439365 0.2399779348 0.1660038793 0.0686552907 0.0818977684
## [426] 0.1416995300 0.0884066727 0.1218154470 0.4442474525 0.0547785508
## [431] 0.1171480665 0.2543631155 0.4062047168 0.1759643372 0.0706204500
## [436] 0.2064570237 0.2998209726 0.1120997464 1.1455948322 0.3944598432
## [441] 0.1724202409 0.1618034024 0.0167343437 0.2507952835 0.1682600121
## [446] 0.1696957165 0.1552093657 0.0675675932 0.1112660250 0.1117089240
## [451] 0.0655837063 0.0003945209 0.1214717711 0.0950748889 0.0373791118
## [456] 0.0715208080 0.0906362925 0.1423168453 0.0050502124 0.5017117683
## [461] 0.2234110684 0.1792354776 0.0192700613 0.2503400892 0.1521765888
## [466] 0.1205337527 0.1500588810 0.0561385386 0.1145136865 0.0498821461
## [471] 0.3640119627 0.2176512760 0.2189045073 0.0386907753 0.1160453607
## [476] 0.0471176780 0.0791355430 0.5701856866 0.1404138455 0.2438570937
## [481] 0.0180721804 0.2401638253 0.1362552669 0.6145147353 0.0806840136
## [486] 0.5471450347 0.1696879991 0.0536053098 0.0834799652 0.0619588327
## [491] 0.0761097581 0.2727608586 0.5146991647 0.2406466970 0.1777100738
## [496] 0.0434884771 0.2694532100 0.0727838154 0.0045895175 0.3742320236
# combine plots
par(mfrow=c(2,2))
plot(y_dexp, main="Exp Density plot")
plot(y_pexp, main="Exp Cumulative Density plot")
plot(y_qexp, main="Exp Quantile plot")
plot(y_rexp, main="Exp Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rexp),main = "Density plot")
hist(y_rexp, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the mean and sd
y_rexp2 <- rexp(N, rate = 4) # Modify rate parameter
y_rexp3 <- rexp(N, rate = 3) # Modify rate parameter
plot(density(y_rexp), # Plot default density
xlim = c(- 0, 2),
main = "Exponential Distribution in R with different Rates",lwd=3)
lines(density(y_rexp2), col = "coral2",lwd=3) # Plot density with higher
mean
## function (x, ...)
## UseMethod("mean")
## <bytecode: 0x000001c701a7f8a0>
## <environment: namespace:base>
lines(density(y_rexp3), col = "green3",lwd=3) # Plot density with higher
sd
## function (x, na.rm = FALSE)
## sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x),
## na.rm = na.rm))
## <bytecode: 0x000001c701655d58>
## <environment: namespace:stats>
legend("topright", # Add legend to density
legend = c("Rate = 5",
"Rate = 1",
"Rate = 7"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 3. Gamma Probability Distributions
# Four functions:
# dgamma for density function of the distribution
# pgamma,for cumulative density of the distribution
# qgamma for the quantile values from 0 to 100%
# rgamma for random samples generation
# Specify x-values for dnorm function
x_dgamma <- seq(0, 1, by = 0.02)
y_dgamma <- dgamma(x_dgamma, shape = 5) # Apply dgamma function
y_pgamma <- pgamma(x_dgamma, shape = 5) # Apply pgamma function
y_qgamma <- qgamma(x_dgamma, shape = 5) # Apply qexp function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rgamma <- rgamma(N, shape = 5) # Draw N exp distributed values
y_rgamma # Print values to RStudio console
## [1] 2.2619035 7.6577021 3.5820966 7.1282469 11.3278857 6.3054247
## [7] 4.9084750 2.5859772 2.8018675 5.5551225 3.0205932 6.6321869
## [13] 4.6900098 10.2150294 2.9656552 2.8576933 2.7968535 2.7062162
## [19] 3.7125822 6.1597296 4.1598487 3.3787262 5.0198022 4.3011006
## [25] 5.8315871 3.9952016 7.4825583 7.7893843 6.3006206 5.3758627
## [31] 4.8268603 3.7149404 1.5105046 3.6061853 7.6509812 5.3834126
## [37] 4.9425340 3.6088598 7.3372068 6.9595115 6.4285059 3.0346045
## [43] 1.5762033 11.1333631 6.2079260 5.3798405 5.1766275 0.9067510
## [49] 4.8472696 4.4812780 4.3318290 2.5281669 2.9208437 6.4491629
## [55] 4.8479671 4.3088991 2.2963145 3.5575716 3.9899019 4.6854155
## [61] 2.1733961 6.8741877 6.4964821 2.4757186 5.2287928 3.9329626
## [67] 5.4189461 6.3979260 4.1432637 4.3264086 2.1399939 3.3622265
## [73] 5.5870983 9.1038475 4.0756697 6.0515652 5.8385487 4.8716793
## [79] 3.6649427 6.7620868 7.0096544 6.3693731 7.9862967 3.9535113
## [85] 5.0336028 3.5877962 4.1628288 2.5254097 5.7337849 5.8559293
## [91] 2.1689394 6.1560193 7.1321903 2.7158266 3.2029694 5.2347017
## [97] 1.7661532 8.1865798 8.9742736 3.2917491 7.8753740 10.2226221
## [103] 3.7793176 4.2942361 2.9307119 5.3474951 7.5223804 4.9247445
## [109] 3.4853947 2.8203229 8.2189941 7.1338153 4.0953583 4.2127556
## [115] 7.2375464 4.2925197 7.5716487 2.0569475 6.7735782 5.2700421
## [121] 6.8429467 2.5608163 4.1524227 5.1818503 7.2699371 3.0505742
## [127] 3.6248066 6.9876878 10.6248015 5.7548695 0.8829102 4.8136845
## [133] 5.1829129 2.7648250 1.4553830 3.6879980 2.5434954 5.0994856
## [139] 3.7026684 4.3174498 5.3087679 2.1671946 6.6629725 7.1151571
## [145] 4.6159344 4.7741057 5.7273835 4.4663829 6.9219112 4.9681959
## [151] 5.2974791 9.8226053 4.9404616 2.7318709 4.8332257 4.1366726
## [157] 3.9122868 5.1662726 2.5942106 3.7263683 5.2710887 4.9337414
## [163] 6.0572464 7.1869377 3.4480402 2.3453137 1.6832571 1.4830366
## [169] 3.7525209 3.3903986 3.2847515 7.2528574 4.6025763 1.9251640
## [175] 2.4190445 1.3801018 4.1390299 8.7765547 3.3807291 3.8175805
## [181] 3.7169098 7.4501380 3.2301911 8.4150137 1.4920770 4.7788649
## [187] 3.0085726 1.9413858 4.3736278 3.0304845 2.7415340 6.9088722
## [193] 3.3239840 5.2020557 6.3794769 4.1553814 4.1134319 3.5237011
## [199] 8.6119374 10.2197372 6.1615253 8.0595701 2.6912255 2.1032603
## [205] 4.6994547 1.4618541 3.0206794 5.6973606 3.8027582 2.9319613
## [211] 4.7445876 4.2345945 2.5253457 8.1541408 4.0955898 3.9289413
## [217] 2.0697246 3.0107157 6.1604194 1.7586345 4.1227474 6.2513678
## [223] 3.8700609 6.2367430 5.3830555 7.5878997 5.9808334 6.7758999
## [229] 3.2910460 3.9051216 4.7386254 6.0618319 2.3772199 7.7308413
## [235] 4.5065406 6.7073731 4.1205229 5.3337594 4.5445551 5.8233492
## [241] 3.4207437 5.7618017 0.7403340 7.8539040 2.1842317 5.4555598
## [247] 4.8861174 5.6449637 2.6963263 7.2392077 4.8688931 12.5490914
## [253] 7.7594844 7.8574652 7.1195175 3.4590974 2.8762962 8.3179190
## [259] 5.0000548 5.8286154 1.9278215 13.0265616 3.9510813 4.3794748
## [265] 3.6402752 9.3048049 7.5218773 2.8810891 8.5538584 2.3580821
## [271] 5.2865345 4.1144997 3.2978899 6.1610412 6.4862883 6.2809848
## [277] 4.3579217 6.4960708 7.2770721 3.2918991 4.3214580 4.5673962
## [283] 4.4905614 5.3629985 4.3383917 8.2036767 3.5439098 7.0822256
## [289] 6.4745765 5.2946595 8.6342849 6.8052981 8.5252813 4.3737314
## [295] 3.7215201 6.8257055 5.1581980 1.4563881 4.7439769 6.8636790
## [301] 5.6929144 4.2321584 5.2558824 5.3852225 6.4573743 4.7650873
## [307] 2.3680894 5.7699280 3.4501801 5.5597539 2.8030186 3.1408392
## [313] 1.7089804 6.9163826 4.3872773 5.6687072 4.1102911 1.7718534
## [319] 4.8574285 7.2348552 6.8096987 11.7663499 2.3229334 4.4398820
## [325] 2.3985745 14.2805814 6.5789936 2.0490783 2.7406622 5.3523136
## [331] 2.5838893 2.0967372 4.2514650 13.2735718 6.9875994 5.4255802
## [337] 5.1173332 9.8036865 3.1336893 3.5319726 3.5573651 2.5271975
## [343] 5.1212184 1.8709189 10.2043269 4.4951996 4.8270663 3.0792340
## [349] 5.9030334 6.0866934 5.2926075 4.9681632 4.4658392 7.3772306
## [355] 2.2234433 1.8251228 4.5539988 4.7977142 4.0049073 4.6824781
## [361] 7.1878512 10.1654264 3.1781650 9.4219453 3.5112166 5.4195737
## [367] 4.8176267 2.7981530 2.4612930 5.0008216 4.5574986 4.5204613
## [373] 3.1313973 4.8873795 7.4611082 9.2875210 6.1380214 3.4101825
## [379] 1.7061712 3.1208682 7.2751703 2.5769827 3.1530830 2.7134803
## [385] 4.6830623 7.8263333 4.9713658 4.1689930 2.5187333 3.2612218
## [391] 5.8518209 4.5526350 7.6337311 7.7507162 6.2528374 7.9797907
## [397] 4.4029377 3.8419008 9.6975512 5.9554557 6.0640241 14.1983984
## [403] 8.7559559 2.2966704 5.2288881 2.2652019 5.8266524 7.9229671
## [409] 3.1850496 4.7501867 3.0183603 5.2055133 3.8271950 2.9138023
## [415] 8.5237288 2.3230928 8.2747745 0.3783881 5.9627771 2.4396949
## [421] 3.0341272 3.2816700 4.3663665 3.2290513 2.0044386 3.2546835
## [427] 11.6785798 5.3349872 4.7314934 4.2505279 2.3273075 4.0100272
## [433] 4.8853191 7.0201348 7.7089145 4.4596232 3.9565281 6.9006570
## [439] 2.4518486 3.4860498 2.2366819 3.7456024 2.1267804 3.9844992
## [445] 7.2446088 4.6645167 6.1746807 6.0865335 3.3277069 4.8810704
## [451] 2.2966088 3.6843535 5.5607338 4.5779172 8.9394882 6.5746949
## [457] 3.6179410 4.2195098 7.5692824 2.9528699 4.9717777 5.0813613
## [463] 5.8754701 4.1607690 6.1078245 4.0727262 3.6691526 9.7426014
## [469] 2.6582682 2.5054276 5.1203673 6.1227020 6.9624701 7.6336397
## [475] 2.3201855 1.9216373 4.0875402 5.2643153 7.7104369 3.0801709
## [481] 7.7134555 8.1638170 2.7391186 3.0687248 5.3768940 10.7704900
## [487] 6.7256832 8.6567082 1.4496390 6.9287287 2.6334968 5.4879249
## [493] 7.0442207 4.9147348 9.1187933 6.6748620 8.9361078 7.5351108
## [499] 1.7597606 8.2727004
# combine plots
par(mfrow=c(2,2))
plot(y_dgamma, main="Gamma Density plot")
plot(y_pgamma, main="Gamma Cumulative Density plot")
plot(y_qgamma, main="Gamma Quantile plot")
plot(y_rgamma, main="Gamma Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rgamma),main = "Density plot")
hist(y_rgamma, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rgamma2 <- rgamma(N, shape = 2) # Modify shape parameter
y_rgamma3 <- rgamma(N, shape = 3) # Modify shape parameter
plot(density(y_rgamma), # Plot default density
xlim = c(- 0, 10),
ylim = c(- 0, .35),
main = "Gamma Distribution in R with different Shapes",lwd=3)
lines(density(y_rgamma2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rgamma3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("Shape = 5",
"Shape = 2",
"Shape = 3"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 4. Beta Probability Distributions
# Four functions:
# dbeta for density function of the distribution
# pbeta,for cumulative density of the distribution
# qbeta for the quantile values from 0 to 100%
# rbeta for random samples generation
# Specify x-values for dbeta function
x_dbeta <- seq(0, 1, by = 0.02)
y_dbeta <- dbeta(x_dbeta, shape1 = 2, shape2 =5) # Apply dbeta function
y_pbeta <- pbeta(x_dbeta, shape1 = 2, shape2 =5) # Apply pbeta function
y_qbeta <- qbeta(x_dbeta, shape1 = 2, shape2 =5) # Apply qbeta function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rbeta <- rbeta(N, shape1 = 2, shape2 = 5) # Draw N exp distributed values
# combine plots
par(mfrow=c(2,2))
plot(y_dbeta, main="Gamma Density plot")
plot(y_pbeta, main="Gamma Cumulative Density plot")
plot(y_qbeta, main="Gamma Quantile plot")
plot(y_rbeta, main="Gamma Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rbeta),main = "Density plot")
hist(y_rbeta, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rbeta2 <- rbeta(N, shape1 = 5, shape2 = 2) # Modify shape parameter
y_rbeta3 <- rbeta(N, shape1 = 7, shape2 = 7) # Modify shape parameter
plot(density(y_rbeta), # Plot default density
xlim = c(- 0, 2),
ylim = c(- 0, 3),
main = "Gamma Distribution in R with different Shapes",lwd=3)
lines(density(y_rbeta2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rbeta3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("Shape1 = 2, shape2=5",
"Shape1 = 5, shape2=2",
"Shape1 = 7, shape2=7"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 5. Chi-Square Probability Distributions
# Four functions:
# dchisq for density function of the distribution
# pchisq,for cumulative density of the distribution
# qchisq for the quantile values from 0 to 100%
# rchisq for random samples generation
# Specify x-values for dnorm function
x_dchisq <- seq(0, 20, by = 0.01)
y_dchisq <- dchisq(x_dchisq, df = 3) # Apply dgamma function
y_pchisq <- pchisq(x_dchisq, df = 3) # Apply pgamma function
x_dchisq <- seq(0, 1, by = 0.01)
y_qchisq <- qchisq(x_dchisq, df = 3) # Apply qexp function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rchisq <- rchisq(N, df = 3) # Draw N exp distributed values
y_rchisq # Print values to RStudio console
## [1] 9.40875055 2.39449258 0.47390375 0.61063988 3.05346606 0.76055155
## [7] 4.22813263 2.18121985 8.60938778 0.72189112 0.64786568 0.60733259
## [13] 3.85738502 5.25352396 1.02763548 2.50508821 1.81485216 3.34653065
## [19] 1.53994122 5.32058188 2.31422368 1.29958709 1.53846829 3.07388630
## [25] 5.03972548 4.60117189 3.99952446 0.77051536 11.09975361 2.87102132
## [31] 2.66297030 0.97438692 9.58511164 2.33491566 1.82202004 0.31053377
## [37] 1.16993670 1.53529001 2.17679004 0.24911057 4.50328189 4.07550909
## [43] 0.40882732 2.71601149 1.48556911 2.91149035 3.96544527 1.67145199
## [49] 1.83814128 10.84706357 4.66938441 4.02486494 0.76251372 8.73986979
## [55] 5.70158601 4.28402436 1.68905146 0.43773623 3.24213731 3.37263247
## [61] 0.24698024 3.69817912 4.80063458 0.55473343 0.89342037 2.72203575
## [67] 0.08623424 6.05450146 7.02666720 0.96049100 5.67837805 8.61924646
## [73] 1.35375731 1.80854914 0.69764473 2.83765195 5.25771695 2.41057643
## [79] 1.11184120 0.62286495 6.09394871 4.80252016 1.62857503 1.73419055
## [85] 4.92319878 1.80697406 5.31603368 0.19579514 4.38843740 2.75813422
## [91] 4.00974470 0.45875695 1.67968454 2.66826916 4.96100896 0.78192253
## [97] 1.22483829 4.63359608 9.14422962 3.26457746 6.11886265 2.66934759
## [103] 0.58634909 4.76454979 2.58500924 1.28933392 1.82988799 2.79782366
## [109] 5.10224022 2.26271269 3.23533157 1.96837213 4.55797867 2.45366481
## [115] 2.78623964 8.10259274 2.42613999 0.56502042 2.32045904 1.66553449
## [121] 1.46762928 2.65247223 0.47889745 1.30915620 2.75920500 2.41948240
## [127] 3.59027295 4.86424338 1.08212524 2.00504782 6.44897562 0.95514994
## [133] 4.94106405 2.09732089 0.14170620 2.71284398 1.44311908 1.73813129
## [139] 0.16362987 7.29436551 0.55050084 1.61259287 1.27948111 4.75469346
## [145] 0.57872591 2.05315443 4.54302047 0.98521294 2.68879334 3.94491626
## [151] 1.68234630 1.64471538 1.14256421 6.57604073 8.61550038 3.70421549
## [157] 6.91122006 1.37971364 2.17300697 1.56100089 1.40502243 3.13862807
## [163] 5.12983401 1.65367214 1.75403713 0.43769853 6.01507458 1.62878151
## [169] 1.48207507 0.20140100 0.75355260 3.70300291 5.71393906 2.35147853
## [175] 3.78689980 2.87434325 5.33529802 3.50729683 4.39108038 0.95995393
## [181] 1.46142662 2.22823674 3.59526631 0.35364592 5.50534980 2.00616905
## [187] 4.31321548 1.65105986 2.82351005 2.04212295 3.33770802 1.06056483
## [193] 3.27196332 10.26585822 3.21413594 1.88555689 2.98764668 5.33299335
## [199] 0.89450408 0.14272733 12.37941712 1.50134073 1.88723251 1.23757404
## [205] 7.44252558 5.25712210 0.66368283 6.50434193 0.34326324 2.77502006
## [211] 1.64567030 0.96518556 3.70368460 4.06409428 3.83572403 1.86725197
## [217] 4.07504842 4.96934595 0.96060553 1.83357930 2.06380655 1.99110640
## [223] 2.85363432 1.84919636 6.07530162 1.15887268 4.74273662 4.05098834
## [229] 2.78334812 6.60366867 4.42457647 6.46911889 1.88190257 1.30509407
## [235] 4.44786060 2.64429315 6.22834731 4.49125663 3.19874276 1.75182019
## [241] 2.74365686 2.87658283 4.03175534 2.25393665 0.34867833 3.28062628
## [247] 1.08382098 3.05832296 0.61140000 0.84739619 0.06916182 4.55163499
## [253] 1.89447981 3.17310550 1.64190736 6.27133478 1.94353395 0.36536351
## [259] 11.43281605 2.84261702 0.47264031 0.21345420 1.76941064 9.05192457
## [265] 1.67488721 3.06676889 1.54592061 1.54252576 8.21569440 3.44653629
## [271] 0.61142602 2.45363230 1.96786170 5.08668112 10.91033243 0.87495420
## [277] 7.59095400 1.13252367 2.91214090 2.30518633 0.60818915 0.40056714
## [283] 1.90707014 2.05440314 2.01931560 0.84046939 2.37367838 5.18537588
## [289] 7.42067081 3.67846312 1.05225825 1.58791362 6.64439628 5.35359710
## [295] 6.16092871 2.75834116 0.50649665 1.56081242 3.10216977 3.36822383
## [301] 2.04978656 5.14793534 0.32135644 4.16446275 4.89426116 0.36479863
## [307] 1.09238204 1.22785925 2.31187268 3.18676316 2.71610860 0.29451038
## [313] 3.34124506 5.73558789 0.88006749 2.23945583 0.75896752 2.69230938
## [319] 1.39445491 0.68600927 6.46720639 0.32457497 6.16194856 3.36246320
## [325] 0.95280092 1.87507381 0.91296744 0.17341532 0.93230551 8.01506002
## [331] 2.22132303 1.76855577 0.32680574 1.55297357 2.37164795 4.67099439
## [337] 5.47919441 1.96202854 1.50609117 4.53360140 0.39519184 1.11236453
## [343] 0.28010174 1.32530773 0.22717598 1.53055254 4.93143765 2.15666910
## [349] 3.71864708 3.62219243 0.98808060 2.36746244 0.31068685 1.27407609
## [355] 3.05935082 2.07381453 6.98316264 4.16332313 1.21919812 1.74032200
## [361] 5.31322981 0.71298834 2.45722527 2.56677598 3.39361980 1.68719617
## [367] 3.64543733 1.60842628 1.26145291 7.99994961 0.51836418 0.42602713
## [373] 2.60605595 3.66170012 4.60457431 5.38959526 0.32303946 7.39590159
## [379] 4.63380827 2.86797763 6.73257721 5.02217780 4.54290862 1.72781440
## [385] 2.55165773 0.51903319 7.20794861 4.27637990 6.97893764 5.27277277
## [391] 0.08424638 6.15941751 2.84354613 1.71553448 4.96513986 2.40313404
## [397] 4.65908322 0.91652584 2.35900077 2.18638557 2.38334553 1.47638425
## [403] 4.14813242 2.91230746 1.97241802 0.44100204 3.02057861 2.86459387
## [409] 4.68297548 1.14348297 0.53490983 3.88500157 2.62846223 3.26888076
## [415] 5.05340695 1.71472359 0.46968665 0.58776463 2.73643733 4.06736212
## [421] 3.20706821 1.40642997 2.02054134 3.36314973 1.85166814 2.93743693
## [427] 1.03219509 0.70642856 7.83573200 2.95669980 3.38391454 5.83193738
## [433] 3.10288614 1.05020945 2.89051613 2.35347553 4.76565805 2.84822843
## [439] 2.39680408 3.25089192 3.40390938 4.74646909 1.45606493 1.41568068
## [445] 0.59590329 9.34740023 0.49047683 5.19266202 0.64214743 0.38472440
## [451] 7.80086877 0.82916509 0.76496724 2.59823113 5.60805953 0.89121768
## [457] 4.84986579 0.14638504 6.15821231 0.51456229 3.07447494 2.09832538
## [463] 2.48757543 3.03690522 11.95474282 5.80105280 2.90603751 2.51528917
## [469] 0.53151149 0.82663654 3.24340917 7.98116945 1.17568590 3.16492180
## [475] 3.20115112 0.38249294 7.52764504 2.46744016 2.66668296 8.71035257
## [481] 1.10757543 1.89540619 2.85836319 1.95992748 0.34443267 2.26796444
## [487] 4.72284239 3.64465760 0.62915959 9.69228171 8.18372736 3.82590431
## [493] 0.58277129 14.54554407 1.52259235 5.60318348 6.75510659 3.74061509
## [499] 0.38593359 0.84105710
# combine plots
par(mfrow=c(2,2))
plot(y_dchisq, main="Chi Square Density plot")
plot(y_pchisq, main="Chi Square Cumulative Density plot")
plot(y_qchisq, main="Chi Square Quantile plot")
plot(y_rchisq, main="chisq Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rchisq),main = "Density plot")
hist(y_rchisq, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rchisq2 <- rchisq(N, df = 1) # Modify shape parameter
y_rchisq3 <- rchisq(N, df = 2) # Modify shape parameter
plot(density(y_rchisq), # Plot default density
xlim = c(- 0, 10),
ylim = c(- 0, 1.1),
main = "Chi Square Distribution in R with different degrees of freedom",lwd=
3)
lines(density(y_rchisq2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rchisq3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("df = 3",
"df = 1",
"df = 2"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 6. Students t Probability Distributions
# Four functions:
# dt for density function of the distribution
# pt,for cumulative density of the distribution
# qt for the quantile values from 0 to 100%
# rt for random samples generation
# Specify x-values for dnorm function
x_dt <- seq(-4, 4, by = 0.01)
y_dt <- dt(x_dt, df = 1) # Apply dgamma function
y_pt <- pt(x_dt, df = 1) # Apply pgamma function
x_dt <- seq(0, 1, by = 0.01)
y_qt <- qt(x_dt, df = 1) # Apply qexp function
set.seed(91929) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rt <- rt(N, df = 1) # Draw N exp distributed values
y_rt # Print values to RStudio console
## [1] -0.206680813 0.687827774 0.097775011 -0.320853310 0.732752143
## [6] 0.239506538 0.829111987 -0.968523712 2.943826284 1.286777777
## [11] -2.267263824 1.354351052 4.312681123 1.080469914 0.638726100
## [16] -1.419955543 -0.243128132 0.053082008 1.406649321 21.815783201
## [21] -1.626295490 -4.601582677 0.834281511 -11.004810264 2.934937810
## [26] -0.336454957 -0.831258982 3.958851185 1.662298533 -0.882703587
## [31] -0.810382620 0.681780649 -2.446399374 0.758066790 -0.213445168
## [36] -0.743516359 -3.783991229 0.412218027 0.342464072 -0.484595660
## [41] 0.263298603 -1.136493441 -0.419313003 -0.027935552 2.297732156
## [46] -0.831028673 -1.506974454 1.845560699 -2.443561862 0.302679257
## [51] 1.651117050 1.356164125 -2.405459421 -2.968223150 0.599379697
## [56] 4.662295938 -0.065611122 -57.858327769 0.517822423 0.958256365
## [61] 5.081258054 0.866385715 -0.856753660 -0.515367493 -2.781923327
## [66] 0.745971073 -0.711160846 0.305279358 0.991753204 -1.877920271
## [71] -1.164522911 -1.411948308 -1.173261105 22.539142063 0.092592839
## [76] -0.376208135 -1.116272186 0.470584966 0.195007956 0.040906665
## [81] 2.142722847 -0.543800768 -0.307655882 0.452904062 0.032532871
## [86] 10.045834946 -0.860729053 0.256368832 0.871727596 1.661314283
## [91] -1.364645917 -2.048647859 -0.503850398 1.372356305 0.118479068
## [96] -1.043361194 0.059507279 -3.133569916 -2.560703884 0.275265477
## [101] -17.779647582 -0.037759774 -2.452026380 -0.157049897 1.514400946
## [106] 0.992970158 0.001793779 -0.911563907 0.769091996 -5.187242304
## [111] 0.969133332 -3.188563317 -6.411608287 0.956685119 0.253864046
## [116] 3.104547101 -0.340563096 1.241340681 0.976833115 -0.163751529
## [121] -69.212656517 0.798819936 -0.569308621 2.122199979 0.276846670
## [126] -3.191976010 6.981356152 2.342004740 0.569817964 -0.300670837
## [131] -0.872034108 -0.261438268 -0.514416741 -35.153470607 0.355096217
## [136] -1.279621823 0.861112020 1.531298364 -15.570277094 4.163489529
## [141] 0.289727931 -6.183919796 -0.008553019 0.704877833 -2.255252963
## [146] 1.143218823 -0.159396613 -0.750181313 -0.049703891 -1.841100844
## [151] -0.659551461 -0.061989428 -5.940545081 7.130882237 -0.792777384
## [156] 0.828461014 -1.037788914 2.541578804 -1.772900612 -0.424425728
## [161] 0.365794488 0.810918845 5.133561129 -4.377453918 -2.542577584
## [166] 0.932951022 0.003141469 -14.466151581 -3.236182668 -0.023161614
## [171] 1.824483762 -1.130040037 8.455279721 -2.057023129 -0.654306517
## [176] -89.802125908 0.025733970 -0.143730802 0.188770469 3.219218085
## [181] -1.133995562 -18.010875564 -1.234131948 -5.831395356 -1.163073196
## [186] 0.054316248 -0.645063743 0.136374130 1.434427137 -0.797505939
## [191] -0.898280717 -0.303451497 3.053033570 -0.841088721 0.158698074
## [196] -0.639101926 -0.336934985 2.134087611 -0.124843443 0.322523503
## [201] -1.077315354 0.540014222 -1.099033931 -0.944349095 0.864301460
## [206] -2.573287892 1.253949450 -1.874308678 1.020442939 -0.809357520
## [211] 0.147526588 2.225650676 17.298929966 -1.034743547 -1.207669329
## [216] -0.070569855 -1.033665770 -3.331253679 0.327824305 4.646954918
## [221] 0.466907147 0.257442142 -9.450080050 -7.308687659 -0.070353159
## [226] -0.762487117 0.019082420 0.266731898 -37.050448609 -4.388492330
## [231] -0.225683481 0.433180501 -1.389130481 0.056516928 -0.998932951
## [236] 1.639358332 -0.185068634 -1.923429067 0.783143766 1.488517093
## [241] -0.265340417 0.855835109 -0.704330760 -0.332917204 -2.644690384
## [246] -0.767778952 -0.634889591 2.209728366 -0.517589213 -0.560293415
## [251] 0.270160407 -1.018048931 3.611526569 -0.249010015 0.023089390
## [256] 0.363967699 2.794213653 0.338758245 2.525812345 -0.209189141
## [261] -2.620461248 0.352409996 -1.571301110 -2.669684838 -0.313992662
## [266] 0.833009866 -1.009821277 -4.639706497 -4.673463929 0.263971348
## [271] 0.561915529 0.572049811 0.585682412 4.493790848 1.517192256
## [276] -0.107479021 0.744448215 -2.201648909 -2.738124294 0.911005998
## [281] 64.986470660 -0.582820713 -1.300450490 -0.853042511 13.487568471
## [286] 1.101078664 15.017409025 -0.781973640 1.697885045 -2.031655557
## [291] -0.639941338 11.213121968 0.748636642 0.392709755 43.156220804
## [296] -0.796788740 0.263809507 -2.385826898 -3.865203484 -2.193934971
## [301] -0.893555618 -1.373922729 0.270707205 -0.167941033 -0.763187127
## [306] 1.401406555 -0.661468077 0.194387326 -0.707461479 0.879387341
## [311] -0.277529822 -0.774965318 -0.677816557 1.080948709 -2.053349443
## [316] 0.125442589 1.233581061 -0.042285071 -0.947610248 3.171778911
## [321] -0.273497251 -0.948441068 -0.232044636 -0.251481896 2.797206862
## [326] 25.604272309 -1.612933594 -8.313805663 0.782409742 0.503845835
## [331] 1.717264262 -0.211119465 -0.450656488 12.096533809 1.141202622
## [336] 0.479245855 8.187343726 -23.480757861 6.311975506 -0.573019748
## [341] 1.199550609 -0.273482269 1.227576810 -0.390158805 -2.816412147
## [346] -0.994995877 -9.023201355 -0.706133178 -0.032878847 -0.189608409
## [351] -6.530603310 0.447348177 0.163341145 1.029268620 -1.267392222
## [356] -0.005770871 -0.142437235 -10.946535408 -2.225425628 1.628219049
## [361] 1.227526223 -0.203279674 -1.156827252 -0.067503263 0.998504927
## [366] 0.046978003 0.238632071 1.715813142 -1.173942188 0.432433159
## [371] -1.523187763 0.457026892 3.846948669 -1.645788299 3.603034105
## [376] -4.539786623 -0.143735278 -0.870343937 -0.038701559 -0.086102021
## [381] 0.358450297 1.244308046 0.127274384 1.360470272 -1.265838454
## [386] 0.430305657 0.496592038 -3.244543945 -5.480050034 4.184942539
## [391] 15.065429777 0.235285626 0.855682649 -0.527824646 0.072696463
## [396] 0.029171955 -0.548077317 0.638290935 -2.169507974 -0.190727397
## [401] 0.800164261 -12.792040079 -0.228393666 -2.067483607 1.173279448
## [406] 0.305630175 -0.443008229 0.265011804 0.900396731 0.569747467
## [411] 5.455293066 0.237078198 -3.202576645 1.375390521 -6.138395517
## [416] 1.809315614 6.095788560 0.335552067 0.216036955 0.093069903
## [421] -1.256168007 0.506617199 -0.643834955 -1.829273486 -0.371929968
## [426] 0.521663592 -0.250761985 -0.715996337 1.797463823 1.149033305
## [431] -1.192012209 -0.632049618 0.196927733 0.368302959 -0.107413518
## [436] 0.707572596 5.544136495 -0.067094422 1.925835267 -1.361592172
## [441] -0.883908826 1.460600287 0.304226405 -0.214359444 -0.711777526
## [446] -1.081616451 0.751022597 30.704746025 34.692301254 2.269407396
## [451] -0.450654504 0.573028909 -0.018551563 1.884073099 -0.218305514
## [456] -0.232918146 0.212579426 3.758713754 -2.187723937 -1.118910572
## [461] -0.623292070 -0.139470191 -0.709325966 1.828023144 -0.227666349
## [466] 0.084369030 -0.245359717 2.026907211 -0.269000414 -0.496034855
## [471] 1.727497655 1.280743082 0.153837527 0.618625551 -2.142471555
## [476] 0.186357444 1.219821528 -2.290075193 -0.736552099 10.473227893
## [481] 4.059547630 1.088285292 2.141971154 -6.087338082 3.610167580
## [486] 2.564431495 -0.830389061 1.506793629 -7.888067967 0.217169451
## [491] 0.181352285 -1.324075549 -4.797856772 -3.421591680 0.431135030
## [496] 0.363778505 -1.318554561 0.676659199 0.773356186 -1.159720300
# combine plots
par(mfrow=c(2,2))
plot(y_dt, main="Students t Density plot")
plot(y_pt, main="Students t Cumulative Density plot")
plot(y_qt, main="Students t Quantile plot")
plot(y_rt, main="Students t Scatter Plot Random Samples")
par(mfrow=c(1,1))
plot(density(y_rt),main = "Density plot",xlim=c(-4,10))
hist(y_rt, main= "Histogram",breaks = 1000,xlim=c(-3,3))
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rt2 <- rchisq(N, df = 2) # Modify shape parameter
y_rt3 <- rchisq(N, df = 3) # Modify shape parameter
plot(density(y_rt), # Plot default density
xlim = c(-4, 10),
main = "Students t Distribution in R with different degrees of freedom",lwd=
3)
lines(density(y_rt2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rt3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("df = 1",
"df = 2",
"df = 3"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 7. F Probability Distributions
# Four functions:
# df for density function of the distribution
# pf,for cumulative density of the distribution
# qf for the quantile values from 0 to 100%
# rf for random samples generation
# Specify x-values for dbeta function
x_df <- seq(0, 20, by = 0.01)
y_df <- df(x_df, df1 = 3, df2 =5) # Apply df function
y_pf <- pf(x_df, df1 = 30, df2 =5) # Apply pf function
x_df <- seq(0, 1, by = 0.01)
y_qf <- qf(x_df, df1 = 3, df2 =5) # Apply qf function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rf <- rf(N, df1 = 3, df2 = 5) # Draw N exp distributed values
# combine plots
par(mfrow=c(2,2))
plot(y_df, main="Gamma Density plot")
plot(y_pf, main="Gamma Cumulative Density plot")
plot(y_qf, main="Gamma Quantile plot")
plot(y_rf, main="Gamma Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rf),main = "Density plot")
hist(y_rf, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rf2 <- rf(N, df1 = 10, df2 = 6) # Modify shape parameter
y_rf3 <- rf(N, df1 = 20, df2 = 7) # Modify shape parameter
plot(density(y_rf), # Plot default density
xlim = c(- 0, 5),
ylim = c(- 0, 1),
main = "F Distribution in R with degrees of freedom",lwd=3)
lines(density(y_rf2), col = "coral2",lwd=3) # Plot density with higher
mean
## function (x, ...)
## UseMethod("mean")
## <bytecode: 0x000001c701a7f8a0>
## <environment: namespace:base>
lines(density(y_rf3), col = "green3",lwd=3) # Plot density with higher
sd
## function (x, na.rm = FALSE)
## sqrt(var(if (is.vector(x) || is.factor(x)) x else as.double(x),
## na.rm = na.rm))
## <bytecode: 0x000001c701655d58>
## <environment: namespace:stats>
legend("topright", # Add legend to density
legend = c("Shape1 = 2, shape2=5",
"Shape1 = 10, shape2=6",
"Shape1 = 20, shape2=7"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 8. Uniform ContinuousProbability Distributions
# Four functions:
# dunif for density function of the distribution
# punif,for cumulative density of the distribution
# qunif for the quantile values from 0 to 100%
# runif for random samples generation
# Specify x-values for dunif
x_dunif <- seq(0, 100, by = 1)
y_dunif <- dunif(x_dunif, min = 10, max = 50) # Apply dunif function
y_punif <- punif(x_dunif, min = 10, max = 50) # Apply punif function
x_qunif <- seq(0, 1, by = 0.01) # Specify x-values for qunif function
y_qunif <- qunif(x_qunif, min = 10, max = 50) # Apply qunif function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_runif <- runif(N, min = 10, max = 50) # Draw N uniformly distributed values
# combine plots
par(mfrow=c(2,2))
plot(y_dunif, main="Uniform Continuous Density plot")
plot(y_punif, main="Uniform Continuous Cumulative Density plot")
plot(y_qunif, main="Uniform Continuous Quantile plot")
plot(y_runif, main="Uniform Continuous Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_runif),main = "Density plot")
hist(y_runif, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_runif2 <- runif(N, min = 10, max = 60) # Modify shape parameter
y_runif3 <- runif(N, min = 10, max = 70) # Modify shape parameter
plot(density(y_runif), # Plot default density
xlim = c(- 0, 100),
ylim = c(- 0, .06),
main = "F Distribution in R with degrees of freedom",lwd=3)
lines(density(y_runif2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_runif3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("Min=10, Max=50",
"Min=10, Max=60",
"Min=10, Max=70"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
## DISCRETE DISTRIBUTIONS
# 1. Binomial Discrete Probability Distributions
# Four functions:
# dbinom for density function of the distribution
# pbinom for cumulative density of the distribution
# qbinom for the quantile values from 0 to 100%
# rbinom for random samples generation
# Specify x-values for dbeta function
x_dbinom <- seq(0, 100, by = 1)
y_dbinom <- dbinom(x_dbinom, size = 100, prob = 0.5) # Apply dbinom function
y_pbinom <- pbinom(x_dbinom, size = 100, prob = 0.5) # Apply pbinom function
x_qbinom <- seq(0, 1, by = 0.01) # Specify x-values for qbinom function
y_qbinom <- qbinom(x_qbinom, size = 100, prob = 0.5) # Apply qbinom function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rbinom <- rbinom(N, size = 100, prob = 0.5) # Draw N binomially distributed valu
es
y_rbinom
## [1] 45 44 55 43 35 47 56 52 49 51 47 50 51 54 53 48 57 55 51 52 48 44 46 49 5
0
## [26] 59 55 46 54 51 61 45 49 45 49 50 40 52 44 60 45 50 55 51 43 49 58 43 44 5
1
## [51] 56 42 53 50 53 47 44 49 42 56 60 51 53 49 57 51 51 51 51 54 53 56 45 48 4
9
## [76] 40 49 50 51 50 50 55 41 52 55 47 47 52 49 51 47 49 46 53 51 56 50 47 49 5
1
## [101] 41 49 50 52 56 57 48 54 51 41 46 54 54 43 52 56 49 56 47 52 48 51 50 53 4
0
## [126] 43 48 44 49 46 46 57 42 49 47 53 49 54 37 54 52 49 50 42 47 48 55 61 54 5
3
## [151] 33 48 52 58 46 53 48 54 48 56 50 43 48 46 50 57 38 37 50 49 50 48 56 52 5
1
## [176] 50 52 58 53 49 52 44 45 49 45 51 56 52 41 50 51 52 53 46 51 43 49 55 46 4
4
## [201] 51 37 56 48 55 50 54 44 59 48 47 44 53 54 41 51 51 46 48 51 47 43 58 51 5
1
## [226] 47 49 43 55 53 50 49 56 56 54 49 55 48 53 63 49 53 50 48 48 59 41 40 49 4
8
## [251] 44 42 64 53 45 50 50 46 52 52 52 58 52 52 49 40 57 59 52 50 52 44 50 52 4
8
## [276] 41 58 43 53 48 59 50 57 48 45 45 56 42 47 50 52 51 44 55 52 47 47 50 42 4
8
## [301] 49 56 55 55 46 60 47 43 44 52 56 43 54 43 54 47 49 51 40 61 37 41 47 48 4
6
## [326] 52 49 50 54 43 49 56 50 55 52 53 54 49 49 38 48 51 50 60 60 46 49 56 53 4
4
## [351] 42 43 45 43 46 56 48 41 51 55 52 51 50 60 46 60 53 52 51 56 39 49 53 60 4
4
## [376] 46 43 42 51 56 61 49 48 55 44 53 50 53 58 47 42 44 51 55 49 57 53 45 48 3
5
## [401] 52 57 57 49 51 53 38 47 52 52 59 57 54 47 49 59 58 47 54 55 55 49 57 47 5
4
## [426] 52 51 45 52 43 44 50 61 53 55 45 47 44 41 41 47 53 49 48 44 50 60 57 53 4
9
## [451] 46 45 47 50 46 47 56 50 53 53 48 57 55 49 49 50 46 54 45 52 54 45 44 51 5
2
## [476] 50 54 51 51 42 47 48 52 49 47 58 42 58 51 58 38 50 44 56 47 50 50 50 49 4
5
# combine plots
par(mfrow=c(2,2))
plot(y_dbinom, main="Binomial Density plot")
plot(y_pf, main="Gamma Cumulative Density plot")
plot(y_qf, main="Gamma Quantile plot")
plot(y_rf, main="Gamma Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rbinom),main = "Density plot")
hist(y_rbinom, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rbinom2 <- rbinom(N,size = 100, prob = 0.7) # Modify prob parameter
y_rbinom3 <- rbinom(N,size = 100, prob = 0.3) # Modify prob parameter
plot(density(y_rbinom), # Plot default density
xlim= c(1,100),
main = "Binomial Distribution in R with degrees of freedom",lwd=3)
lines(density(y_rbinom2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rbinom3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("P Value = 0.5",
"P value = 0.7",
"P Value = 0.3"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 2. Poisson Discrete Probability Distributions
# Four functions:
# dpois for density function of the distribution
# ppois for cumulative density of the distribution
# qpois for the quantile values from 0 to 100%
# rpois for random samples generation
# Specify x-values for dbeta function
x_dpois <- seq(-5, 30, by = 1)
y_dpois <- dpois(x_dpois, lambda = 10) # Apply dpois function)
y_ppois <- ppois(x_dpois, lambda = 10) # Apply ppois function
x_qpois <- seq(0, 1, by = 0.005)
y_qpois <- qpois(x_qpois, lambda = 10) # Apply qpois function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rpois <- rpois(N, lambda = 10) # Draw N binomially distributed values
y_rpois
## [1] 6 14 8 16 6 12 10 6 7 11 7 12 10 16 7 7 7 19 13 15 10 9 12 10 1
0
## [26] 14 11 10 8 11 15 9 14 11 10 8 8 10 12 15 8 4 17 12 11 10 2 10 9 1
1
## [51] 9 17 10 9 11 8 8 11 10 10 5 13 12 6 11 9 14 9 9 11 9 9 6 10 1
1
## [76] 15 9 14 10 8 5 13 12 14 9 11 14 12 9 12 8 11 11 5 12 13 7 7 11
4
## [101] 14 15 8 10 11 7 6 9 12 11 13 10 8 6 9 10 3 4 13 11 12 13 5 13 1
1
## [126] 13 6 9 12 8 13 17 11 15 10 7 4 8 13 11 10 8 14 11 11 5 12 13 10 1
0
## [151] 11 9 11 11 7 16 10 7 10 9 9 14 10 6 8 13 5 12 13 8 11 11 4 4
8
## [176] 9 15 8 11 9 13 12 6 4 9 7 8 8 9 8 8 5 4 4 10 7 5 9 13
7
## [201] 10 13 8 3 12 9 7 10 11 15 16 12 14 6 5 10 4 7 11 8 11 13 11 9 1
0
## [226] 2 7 17 16 5 17 18 10 10 12 11 14 12 13 8 9 15 10 12 6 14 10 12 9
9
## [251] 15 5 8 11 7 12 8 6 14 6 14 10 18 14 15 9 9 14 10 11 5 19 9 9
8
## [276] 8 16 12 11 11 9 12 13 12 12 12 9 10 12 11 11 9 8 5 10 13 9 14 8
9
## [301] 5 11 15 13 15 9 10 10 13 10 4 10 13 11 9 7 11 12 10 6 11 8 9 10 1
8
## [326] 4 13 9 13 10 9 5 10 13 13 8 10 6 20 12 10 10 7 12 11 9 6 8 13 1
2
## [351] 9 12 11 10 16 7 8 9 12 6 10 5 16 9 9 9 5 11 7 10 9 10 7 7
8
## [376] 13 15 13 10 13 17 7 15 8 8 12 8 9 6 11 10 10 7 10 13 15 12 8 14 1
1
## [401] 13 6 7 7 10 14 10 9 13 9 8 13 9 7 3 11 14 12 14 9 11 6 12 13
6
## [426] 4 6 12 10 11 11 6 11 14 7 10 7 11 8 14 11 15 6 14 0 12 6 7 8
5
## [451] 14 18 7 4 7 11 9 12 9 12 10 10 4 5 7 9 13 6 8 9 12 9 8 8 1
6
## [476] 15 11 13 10 6 8 5 10 15 12 8 3 8 7 10 12 15 13 10 12 9 9 12 16
6
# combine plots
par(mfrow=c(2,2))
plot(y_dpois, main="Poisson Density plot")
plot(y_ppois, main="Poisson Cumulative Density plot")
plot(y_qpois, main="Poisson Quantile plot")
plot(y_rpois, main="Poisson Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rpois),main = "Density plot")
hist(y_rpois, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rpois2 <- rpois(N, lambda = 15) # Modify prob parameter
y_rpois3 <- rpois(N, lambda = 20) # Modify prob parameter
plot(density(y_rpois), # Plot default density
xlim= c(0,60),
main = "Poisson Distribution in R with lambda parameters",lwd=3)
lines(density(y_rpois2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rpois3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("lambda = 10",
"lambda = 15",
";lambda = 20"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 3. Geometric Discrete Probability Distributions
# Four functions:
# dgeom for density function of the distribution
# pgeom for cumulative density of the distribution
# qgeom for the quantile values from 0 to 100%
# rgeom for random samples generation
# Specify x-values for dbeta function
x_dgeom <- seq(0, 20, by = 1)
y_dgeom <- dgeom(x_dgeom, prob = 0.5) # Apply dgeom function)
y_pgeom <- pgeom(x_dgeom, prob = 0.5) # Apply pgeom function
x_qgeom <- seq(0, 1, by = 0.01) # Specify x-values for qgeom function
y_qgeom <- qgeom(x_qgeom, prob = 0.5) # Apply qgeom function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rgeom <- rgeom(N, prob = 0.5) # Draw N binomially distributed values
y_rgeom
## [1] 4 1 5 0 1 0 2 0 1 0 3 1 3 2 1 0 0 2 4 0 1 1 1 0
0
## [26] 1 2 1 0 0 0 0 0 0 0 2 3 2 1 0 1 2 0 0 1 0 0 1 0
1
## [51] 4 0 0 0 1 4 5 0 0 1 1 1 1 2 0 1 0 1 1 0 4 0 0 0
0
## [76] 1 1 0 1 0 5 1 0 1 3 0 0 0 0 0 2 0 0 1 0 2 0 0 0
6
## [101] 1 0 0 1 1 2 4 0 0 1 2 3 0 3 0 1 0 1 1 1 0 2 1 0
0
## [126] 1 1 1 1 1 7 0 1 0 0 2 0 1 2 1 1 1 2 1 0 1 1 1 0
0
## [151] 0 1 0 0 2 0 0 0 1 0 0 0 0 1 1 3 1 0 1 1 0 1 4 2
0
## [176] 3 1 2 0 6 0 0 0 0 0 0 2 2 4 0 0 3 1 0 1 2 1 0 0
0
## [201] 0 0 2 2 1 2 4 0 1 0 2 0 0 1 2 0 0 2 0 0 0 1 1 1
0
## [226] 2 1 0 0 1 0 0 1 0 0 1 1 3 0 0 7 1 5 0 0 0 2 1 3
1
## [251] 1 1 0 0 1 0 1 0 0 1 2 0 1 0 0 2 1 2 2 1 0 1 1 2
1
## [276] 2 1 1 0 1 0 1 0 1 2 0 2 1 2 4 2 0 0 2 1 0 2 1 2
0
## [301] 1 3 0 3 0 0 1 1 1 3 3 0 2 0 1 1 1 1 1 0 5 0 0 0
0
## [326] 2 3 2 1 2 0 0 1 0 3 0 1 1 0 2 1 2 0 2 0 0 5 1 3
3
## [351] 0 1 1 0 1 0 2 0 1 3 10 0 2 1 1 1 0 0 0 0 0 1 0 1
0
## [376] 3 1 2 0 0 8 0 3 0 6 0 3 0 3 1 0 0 2 0 0 0 0 1 0
0
## [401] 1 0 1 0 7 0 0 1 2 2 0 0 1 0 0 2 0 0 0 0 1 0 2 0
1
## [426] 2 4 1 1 0 2 1 1 2 1 0 0 0 3 2 1 0 2 1 0 0 1 1 1
3
## [451] 1 2 2 0 2 0 0 2 2 1 0 4 2 0 0 0 0 3 0 0 0 1 4 3
5
## [476] 1 3 0 1 0 1 2 2 4 2 1 0 1 0 2 1 1 3 1 1 1 1 0 4
0
# combine plots
par(mfrow=c(2,2))
plot(y_dgeom, main="Geometric Density plot")
plot(y_pgeom, main="Geometric Cumulative Density plot")
plot(y_qgeom, main="Geometric Quantile plot")
plot(y_rgeom, main="Geometric Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rgeom),main = "Density plot")
hist(y_rgeom, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rgeom2 <- rgeom(N, prob = 0.7) # Modify prob parameter
y_rgeom3 <- rgeom(N, prob = 0.3) # Modify prob parameter
plot(density(y_rpois), # Plot default density
xlim= c(0,50),
main = "Geomtric Distribution in R with Probababilities",lwd=3)
lines(density(y_rgeom2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rgeom3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("P value = 0.5",
"P value = 0.7",
"P value = 0.3"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))
# 4. Binomial Discrete Probability Distributions
# Four functions:
# dnbinom for density function of the distribution
# pnbinom for cumulative density of the distribution
# qnbinom for the quantile values from 0 to 100%
# rnbinom for random samples generation
# Specify x-values for dbeta function
x_dnbinom <- seq(0, 100, by = 1)
y_dnbinom <- dnbinom(x_dnbinom, size = 100, prob = 0.5) # Apply dnbinom function
y_pnbinom <- pnbinom(x_dnbinom, size = 100, prob = 0.5) # Apply pnbinom function
x_dnbinom <- seq(0, 1, by = 0.01)
y_qnbinom <- qnbinom(x_dnbinom, size = 100, prob = 0.5) # Apply qnbinom function
set.seed(13579) # Set seed for reproducibility
N <- 500 # Specify sample size
y_rnbinom <- rnbinom(N, size = 100, prob = 0.5) # Draw N binomially distributed va
lues
y_rnbinom
## [1] 99 105 134 91 95 100 121 83 81 102 92 100 109 89 107 124 121 105
## [19] 78 77 93 106 118 75 131 106 78 98 99 124 100 99 96 87 118 91
## [37] 100 106 77 99 100 86 97 120 122 99 93 103 83 118 83 86 133 106
## [55] 118 90 115 96 105 108 109 111 95 78 98 100 103 105 129 78 93 78
## [73] 91 94 82 120 101 104 110 110 92 99 101 98 103 108 105 72 78 88
## [91] 111 74 79 110 92 105 96 93 84 100 109 96 106 94 120 130 83 77
## [109] 82 97 88 75 102 112 83 98 82 104 111 119 102 97 94 113 107 98
## [127] 99 79 99 105 94 61 127 90 85 105 105 107 112 96 93 114 101 117
## [145] 115 106 105 107 98 103 113 105 111 126 115 103 112 83 115 101 112 89
## [163] 99 95 76 109 112 93 115 83 107 115 140 77 92 85 85 92 112 122
## [181] 87 84 87 122 89 112 105 111 72 101 97 87 98 113 105 80 84 129
## [199] 96 106 78 106 100 83 113 100 107 110 113 121 113 91 120 72 97 106
## [217] 90 119 94 95 88 103 82 94 86 92 80 83 95 96 94 107 83 77
## [235] 109 101 84 112 114 95 83 104 127 93 103 104 103 104 114 79 109 123
## [253] 74 100 105 128 83 127 126 91 93 112 128 131 98 101 113 103 102 98
## [271] 112 76 83 106 105 90 114 85 110 117 87 93 113 96 76 78 76 109
## [289] 73 129 105 98 112 105 81 87 94 116 93 71 100 108 101 80 72 105
## [307] 94 106 95 86 94 102 89 108 76 97 115 110 71 104 76 124 81 117
## [325] 86 91 101 100 113 85 92 106 79 102 89 84 132 112 93 102 93 119
## [343] 95 89 99 82 96 75 93 97 114 132 100 95 117 117 94 126 115 108
## [361] 91 95 100 91 85 94 95 114 99 99 95 106 98 114 100 133 135 103
## [379] 92 125 117 102 92 96 83 90 96 103 89 92 104 95 90 102 64 105
## [397] 99 95 88 120 102 110 85 107 99 98 109 99 83 96 85 90 123 121
## [415] 78 117 80 90 114 120 87 106 113 68 83 86 106 98 92 75 93 96
## [433] 103 115 106 95 123 78 105 108 92 100 103 108 97 110 84 91 91 85
## [451] 97 119 96 91 88 92 88 105 122 109 82 100 100 74 98 96 90 88
## [469] 100 118 113 93 105 90 101 101 84 93 97 105 100 100 110 104 94 109
## [487] 99 82 125 124 86 130 114 114 90 100 94 113 96 97
# combine plots
par(mfrow=c(2,2))
plot(y_dnbinom, main="Negative Binomial Density plot")
plot(y_pnbinom, main="Negative Binomial Cumulative Density plot")
plot(y_qnbinom, main="Negative Binomial Quantile plot")
plot(y_rnbinom, main="Negative Binomial Scatter Plot Random Samples")
par(mfrow=c(1,2))
par(mfrow=c(1,2))
plot(density(y_rnbinom),main = "Density plot")
hist(y_rnbinom, main= "Histogram")
par(mfrow=c(1,1))
## Modify values of the shape parameter
y_rnbinom2 <- rnbinom(N,size = 100, prob = 0.7) # Modify prob parameter
y_rnbinom3 <- rnbinom(N,size = 100, prob = 0.3) # Modify prob parameter
plot(density(y_rnbinom), # Plot default density
xlim= c(0,300),
ylim= c(0,0.07),
main = "Binomial Distribution in R with degrees of freedom",lwd=3)
lines(density(y_rnbinom2), col = "coral2",lwd=3) # Plot density with higher mean
lines(density(y_rnbinom3), col = "green3",lwd=3) # Plot density with higher sd
legend("topright", # Add legend to density
legend = c("P Value = 0.5",
"P value = 0.7",
"P Value = 0.3"),
col = c("black", "coral2", "green3"),lty = 2, lwd=3)
par(mfrow=c(1,1))