Mills, Jason
2006-Mar-14 18:59 UTC
[R] ignoring objects containing NaN's in a recursive function
Hi- General question: I have written a function that performs a permutation test. I use the function "sample" to randomize my data, then I call on another function I wrote to analyze the randomized data. This process repeats "n" times-- 10, 100 or whatever. The function applied to the randomized data creates a matrix and sometimes the matrix contains NaN's. I would like my function to simply ignore any matrices that contain NaN's and continue to cycle until it has generated "n" matrices without any NaN's. I am aware of the function "is.nan", but I'm not sure how to fit it in. Specifics: Here's the section of code that needs improvement: for (i in 1:n){ S[,,]= sample (Y[,,]) dig=auto(S,H,J,T) out[i,]=dig[1,]} n= number of randomizations S= array to store randomized data Y= original data array dig= the matrix returned by the function "auto" out= matrix that collects the first row of each "dig" matrix up to "n" Sometimes "dig" includes NaN's. I want the function to ignore those "dig"s but keep working until it has generated "n" matrices that contain no NaN's. How could I do this? Thanks. Jason Mills
Berton Gunter
2006-Mar-14 19:31 UTC
[R] ignoring objects containing NaN's in a recursive function
if(any(is.nan(...)))... ##repeat However, 1) Have you tried RSiteSearch('permutation test',restr='function') to see what's already there? 2) I suspect that there are better ways to do this. In particular, generating all random whatevers at once is way more efficient than repeated calls; 2) It is **not** a permutation test, since there is no guarantee that all the calls to sample() generate different permutations. (depending on the sample size, this may be nitpicking). Cheers, -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mills, Jason > Sent: Tuesday, March 14, 2006 11:00 AM > To: r-help at stat.math.ethz.ch > Subject: [R] ignoring objects containing NaN's in a recursive function > > Hi- > > General question: > I have written a function that performs a permutation test. I use the > function "sample" to randomize my data, then I call on > another function > I wrote to analyze the randomized data. This process repeats "n" > times-- 10, 100 or whatever. > > The function applied to the randomized data creates a matrix and > sometimes the matrix contains NaN's. I would like my > function to simply > ignore any matrices that contain NaN's and continue to cycle until it > has generated "n" matrices without any NaN's. I am aware of the > function "is.nan", but I'm not sure how to fit it in. > > > > Specifics: > Here's the section of code that needs improvement: > > for (i in 1:n){ > S[,,]= sample (Y[,,]) > dig=auto(S,H,J,T) > out[i,]=dig[1,]} > > > n= number of randomizations > S= array to store randomized data > Y= original data array > dig= the matrix returned by the function "auto" > out= matrix that collects the first row of each "dig" matrix up to "n" > > Sometimes "dig" includes NaN's. I want the function to ignore those > "dig"s but keep working until it has generated "n" matrices > that contain > no NaN's. > > How could I do this? > > Thanks. > > Jason Mills > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >