Good Mourning, I have a function to generate a matrix as I show part of it; g[j,i]<-if (gen[j,i]==0) al1[i,1]+al1[i,1] else ... However i would like that this function occurred with a probability P and that another function (another formula to generate g matrix) with probability P-1 That?s it, if P is .7, i would like that in 70% of the times (for random i and j) the matrix g was generated according to the formula above and in 30% of the times with a different formula which i did not write could anyone help me? Thank you very much M?rcio -- View this message in context: http://www.nabble.com/How-to-separate-a-function-by-2-probabilities-tp25491943p25491943.html Sent from the R help mailing list archive at Nabble.com.
Marcio Define two functions, e.g. f1<-function(i,j) i+j f2<-function(i,j) i-j then call them based on the probability e.g. 0.7 f <- if(runif(1)>0.7) f1 else f2 f(1,1) or more compact (if(runif(1)>0.7) f1 else f2)(1,1) HTH Schalk Heunis On Thu, Sep 17, 2009 at 5:10 PM, Marcio Resende <mresendeufv@yahoo.com.br>wrote:> > Good Mourning, > I have a function to generate a matrix as I show part of it; > > g[j,i]<-if (gen[j,i]==0) al1[i,1]+al1[i,1] else ... > > However i would like that this function occurred with a probability P and > that another function (another formula to generate g matrix) with > probability P-1 > > That´s it, if P is .7, i would like that in 70% of the times (for random i > and j) the matrix g was generated according to the formula above and in 30% > of the times with a different formula which i did not write > > could anyone help me? > Thank you very much > Márcio > > -- > View this message in context: > http://www.nabble.com/How-to-separate-a-function-by-2-probabilities-tp25491943p25491943.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Assuming storage is not a problem, first generate two matrices, one by each method, call these A and B. Then if dim(A) = dim(B) = c(m,n) and k = m*n z <- rbinom(k,1, .7) result <- A*z + B*(1-z) Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Marcio Resende Sent: Thursday, September 17, 2009 8:11 AM To: r-help at r-project.org Subject: [R] How to separate a function by 2 probabilities Good Mourning, I have a function to generate a matrix as I show part of it; g[j,i]<-if (gen[j,i]==0) al1[i,1]+al1[i,1] else ... However i would like that this function occurred with a probability P and that another function (another formula to generate g matrix) with probability P-1 That?s it, if P is .7, i would like that in 70% of the times (for random i and j) the matrix g was generated according to the formula above and in 30% of the times with a different formula which i did not write could anyone help me? Thank you very much M?rcio -- View this message in context: http://www.nabble.com/How-to-separate-a-function-by-2-probabilities-tp254919 43p25491943.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Marcio Resende wrote:> > Good Mourning, > I have a function to generate a matrix as I show part of it; > > g[j,i]<-if (gen[j,i]==0) al1[i,1]+al1[i,1] else ... > > However i would like that this function occurred with a probability P and > that another function (another formula to generate g matrix) with > probability P-1 > > That?s it, if P is .7, i would like that in 70% of the times (for random i > and j) the matrix g was generated according to the formula above and in > 30% of the times with a different formula which i did not write > >something along the lines of if (runif(1)<0.7) {first formula} else {second formula} you can probably make this much more efficient by appropriate vectorization, but you didn't show enough of what you're doing to make specific recommendations cheers -- View this message in context: http://www.nabble.com/How-to-separate-a-function-by-2-probabilities-tp25491943p25492242.html Sent from the R help mailing list archive at Nabble.com.