random sampling - matrix

后端 未结 2 858
青春惊慌失措
青春惊慌失措 2020-12-11 19:47

How can I take a sample of n random points from a matrix populated with 1\'s and 0\'s ?

a=rep(0:1,5)
b=rep(0,10)
c=rep(1,10)
dataset=matrix(cbind(a,b,c),nrow         


        
2条回答
  •  一整个雨季
    2020-12-11 20:26

    Sample seems the best bet for you. To get 1000 random positions you can do something like:

    rows = sample(1:nrow(dataset), 1000, replace = TRUE)
    columns = sample(1:ncol(dataset), 1000, replace = TRUE)
    

    I think this gives what you want, but ofcourse I could be mistaken.

    Extracting the items from the matrix can be done like:

    random_sample = mapply(function(row, col) 
                               return(dataset[row,col]), 
                        row = rows, col = columns)
    

    Sampling strategies

    In the comments you speak that your sample needs to have spread. A random sample has no garantuees that there will be no clusters, because of its random nature. There are several more sampling schemes that might be interesting to explore:

    • Regular sampling, skip the randomness and just sample regularly. Samples the entire matrix space evenly, but there is no randomness.
    • Stratified random sampling, you divide your matrix space into regular subset, and then sample randomly in those subsets. Presents a mix between random and regular.

    To check if your random sampling produces good results, I'd repeat the random sampling a few times and compare the results (as I assume that the sampling will be input for another analysis?).

提交回复
热议问题