{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Check Spatial Autocorrelation of residuals\n", "ArcGIS PRO - R" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "###### Importing ####\n", "library(arcgisbinding)\n", "library(sp)\n", "library(spdep)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#### Initialize arcgisbinding ####\n", "arc.check_product()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "##### Current workspace \n", "getwd()\n", "\n", "#### Input Variables ####\n", "fullPath = '../data'\n", "inputFC = file.path(fullPath, \"pysal_automodel.shp\")\n", "galFile = file.path(fullPath,\"queen.gal\")\n", "uniqueField = 'MYID'\n", "residualField = 'RESID'\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "### Loading dataset\n", "info <- arc.open(inputFC)\n", "#### Create Data.Frame ####\n", "df <- arc.select(info, c(uniqueField, residualField))\n", "head(df)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#### Spatial Data Object####\n", "spObject <- arc.data2sp(df)\n", "\n", "#### Plot Spatial Data #### \n", "spplot(spObject[residualField])" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "#### Loading Spatial Dependence Weight Matrix ####\n", "gal <- read.gal(galFile, df[[uniqueField]])\n", "\n", "#### Weights #####\n", "col.W <- nb2listw(gal, style=\"W\")\n", "col.W" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "### Calculate Moran ####\n", " moranI = moran.test(df[[residualField]], col.W)\n", " print( paste(\"Moran's I:\" , moranI$statistic))\n", " print( paste(\"Moran's I p-value:\" , moranI$p.value))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "### Moran plot ###\n", "moran.plot(df[[residualField]], col.W)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "df['STDNORM'] = randnorm\n", "arc.write(outputFC, df)" ] } ], "metadata": { "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "3.3.0" } }, "nbformat": 4, "nbformat_minor": 2 }