I have a set of points on a grid which is a subset of a larger background set of points on a grid. I would like to test if the subset points are randomly distributed (or not). If it helps, here is the real-world example I am working on:
The length of the yellow sausages is proportional to the number of genes in the subset, while the area of the grey circles is proportional to the number of genes at each grid point belonging to the total/background set of genes. In this case the yellow genes are clearly non-randomly distributed. The spatial distribution is important, I believe... imagine just three yellow genes "landing" in three neighbouring grid squares - I'm pretty sure that considering each grid point in isolation there is nothing special about the sampling - however the fact that the three genes' grid nodes are so close on the grid of 500 nodes is special/interesting, right?
I think the R spatstat package can help me here but I am not sure how to take into account the "background distribution".
Here's what I have managed so far - with dummy random data:
library(spatstat) # here is the "background" distribution of 100 points on a discrete 10x5 grid # (yes there are duplicate points at the same coordinates) bg = ppp(sample(0:9, 100, replace=T), sample(0:4, 100, replace=T), window=owin(c(0,9),c(0,4))) # if you want to visualise the density of points: # make a bitmap image version bgim = as.im(bg, dimyx=c(5,10)) # plot(bgim) # now make a random subset of the points subset = bg[sample(1:100, 10, replace=F)] # plot bg with plus signs plot(bg, pch=3) # add the fg points plot(subset, add=T)
So I can do test for CSR as follows
but I don't know how to include the background distribution. I've tried fitting models with ppm but am out of my depth. Can anyone help please?
If possible I'd like the test to be fast (1-2s max on subsets up to a few thousand genes out of the total of 12,500 genes).
Writing this maybe I have a rough idea how to tackle this... Should I load the entire set of genes into a ppp (point pattern) and a "mark" the subset with a label (e.g. selected/unselected)? Then maybe some of the simple tests will then work straight out of the box? Think of it like a distribution of trees (the forest kind), and some are marked as infected with a disease. You would want to know if the disease is localised with respect to the background distribution of trees.
Update: my solution thanks to Andy W.
Pass an expression which generates random subsets of the background point set (the same size as the subset of interest of course)
result = mad.test(subset, simulate=expression(bg[sample(1:bg$n,subset$n,replace=F)]), nsim=1000)