From 6bb0e534e8a2619e2a339598fd035868c3390a38 Mon Sep 17 00:00:00 2001 From: St33v Date: Thu, 16 May 2013 19:28:14 +1100 Subject: Create probability Added functions from sp_fn.R --- probability | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 probability (limited to 'probability') diff --git a/probability b/probability new file mode 100644 index 0000000..3aa001d --- /dev/null +++ b/probability @@ -0,0 +1,38 @@ +####################################################################### +# Get a result based on a probability (default is 0.5) +# a typical usage is: 'if(pr(x)) then ... +# The default case is equivalent to tossing a coin +# You can pass in a vector of probabilties to get a vector of T, F results +# +pr <- function(x = 0.5){ + if (!length(x)) {return()} + if (x<0 || x>1) stop(" prob out of range\n") + return(ifelse(rbinom(length(x), 1, x), TRUE, FALSE)) +} + + +####################################################################### +# multinomial randomiser; by S.B. extended by S.P. +# n - length of result vector +# p - vector of probabilities; has to sum to <= 1 +# val - vector of return values, with length == p or: +# if longer than p, should be 1 element longer (LAST element is returned) +rMulti <- function(n=1, p, val) { + res <- 1:n + for(i in res) { + element <- ((1:length(p))[runif(1)