{"id":378,"date":"2012-01-31T14:56:21","date_gmt":"2012-01-31T18:56:21","guid":{"rendered":"http:\/\/web.colby.edu\/mgimond\/?p=378"},"modified":"2012-04-18T09:21:14","modified_gmt":"2012-04-18T13:21:14","slug":"r-comparing-two-point-patterns","status":"publish","type":"post","link":"https:\/\/web.colby.edu\/mgimond\/2012\/01\/31\/r-comparing-two-point-patterns\/","title":{"rendered":"R: Comparing two marked point patterns"},"content":{"rendered":"<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# The following script was inspired by a discussion at http:\/\/gis.stackexchange.com\/q\/19522\/4490\r\n# (later merged with http:\/\/gis.stackexchange.com\/q\/4484\/4490)\r\n#\r\n# The script performs permutations to compare two marked point patterns. A simple ANN statistic\r\n# is used in this example.\r\n#\r\nlibrary(spatstat)\r\n\r\n# Generate random points\r\nZ = as.im(function(x,y){ 5 * exp(2 * x - 1) }, owin()) # Generates a sloped density surface\r\nP1 = rpoint(100, Z)\r\nP2 = rpoint(50, Z)\r\n\r\n# Combine points into a single markes PPP\r\nP = superimpose(A=P1,B=P2) # Assign label A to P1 and B to P2\r\n\r\n# Plot points\r\nplot.owin(P$window,col=&quot;#DDDDDD&quot;)\r\nplot(split(P)$B,pch=16,cols=&quot;white&quot;,add=T)\r\nplot(split(P)$A,pch=16,cols=&quot;blue&quot;,add=T)\r\n<\/pre>\n<p><a href=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/points.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-380\" src=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/points.jpg\" alt=\"\" width=\"225\" height=\"227\" srcset=\"https:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/points.jpg 225w, https:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/points-150x150.jpg 150w\" sizes=\"(max-width: 225px) 100vw, 225px\" \/><\/a><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Permutate labels (do not change point locations)\r\nnn.sim = vector()\r\nP.r = P\r\nfor(i in 1:1999){\r\n marks(P.r) = sample(P$marks) # Randomly reassign labels\r\n nn.sim[i] = mean(nncross(split(P.r)$A,split(P.r)$B)$dist)\r\n}\r\n\r\n# Plot distribution and original statistic (using red line)\r\nhist(nn.sim, breaks=30)\r\nabline(v=mean(nncross(split(P)$A,split(P)$B)$dist),col=&quot;red&quot;)\r\n<\/pre>\n<p><a href=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/hist.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-381\" src=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/hist.jpg\" alt=\"\" width=\"403\" height=\"425\" srcset=\"https:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/hist.jpg 403w, https:\/\/web.colby.edu\/mgimond\/files\/2012\/01\/hist-284x300.jpg 284w\" sizes=\"(max-width: 403px) 100vw, 403px\" \/><\/a><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Compute empirical cumulative distribution\r\nnn.sim.ecdf = ecdf(nn.sim)\r\n# See how the original stat compares to the simulated distribution&lt;\/pre&gt;\r\nnn.sim.ecdf(mean(nncross(split(P)$A,split(P)$B)$dist))\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1199,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[12807,12808],"tags":[],"_links":{"self":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/378"}],"collection":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/users\/1199"}],"replies":[{"embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/comments?post=378"}],"version-history":[{"count":11,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":427,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/378\/revisions\/427"}],"wp:attachment":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/media?parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/categories?post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/tags?post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}