Print

Print


The problem is that as soon as your overall probability (param1) is greater than 0.5, then, if you want it to remain fixed, there are values of your 2nd parameter (param2) that are not possible. For example, if param1 is 0.75, you will get six 1s in average with a most scattered distribution of 4 on the 1st vs 2 on the 2nd, that is a param2 no greater than 4/6.
It is easy to see that the maximum value that can be taken by param2 is max_param2=1/(2*param1)... the minimum being 1-max_param2.
But once you get the right value for param2 (param2 capped at max_param2 and min_param2), you can calculate the probability of a binary on the 1st being 1 and the proba of a binary on the 2nd binary being 1, to finally generate your 8 binaries. So here would be my proposed function where I start by redefining param2 when it is too large or too small :

randomBinaries<-function(param1=0.5,param2=0.5){
param2<-ifelse(param2>0.5,min(param2,1/(param1*2)),max(param2,1-1/(param1*2)))
p_elem1<-param2*(param1*2)
p_elem2<-param1*2*(1-param2)
elem1<-rbinom(4,1,p_elem1)
elem2<-rbinom(4,1,p_elem2)
binaries<-matrix(c(elem1,elem2),nrow=4,ncol=2,byrow=F)
dimnames(binaries)<-list(c("pair1","pair2","pair3","pair4"),c("1st","2nd"))
return(binaries)
}

Nicolas


Date: Tue, 15 Oct 2013 13:11:44 -0400
From: [log in to unmask]
Subject: Writing an R fn to produce random sequences of binaries XXXX
To: [log in to unmask]

I am attempting to write an R fn to produce a random sequence of 8 binaries
(4 ordered pairs). I would like to parmeterize the fn so that it takes 2
arguments: 1 that is an overall probability of a 1 vs. 0 and the other
which controls the likelihood of 1s on the 1st vs. the 2nd element of the 4
ordered pairs.

Here are some examples:
Overall 1 Probability Parm 1st Element Probability Parm Average N of 1s Average N 1st Element 1s   1 1.5 2 2.5 3 3.5 4 4.5
0.5 1 4 4 1 0 1 0 1 0 1 0
0.5 0 4 0 0 1 0 1 0 1 0 1
0.5 0.75 4 3 1 0 0 1 1 0 1 0
0.5 0.25 4 1 0 1 0 1 1 0 0 1
1 1 8 4 1 1 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0
0.5 0.5 4 2 1 0 0 1 1 0 0 1
 
 
Can anyone suggest an algorithm for this? I have a 1st draft of the fn
definition, but my algorithm is not correct as I am not getting the average
number of 1s (averaged over 10,000 sequences) correct.
Thanks!
 
Dan
You may leave the list at any time by sending the command SIGNOFF allstat
to [log in to unmask], leaving the subject line blank.
You may leave the list at any time by sending the command

SIGNOFF allstat

to [log in to unmask], leaving the subject line blank.