Dear useRs I am looking for a way to randomise the values within a matrix: the conditions are that the sums of the rows and the sums of the columns should remain the same as in the original matrix. Any help would be appreciated Cheers Yann
"Yann Clough" <y.clough at NS1.uaoe.gwdg.de> writes:> Dear useRs > > I am looking for a way to randomise the values within a matrix: > the conditions are that the sums of the rows and the sums of the columns should > remain the same as in the original matrix. > > Any help would be appreciatedEr, explain further... Are you shuffling around constant numbers (as in the 0/1 values in item analysis) or can they be integers of varying magnitude (as in coloured-balls-in-urns), or ? -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
Dear Yann, Whether you can do this depends upon what you mean. If you mean randomly permute the elements of the matrix preserving row and column sums, then this generally won't be possible. Consider, e.g., matrix(1:4, 2, 2). If you mean generate a matrix with specified row and column sums but randomly sampled elements, then it should be possible to do it. For example, if all elements are positive, you can generate a matrix with unconstrained random elements and then make them match the required row and column sums by iterative proportional fitting. The loglin function in the MASS package will do this: loglin(X, list(1, 2), fit=TRUE)$fit (where your original matrix with random elements is X). If there are negative elements, then you could add a constant, adjust as above, and subtract the constant. I hope this helps, John -------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario Canada L8S 4M4 905-525-9140x23604 http://socserv.mcmaster.ca/jfox --------------------------------> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Yann Clough > Sent: Wednesday, February 09, 2005 6:08 AM > To: r-help at stat.math.ethz.ch > Subject: [R] randomisation > > Dear useRs > > I am looking for a way to randomise the values within a matrix: > the conditions are that the sums of the rows and the sums of > the columns should remain the same as in the original matrix. > > Any help would be appreciated > Cheers > > Yann > > ______________________________________________ > 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
I am working on an ecological problem and dealing with a matrix where rows correspond to samples, and columns correspond to species. The values in the matrix are recorded abundances of the organisms. I want to create a series of randomised datasets where total abundances per sample (rowSums) and per species (colSums) are equal to those in the dataset of my observations. Simple example of the kind of thing I have: matrix(c(1,0,2,10,1,3,5,6,7,1,0,0),nrow=4, ncol=3,by=row) # observed data rowSums(tempmatrix) #individuals per location, colSums(tempmatrix) #individuals per species example of a matrix which complies with the two restrictions: tempmatrix2=matrix(c(1,0,2,11,0,3,5,6,7,0,1,0),nrow=4, ncol=3,by=row) rowSums(tempmatrix2) colSums(tempmatrix2) hope this is clear Cheers Yann *************************** Yann Clough Fachgebiet Agraroekologie Waldweg 26 D-37073 Goettingen Tel: 0551/39-2157 email: y.clough at uaoe.gwdg.de www: http://wwwuser.gwdg.de/~uaoe/mitarbeiter/y_clough.htm
Yann Clough <y.clough <at> NS1.uaoe.gwdg.de> writes: : : Dear useRs : : I am looking for a way to randomise the values within a matrix: : the conditions are that the sums of the rows and the sums of the columns should : remain the same as in the original matrix. : ?r2dtable
Selon Yann Clough <y.clough at NS1.uaoe.gwdg.de>:> Dear useRs > > I am looking for a way to randomise the values within a matrix: > the conditions are that the sums of the rows and the sums of the columns > should > remain the same as in the original matrix. > > Any help would be appreciated > Cheers > > Yann > > ______________________________________________ > 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 >Hello, your question makes me think about what does the chisq.test function with simulate.p.value = T, maybe that could help to look what is done there. The help page of chisq.test points to that reference wich seems quite like what you are looking for : Patefield, W. M. (1981) Algorithm AS159. An efficient method of generating r x c tables with given row and column totals. Applied Statistics 30, 91-97. Romain.
Thanks to Gabor, Francois, Peter D., Jari, Peter R.and John r2dtable is what I was looking for, it implements the Patefield algorithm. Patefield, W. M. (1981) Algorithm AS159. An efficient method of generating r x c tables with given row and column totals. Applied Statistics 30, 91 97. This definitely made my day! Cheers Yann