{"id":125,"date":"2016-06-11T07:28:47","date_gmt":"2016-06-11T07:28:47","guid":{"rendered":"http:\/\/www.uncertain-future.org.uk\/?page_id=125"},"modified":"2016-06-11T07:42:18","modified_gmt":"2016-06-11T07:42:18","slug":"generating-sets-of-independent-random-variables","status":"publish","type":"page","link":"http:\/\/www.uncertain-future.org.uk\/?page_id=125","title":{"rendered":"Generating sets of independent random variables"},"content":{"rendered":"<p><u><\/u><u>Libraries<\/u><\/p>\n<p>No special library routines are required for these routines<\/p>\n<p><u>Download<\/u><\/p>\n<p>Download sample code for routines plotting samples from different distributions\u00a0[<a href=\"http:\/\/www.uncertain-future.org.uk\/wp-content\/uploads\/2016\/06\/R-Plot-Dists.zip\"><em>Here<\/em><\/a>]<\/p>\n<p><u>Procedure<\/u><\/p>\n<p>These routines are easily used in R to generate a file of independent random parameter sets for use as an input to a model.\u00a0 One way of doing this is to use the data.frame construct to produce a file with each parameter set as a row in the data.frame.\u00a0\u00a0 Thus to generate 250 samples of 3 parameters (Szm, Ksat, Chv) from normal, lognormal and uniform distributions respectively, the following script will produce a data.frame called params<\/p>\n<p>num &lt;- 250;<\/p>\n<p>Szm &lt;- rnorm(num, 0.025,0.005);<\/p>\n<p>Ksat &lt;- rlnorm(num,1,0.5);<\/p>\n<p>Chv &lt;- runif(num, 1000,3000);<\/p>\n<p>params &lt;- data.frame(Szm,Ksat,Chv);<\/p>\n<p>write.table(params, file=\u201dfilename\u201d,quote=FALSE);<\/p>\n<p>&nbsp;<\/p>\n<p>Alternatively, the file may be written in comma separated variable format (.csv) for easy input into, for example, Excel, by using\u00a0write.csv.<\/p>\n<p>A table can be read back into R by using the functions<\/p>\n<p>params &lt;- read.table(file=\u201dfilename\u201d)<\/p>\n<p><em><u>or<\/u><\/em><\/p>\n<p>params &lt;- read.csv(file=\u201dfilename\u201d)<\/p>\n<p>Information on other parameters for the\u00a0read.table \/ write.table\u00a0or other functions can be obtained by using the R help system.<\/p>\n<p><strong>Plotting different distributions.<\/strong><\/p>\n<p>The following routines written in R allow the user to plot any number of random samples from selected distributions (see Figure 3.2 in\u00a0<strong>Environmental Modelling: An Uncertain Future?<\/strong>).\u00a0 The mean and standard deviations of samples are plotted, together with the sample PDF and CDF, are plotted.<\/p>\n<p>xuniform (num, min, max)<\/p>\n<p>xnormal (num, mean, sd)<\/p>\n<p>xlognorm(num, min, logmean, logsd)<\/p>\n<p>xexp(num, min, rate)<\/p>\n<p>xgamma(num, min, shape, rate)<\/p>\n<p>xbeta (num, min, max, shape1, shape2)<\/p>\n<p>xtriangle (num, min, peak, max)<\/p>\n<p>xtrapezoid (num, min, peak1, peak2, max)<\/p>\n<p>&nbsp;<\/p>\n<p>These can be used to explore the effects of changing sample size (num), distribution function, and distribution parameters (min, max, mean, sd,\u2026..etc).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Libraries No special library routines are required for these routines Download Download sample code for routines plotting samples from different distributions\u00a0[Here] Procedure These routines are easily used in R to generate a file of independent random parameter sets for use as an input to a model.\u00a0 One way of doing this is to use the &hellip; <a href=\"http:\/\/www.uncertain-future.org.uk\/?page_id=125\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Generating sets of independent random variables<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":28,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-125","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/125","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=125"}],"version-history":[{"count":2,"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/125\/revisions"}],"predecessor-version":[{"id":138,"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/125\/revisions\/138"}],"up":[{"embeddable":true,"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=\/wp\/v2\/pages\/28"}],"wp:attachment":[{"href":"http:\/\/www.uncertain-future.org.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=125"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}