{"id":430,"date":"2012-05-04T12:15:56","date_gmt":"2012-05-04T16:15:56","guid":{"rendered":"http:\/\/web.colby.edu\/mgimond\/?p=430"},"modified":"2012-05-04T12:16:50","modified_gmt":"2012-05-04T16:16:50","slug":"430","status":"publish","type":"post","link":"https:\/\/web.colby.edu\/mgimond\/2012\/05\/04\/430\/","title":{"rendered":"Is Moran&#8217;s I robust?"},"content":{"rendered":"<p>Moran&#8217;s I (both global and local) is a measure of spatial autocorrelation. \u00a0One form of its equation looks like this:<\/p>\n<img src='https:\/\/s0.wp.com\/latex.php?latex=++I+%3D+%5Cfrac%7BN%7D%7B%5Csum_%7Bi%7D+%5Csum_%7Bj%7D+w_%7Bij%7D%7D+%5Cfrac%7B%5Csum_%7Bi%7D+%5Csum_%7Bj%7D+w_%7Bij%7D+%28X_i+-+%5Cbar+X%29%28X_j+-+%5Cbar+X%29%7D%7B%5Csum_%7Bi%7D+%28X_i+-+%5Cbar+X+%29%5E2%7D&#038;bg=EEEEEE&#038;fg=333333&#038;s=3' alt='  I = \\frac{N}{\\sum_{i} \\sum_{j} w_{ij}} \\frac{\\sum_{i} \\sum_{j} w_{ij} (X_i - \\bar X)(X_j - \\bar X)}{\\sum_{i} (X_i - \\bar X )^2}' title='  I = \\frac{N}{\\sum_{i} \\sum_{j} w_{ij}} \\frac{\\sum_{i} \\sum_{j} w_{ij} (X_i - \\bar X)(X_j - \\bar X)}{\\sum_{i} (X_i - \\bar X )^2}' class='latex' \/>\n<p>Note the residual and spread terms. As such, Moran&#8217;s I can be sensitive to outliers. The following example demonstrates this point:<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\nlibrary(spdep)\r\nlibrary(maptools)\r\n# Load NC data\r\nNC &lt;- readShapePoly(system.file(&quot;etc\/shapes\/sids.shp&quot;, package=&quot;spdep&quot;)[1],\r\n      ID=&quot;FIPSNO&quot;, proj4string=CRS(&quot;+proj=longlat +ellps=clrk66&quot;))\r\nrn &lt;- sapply(slot(NC, &quot;polygons&quot;), function(x) slot(x, &quot;ID&quot;))\r\nNB &lt;- read.gal(system.file(&quot;etc\/weights\/ncCR85.gal&quot;, package=&quot;spdep&quot;)[1], \r\n      region.id=rn)\r\nn  &lt;- length(NB)\r\n# Define some variables\r\nset.seed(4956)\r\nx.norm &lt;- rnorm(n)     # Normal distribution\r\nrho    &lt;- 0.3          # autoregressive parameter\r\nW      &lt;- nb2listw(NB) # Generate spatial weights\r\n# Generate spatially autocorrelated datasets \r\n# (one normally distributed the other skewed)\r\nx.norm.auto &lt;- invIrW(W, rho) %*% x.norm # Generate autocorrelated values\r\nx.skew.auto &lt;- exp(x.norm.auto) # Transform orginal data to create a 'skewed' version\r\n<\/pre>\n<p>You can map the simulated data as follows:<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Append data to NC polygon object\r\nauto_sk &lt;- as.vector(x.skew.auto)\r\nauto_no &lt;- as.vector(x.norm.auto)\r\nrn      &lt;- as.integer(rn)\r\nNCa     &lt;- spCbind(NC,auto_sk)\r\nNCa     &lt;- spCbind(NCa,auto_no)\r\nNCa     &lt;- spCbind(NCa,rn)   # Needed if the GAL file is to be used in GeoDa\r\n# Plot map (skewed data in this example)\r\nbrks &lt;- quantile(auto_sk, seq(0,1,1\/10))\r\ncols &lt;- grey.colors(length(brks)-1, 0.95, 0.55, 2.2)\r\nplot(NCa, col=cols[findInterval(auto_sk, brks, all.inside=TRUE)])\r\n<\/pre>\n<p><a href=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot02.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot02.jpg\" alt=\"\" width=\"438\" height=\"194\" class=\"alignnone size-full wp-image-477\" srcset=\"https:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot02.jpg 438w, https:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot02-300x132.jpg 300w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/a><\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Compute Moran's I for both datasets\r\nI.norm = moran.test(x.norm.auto, listw=W)\r\nI.skew = moran.test(x.skew.auto, listw=W)\r\nI.norm$p.value;I.skew$p.value\r\n# The above approach makes assumptions about the Moran's I when in fact, it may not be.\r\n# To correct for this, we can generate an empirical sampling distribution function\r\n# using a Monte Carlo procedure.\r\nMCI.norm &lt;- moran.mc(x.norm.auto, listw=W, nsim=9999)\r\nMCI.skew &lt;- moran.mc(x.skew.auto, listw=W, nsim=9999)\r\n# Compare the P-values\r\nMCI.norm$p.value;MCI.skew$p.value\r\n<\/pre>\n<p>The p-values from the Monte Carlo simulations are 0.0133 and 0.1672 for the normal and skewed data respectively. The test results under different data distributions provide us with two distinct conclusions at a 5% significance level!<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\noopar &lt;- par(mfrow=c(1,2), mar=c(4,4,3,2)+0.1)\r\nmoran.plot(x.norm.auto,W)\r\nmoran.plot(x.skew.auto,W)\r\npar(oopar)\r\n<\/pre>\n<p><a href=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot.jpeg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot.jpeg\" alt=\"\" width=\"594\" height=\"313\" class=\"alignnone size-full wp-image-479\" srcset=\"https:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot.jpeg 594w, https:\/\/web.colby.edu\/mgimond\/files\/2012\/05\/Rplot-300x158.jpg 300w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/a> <\/p>\n<p>NOTE: To export the spatial data to a shapefile for further analysis in GeoDa:<\/p>\n<pre class=\"brush: r; title: ; notranslate\" title=\"\">\r\n# Write map object to shapefile\r\nwritePolyShape(NCa,&quot;NCa&quot;,factor2char = F)\r\n<\/pre>\n<p>&#8211;&gt; If using Geoda, the weight file can be found under C:\\Program Files\\R\\R-2.12.2\\library\\spdep\\etc\\weights<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Moran&#8217;s I (both global and local) is a measure of spatial autocorrelation. \u00a0One form of its equation looks like this: Note the residual and spread terms. As such, Moran&#8217;s I can be sensitive to outliers. The following example demonstrates this point: You can map the simulated data as follows: The p-values from the Monte Carlo [&hellip;]<\/p>\n","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\/430"}],"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=430"}],"version-history":[{"count":59,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/430\/revisions"}],"predecessor-version":[{"id":520,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/posts\/430\/revisions\/520"}],"wp:attachment":[{"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/media?parent=430"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/categories?post=430"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/web.colby.edu\/mgimond\/wp-json\/wp\/v2\/tags?post=430"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}