I want to sample stratified random points using R. I found the sampleStratified function from the R raster package.

This functions samples the same amount of points for each class of my classification, but I want to have points that are randomly distributed within each class, where each class has a number of points proportional to its relative area.

Can anybody suggest another way of doing that?

  • Wouldn't that just random points across the entire map? That way, the number of points per class will proportional to the relative area, due to the nature of random points? Sep 16 '19 at 9:17
  • I have two very small classes with only 1-5 % of image coverage. My aim is to collect 10000 points for 3750000 pixels. The problem might be that the random sampling underpresent the two small classes. Sep 16 '19 at 9:22

First, determine the fraction of the total area that each class takes up.
This is best done by just evaluating the total number of pixels with the given value, easily done iteratively. Perhaps by just reading all pixels into a long vector and subsetting that:

inputRaster <- raster(result_from_classification)    
classifed_values <- values(inputRaster)
data_from_1s <- classified_values[which(classified_values==1)
number_of_1s <- NROW(data_from_1s)


Second, you need to determine the number of samples you want per class and extract that number of samples from the class:

number_of_samples <- 10000  
number_of_pixels <- 3750000  
number_of_samples_of_1s <- number_of_1s / number_of_pixels * number_of_samples 
representative_sample_of_1s <- sample( data_from_1s , number_of_samples_of_1s )

All the code above has not been tested for errors, but should be fairly close to functioning.

  • Thanks it works pretty well! Sep 17 '19 at 19:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.