[R] randomisation

John Fox jfox at mcmaster.ca
Wed Feb 9 14:07:15 CET 2005


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




More information about the R-help mailing list