{"id":1369,"date":"2026-10-22T08:04:10","date_gmt":"2026-10-22T08:04:10","guid":{"rendered":"https:\/\/helpwithmygisassignment.com\/blog\/?p=1369"},"modified":"2026-10-22T08:04:29","modified_gmt":"2026-10-22T08:04:29","slug":"spatial-regression-with-r-spdep","status":"publish","type":"post","link":"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/","title":{"rendered":"Spatial Regression with R: Fitting Spatial Lag and Spatial Error Models in spdep"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_89 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Spatial_Regression_with_R_Fitting_Spatial_Lag_and_Spatial_Error_Models_in_spdep\" >Spatial Regression with R: Fitting Spatial Lag and Spatial Error Models in spdep<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Introduction\" >Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#1_Prerequisites\" >1. Prerequisites<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#2_Load_Spatial_Polygon_Data\" >2. Load Spatial Polygon Data<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Visualize_the_dependent_variable\" >Visualize the dependent variable<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#3_Run_a_Baseline_OLS_Regression\" >3. Run a Baseline OLS Regression<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#4_Build_a_Spatial_Weights_Matrix\" >4. Build a Spatial Weights Matrix<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#41_Queen_Contiguity\" >4.1 Queen Contiguity<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#42_Rook_Contiguity\" >4.2 Rook Contiguity<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#5_Convert_Neighbors_to_Spatial_Weights\" >5. Convert Neighbors to Spatial Weights<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#6_Check_the_Spatial_Structure\" >6. Check the Spatial Structure<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#7_Test_OLS_Residuals_for_Spatial_Autocorrelation\" >7. Test OLS Residuals for Spatial Autocorrelation<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Why_this_matters\" >Why this matters<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#8_Run_Lagrange_Multiplier_Diagnostics\" >8. Run Lagrange Multiplier Diagnostics<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-15\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Important_version_note\" >Important version note<\/a><\/li><\/ul><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-16\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#9_Understanding_the_Spatial_Lag_Model\" >9. Understanding the Spatial Lag Model<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-17\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#10_Fit_the_spatial_Lag_Model_in_R_using_Spatial_Regression_with_R_spdep\" >10. Fit the spatial Lag Model in R using Spatial Regression with R spdep<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-18\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#101_Interpreting_rho\" >10.1 Interpreting \\(\\rho\\)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-19\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#11_Calculate_Spatial_Lag_Impacts\" >11. Calculate Spatial Lag Impacts<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-20\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#12_Fit_the_Spatial_Error_Model\" >12. Fit the Spatial Error Model<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-21\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#13_Estimate_the_Spatial_Error_Model_in_R\" >13. Estimate the Spatial Error Model in R<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-22\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#131_Interpreting_lambda\" >13.1 Interpreting \\(\\lambda\\)<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-23\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#14_Compare_OLS_Spatial_Lag_and_Spatial_Error_Models\" >14. Compare OLS, Spatial Lag, and Spatial Error Models<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-24\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#141_Interpreting_AIC\" >14.1 Interpreting AIC<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-25\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#142_Interpreting_Log-Likelihood\" >14.2 Interpreting Log-Likelihood<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-26\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#15_A_Complete_Reproducible_Workflow\" >15. A Complete Reproducible Workflow<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-27\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#16_A_Practical_Decision_Framework\" >16. A Practical Decision Framework<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-28\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#17_Common_Problems_and_Troubleshooting\" >17. Common Problems and Troubleshooting<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-29\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Problem_1_Invalid_geometries\" >Problem 1: Invalid geometries<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-30\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Problem_2_Islands_with_no_neighbors\" >Problem 2: Islands with no neighbors<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-31\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Problem_3_Coordinate_reference_system\" >Problem 3: Coordinate reference system<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-32\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#18_Queen_vs_Rook_Which_Should_You_Use\" >18. Queen vs Rook: Which Should You Use?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-33\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#19_What_You_Should_Report_in_an_Applied_Study\" >19. What You Should Report in an Applied Study<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-34\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/#Summary\" >Summary<\/a><\/li><\/ul><\/nav><\/div>\n<h1><span class=\"ez-toc-section\" id=\"Spatial_Regression_with_R_Fitting_Spatial_Lag_and_Spatial_Error_Models_in_spdep\"><\/span>Spatial Regression with R: Fitting Spatial Lag and Spatial Error Models in <code dir=\"ltr\">spdep<\/code><span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1474\" src=\"https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_dczmwdczmwdczmwd-1024x559.jpg\" alt=\"Spatial Regression with R spdep workflow diagram illustrating spatial dependence and OLS residuals\" width=\"1024\" height=\"559\" srcset=\"https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_dczmwdczmwdczmwd-1024x559.jpg 1024w, https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_dczmwdczmwdczmwd-300x164.jpg 300w, https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_dczmwdczmwdczmwd-768x419.jpg 768w, https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_dczmwdczmwdczmwd.jpg 1408w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>When performing spatial regression with R<a href=\"ttps:\/\/cran.r-project.org\/package=spdep) \u2014 spatial weights and spatial dependence diagnostics\" target=\"_blank\" rel=\"noopener\"> spdep<\/a>, Ordinary Least Squares (OLS) is often the natural starting point for regression analysis. Ordinary Least Squares (OLS) is often the natural starting point for regression analysis. In a conventional linear model, we assume that observations are sufficiently independent that the error terms do not exhibit systematic spatial relationships.<\/p>\n<p>That assumption can be problematic when the observations are geographic units such as counties, census tracts, wards, districts, or administrative polygons.<\/p>\n<p>For example, suppose we model household income across neighboring counties. Economic conditions in one county may influence neighboring counties, while similar counties may also share unobserved regional characteristics. Consequently, observations that are geographically close may not be statistically independent.<\/p>\n<p>A conventional OLS model can be written as:<\/p>\n<p><span data-math-source=\"y = X\\beta + \\epsilon\" data-math-display=\"true\">\\[ y = X\\beta + \\epsilon \\]<\/span><\/p>\n<p>where:<\/p>\n<ul>\n<li><span data-math-source=\"y\" data-math-display=\"false\">\\(y\\)<\/span> = dependent variable<\/li>\n<li><span data-math-source=\"X\" data-math-display=\"false\">\\(X\\)<\/span> = explanatory variables<\/li>\n<li><span data-math-source=\"\\beta\" data-math-display=\"false\">\\(\\beta\\)<\/span> = regression coefficients<\/li>\n<li><span data-math-source=\"\\epsilon\" data-math-display=\"false\">\\(\\epsilon\\)<\/span> = error term<\/li>\n<\/ul>\n<p>When spatial dependence exists, the error structure may violate the assumptions underlying conventional OLS inference. Standard errors can become unreliable, making significance tests and p-values misleading.<\/p>\n<p>Spatial econometrics addresses this problem by explicitly incorporating relationships between neighboring observations.<\/p>\n<p>Two fundamental models are:<\/p>\n<p><strong>Spatial Lag Model (SAR):<\/strong><\/p>\n<p><span data-math-source=\"y = \\rho Wy + X\\beta + \\epsilon\" data-math-display=\"true\">\\[ y = \\rho Wy + X\\beta + \\epsilon \\]<\/span><\/p>\n<p>where <span data-math-source=\"\\rho\" data-math-display=\"false\">\\(\\rho\\)<\/span> measures dependence between the dependent variable and neighboring observations.<\/p>\n<p><strong>Spatial Error Model (SEM):<\/strong><\/p>\n<p><span data-math-source=\"y = X\\beta + u\" data-math-display=\"true\">\\[ y = X\\beta + u \\]<\/span><\/p>\n<p>with<\/p>\n<p><span data-math-source=\"u = \\lambda Wu + \\epsilon\" data-math-display=\"true\">\\[ u = \\lambda Wu + \\epsilon \\]<\/span><\/p>\n<p>where <span data-math-source=\"\\lambda\" data-math-display=\"false\">\\(\\lambda\\)<\/span> represents spatial dependence in the error process.<\/p>\n<p>In R, packages such as <code dir=\"ltr\">spdep<\/code> and <a href=\"https:\/\/cran.r-project.org\/package=spatialreg](https:\/\/cran.r-project.org\/package=spatialreg\" target=\"_blank\" rel=\"noopener\"><code dir=\"ltr\">spatialreg<\/code><\/a> provide tools for constructing spatial weights, diagnosing spatial dependence, and estimating these models.<\/p>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"1_Prerequisites\"><\/span>1. Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>We will use four main R packages:<\/p>\n<ul>\n<li><strong><code dir=\"ltr\">sf<\/code><\/strong> \u2014 reading and manipulating spatial vector data<\/li>\n<li><strong><code dir=\"ltr\">spdep<\/code><\/strong> \u2014 spatial weights and spatial dependence diagnostics<\/li>\n<li><strong><code dir=\"ltr\">spatialreg<\/code><\/strong> \u2014 spatial regression models<\/li>\n<li><strong><code dir=\"ltr\">tmap<\/code><\/strong> \u2014 mapping and visual inspection<\/li>\n<\/ul>\n<p>Install them if necessary:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Install packages once if they are not already installed\r\ninstall.packages(c(\r\n  \"sf\",\r\n  \"spdep\",\r\n  \"spatialreg\",\r\n  \"tmap\"\r\n))<\/code><\/pre>\n<\/div>\n<p>Load the packages:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Load required libraries\r\nlibrary(sf)\r\nlibrary(spdep)\r\nlibrary(spatialreg)\r\nlibrary(tmap)<\/code><\/pre>\n<\/div>\n<p>A useful distinction is that <code dir=\"ltr\">spdep<\/code> is particularly important for <strong>spatial-neighbor structures and diagnostics<\/strong>, while <code dir=\"ltr\">spatialreg<\/code> contains functions for estimating spatial regression models such as <code dir=\"ltr\">lagsarlm()<\/code> and <code dir=\"ltr\">errorsarlm()<\/code>.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"2_Load_Spatial_Polygon_Data\"><\/span>2. Load Spatial Polygon Data<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>For this tutorial, assume you have a polygon dataset containing geographic units and socioeconomic variables.<\/p>\n<p>For example:<\/p>\n<ul>\n<li><code dir=\"ltr\">income<\/code> \u2014 average household income<\/li>\n<li><code dir=\"ltr\">unemployment<\/code> \u2014 unemployment rate<\/li>\n<li><code dir=\"ltr\">education<\/code> \u2014 percentage of adults with higher education<\/li>\n<li><code dir=\"ltr\">population_density<\/code> \u2014 population per square kilometer<\/li>\n<\/ul>\n<p>Suppose the dataset is stored as:<\/p>\n<div>\n<pre dir=\"ltr\"><code>data\/counties.shp<\/code><\/pre>\n<\/div>\n<p>Read it with <code dir=\"ltr\">sf<\/code>:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Read polygon data\r\ncounties &lt;- st_read(\"data\/counties.shp\")\r\n\r\n# Inspect the dataset\r\nprint(counties)\r\n\r\n# Display attribute names\r\nnames(counties)\r\n\r\n# Check the coordinate reference system\r\nst_crs(counties)<\/code><\/pre>\n<\/div>\n<p>You should verify that the variables required for your regression actually exist:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Examine the first few observations\r\nhead(counties)\r\n\r\n# Check the structure of the attribute table\r\nstr(counties)<\/code><\/pre>\n<\/div>\n<h3><span class=\"ez-toc-section\" id=\"Visualize_the_dependent_variable\"><\/span>Visualize the dependent variable<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Before modeling, map the dependent variable.<\/p>\n<div>\n<pre dir=\"ltr\"><code># Basic thematic map\r\ntm_shape(counties) +\r\n  tm_polygons(\r\n    col = \"income\",\r\n    title = \"Average Income\"\r\n  )<\/code><\/pre>\n<\/div>\n<p>This is not merely cosmetic. Mapping the dependent variable can reveal broad geographic patterns that may later manifest as spatial autocorrelation.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"3_Run_a_Baseline_OLS_Regression\"><\/span>3. Run a Baseline OLS Regression<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Start with the conventional model before introducing spatial econometrics.<\/p>\n<p>Suppose we want to explain <code dir=\"ltr\">income<\/code> using unemployment, education, and population density.<\/p>\n<div>\n<pre dir=\"ltr\"><code># Fit conventional OLS model\r\nols_model &lt;- lm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties\r\n)\r\n\r\n# Display regression results\r\nsummary(ols_model)<\/code><\/pre>\n<\/div>\n<p>The output will contain coefficients such as:<\/p>\n<div>\n<pre dir=\"ltr\"><code>Coefficients:\r\n\r\n                     Estimate   Std. Error   t value   Pr(&gt;|t|)\r\n(Intercept)            ...\r\nunemployment           ...\r\neducation              ...\r\npopulation_density     ...<\/code><\/pre>\n<\/div>\n<p>The coefficient estimates describe the association between each explanatory variable and income, holding the other variables constant.<\/p>\n<p>For example, if the coefficient for <code dir=\"ltr\">education<\/code> is positive, the model estimates that higher education levels are associated with higher income, conditional on the other predictors.<\/p>\n<p>However, these coefficients should not yet be interpreted as proof that OLS is appropriate.<\/p>\n<p>The next question is:<\/p>\n<blockquote><p><strong>Are the OLS residuals spatially independent?<\/strong><\/p><\/blockquote>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"4_Build_a_Spatial_Weights_Matrix\"><\/span>4. Build a Spatial Weights Matrix<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Spatial econometric models need a formal definition of which observations are considered neighbors.<\/p>\n<p>For polygon data, two common approaches are:<\/p>\n<ul>\n<li><strong>Queen contiguity<\/strong> \u2014 polygons sharing either an edge or a vertex are neighbors.<\/li>\n<li><strong>Rook contiguity<\/strong> \u2014 polygons must share an edge to be neighbors.<\/li>\n<\/ul>\n<p>The resulting neighbor structure is represented using a spatial weights matrix.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"41_Queen_Contiguity\"><\/span>4.1 Queen Contiguity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Use <code dir=\"ltr\">poly2nb()<\/code>:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Create Queen-contiguity neighbors\r\nqueen_nb &lt;- poly2nb(\r\n  counties,\r\n  queen = TRUE\r\n)\r\n\r\n# Inspect the neighbor object\r\nqueen_nb<\/code><\/pre>\n<\/div>\n<p>You can inspect the neighbors of individual polygons:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Neighbors of the first five polygons\r\nqueen_nb[1:5]<\/code><\/pre>\n<\/div>\n<p>For example, if polygon 1 returns:<\/p>\n<div>\n<pre dir=\"ltr\"><code>1: 2 5 8<\/code><\/pre>\n<\/div>\n<p>then polygons 2, 5, and 8 are considered neighbors of polygon 1.<\/p>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"42_Rook_Contiguity\"><\/span>4.2 Rook Contiguity<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Create a Rook-based neighbor structure with:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Create Rook-contiguity neighbors\r\nrook_nb &lt;- poly2nb(\r\n  counties,\r\n  queen = FALSE\r\n)\r\n\r\n# Inspect the neighbor structure\r\nrook_nb<\/code><\/pre>\n<\/div>\n<p>The difference is important.<\/p>\n<p>Imagine two polygons touching only at one corner:<\/p>\n<div>\n<pre dir=\"ltr\"><code>+-----+\r\n|  A  |\r\n+-----+\r\n      +\r\n      | B |\r\n      +<\/code><\/pre>\n<\/div>\n<p>Under <strong>Queen contiguity<\/strong>, they can be neighbors.<\/p>\n<p>Under <strong>Rook contiguity<\/strong>, they are not neighbors because they do not share an edge.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"5_Convert_Neighbors_to_Spatial_Weights\"><\/span>5. Convert Neighbors to Spatial Weights<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><code dir=\"ltr\">poly2nb()<\/code> creates a neighbor list, but spatial regression functions need a spatial weights object.<\/p>\n<p>Use <code dir=\"ltr\">nb2listw()<\/code>:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Convert Queen neighbors to row-standardized spatial weights\r\nqueen_listw &lt;- nb2listw(\r\n  queen_nb,\r\n  style = \"W\"\r\n)\r\n\r\n# Inspect the weights object\r\nqueen_listw<\/code><\/pre>\n<\/div>\n<p>The <code dir=\"ltr\">\"W\"<\/code> style creates row-standardized weights.<\/p>\n<p>For an observation with four neighbors, for example, each neighbor may receive a weight of:<\/p>\n<p><span data-math-source=\"\\frac{1}{4}=0.25\" data-math-display=\"true\">\\[ \\frac{1}{4}=0.25 \\]<\/span><\/p>\n<p>This means the spatial lag can be interpreted approximately as a weighted average of neighboring observations.<\/p>\n<p>You can create Rook weights in exactly the same way:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Convert Rook neighbors to spatial weights\r\nrook_listw &lt;- nb2listw(\r\n  rook_nb,\r\n  style = \"W\"\r\n)<\/code><\/pre>\n<\/div>\n<p>For the rest of this tutorial, we will use the Queen-contiguity weights.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"6_Check_the_Spatial_Structure\"><\/span>6. Check the Spatial Structure<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>It is useful to visualize the neighbor relationships.<\/p>\n<div>\n<pre dir=\"ltr\"><code># Obtain polygon centroids\r\ncentroids &lt;- st_centroid(counties)\r\n\r\n# Convert centroids to coordinates\r\ncoords &lt;- st_coordinates(centroids)\r\n\r\n# Plot polygons\r\nplot(st_geometry(counties))\r\n\r\n# Draw Queen neighbor links\r\nplot(\r\n  queen_nb,\r\n  coords,\r\n  add = TRUE,\r\n  col = \"red\"\r\n)<\/code><\/pre>\n<\/div>\n<p>You should see lines connecting neighboring polygons.<\/p>\n<p>This provides an important sanity check.<\/p>\n<p>A spatial weights matrix is not automatically correct simply because <code dir=\"ltr\">poly2nb()<\/code> successfully generated one. Your definition of &#8220;neighbor&#8221; should make substantive sense for the geographic process being modeled.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"7_Test_OLS_Residuals_for_Spatial_Autocorrelation\"><\/span>7. Test OLS Residuals for Spatial Autocorrelation<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Now extract the OLS residuals:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Extract OLS residuals\r\nols_residuals &lt;- residuals(ols_model)<\/code><\/pre>\n<\/div>\n<p>Use Moran&#8217;s I to test whether the residuals exhibit spatial autocorrelation:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Moran's I test on OLS residuals\r\nmoran.test(\r\n  ols_residuals,\r\n  queen_listw\r\n)<\/code><\/pre>\n<\/div>\n<p>A typical result contains:<\/p>\n<div>\n<pre dir=\"ltr\"><code>Moran I statistic\r\nExpectation\r\nVariance\r\np-value<\/code><\/pre>\n<\/div>\n<p>The null hypothesis is approximately:<\/p>\n<p><span data-math-source=\"H_0: I = E(I)\" data-math-display=\"true\">\\[ H_0: I = E(I) \\]<\/span><\/p>\n<p>meaning that the residuals do not exhibit spatial autocorrelation under the specified spatial structure.<\/p>\n<p>If the test produces a small p-value, there is evidence that the OLS residuals are spatially autocorrelated.<\/p>\n<p>For example:<\/p>\n<div>\n<pre dir=\"ltr\"><code>Moran I statistic = 0.31\r\np-value = 0.002<\/code><\/pre>\n<\/div>\n<p>would provide evidence against spatial independence.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Why_this_matters\"><\/span>Why this matters<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Suppose neighboring counties consistently have similar unexplained income values.<\/p>\n<p>OLS does not explicitly model that geographic relationship.<\/p>\n<p>The resulting inference may therefore underestimate or otherwise misrepresent uncertainty.<\/p>\n<p>This is where spatial regression becomes useful.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"8_Run_Lagrange_Multiplier_Diagnostics\"><\/span>8. Run Lagrange Multiplier Diagnostics<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Moran&#8217;s I tells us that spatial autocorrelation exists, but we also want to investigate <strong>what type of spatial model may be appropriate<\/strong>.<\/p>\n<p>The classic approach is to use Lagrange Multiplier (LM) diagnostics.<\/p>\n<p>Depending on your installed <code dir=\"ltr\">spdep<\/code> version, you may encounter the older <code dir=\"ltr\">lm.LMtests()<\/code> interface or the newer <code dir=\"ltr\">lm.RStests()<\/code> interface. For compatibility with workflows that specifically use <code dir=\"ltr\">lm.LMtests()<\/code>, the following code illustrates the requested approach.<\/p>\n<div>\n<pre dir=\"ltr\"><code># Run Lagrange Multiplier diagnostics\r\nlm_tests &lt;- lm.LMtests(\r\n  ols_model,\r\n  queen_listw,\r\n  test = \"all\"\r\n)\r\n\r\n# Display results\r\nlm_tests<\/code><\/pre>\n<\/div>\n<p>The diagnostics may include tests associated with:<\/p>\n<ul>\n<li>spatial lag dependence<\/li>\n<li>spatial error dependence<\/li>\n<li>robust spatial lag dependence<\/li>\n<li>robust spatial error dependence<\/li>\n<\/ul>\n<p>Conceptually, these tests help answer:<\/p>\n<blockquote><p>Is the spatial dependence primarily associated with the dependent variable itself, or with the error process?<\/p><\/blockquote>\n<h3><span class=\"ez-toc-section\" id=\"Important_version_note\"><\/span>Important version note<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In newer versions of <code dir=\"ltr\">spdep<\/code>, some users may see a deprecation warning for <code dir=\"ltr\">lm.LMtests()<\/code>.<\/p>\n<p>The modern equivalent is:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Modern spatial regression diagnostics\r\nlm.RStests(\r\n  ols_model,\r\n  queen_listw,\r\n  test = \"all\"\r\n)<\/code><\/pre>\n<\/div>\n<p>If your installed version recommends <code dir=\"ltr\">lm.RStests()<\/code>, use that function rather than treating the warning as a modeling error.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"9_Understanding_the_Spatial_Lag_Model\"><\/span>9. Understanding the Spatial Lag Model<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The <strong>Spatial Lag Model<\/strong>, also called the Spatial Autoregressive Model (SAR), introduces the spatially lagged dependent variable:<\/p>\n<p><span data-math-source=\"y = \\rho Wy + X\\beta + \\epsilon\" data-math-display=\"true\">\\[ y = \\rho Wy + X\\beta + \\epsilon \\]<\/span><\/p>\n<p>Here:<\/p>\n<ul>\n<li><span data-math-source=\"y\" data-math-display=\"false\">\\(y\\)<\/span> = dependent variable<\/li>\n<li><span data-math-source=\"Wy\" data-math-display=\"false\">\\(Wy\\)<\/span> = spatial lag of the dependent variable<\/li>\n<li><span data-math-source=\"X\" data-math-display=\"false\">\\(X\\)<\/span> = explanatory variables<\/li>\n<li><span data-math-source=\"\\beta\" data-math-display=\"false\">\\(\\beta\\)<\/span> = regression coefficients<\/li>\n<li><span data-math-source=\"\\rho\" data-math-display=\"false\">\\(\\rho\\)<\/span> = spatial autoregressive parameter<\/li>\n<li><span data-math-source=\"\\epsilon\" data-math-display=\"false\">\\(\\epsilon\\)<\/span> = random error<\/li>\n<\/ul>\n<p>The term <span data-math-source=\"Wy\" data-math-display=\"false\">\\(Wy\\)<\/span> represents neighboring values of the dependent variable.<\/p>\n<p>For our income example:<\/p>\n<p><span data-math-source=\"Wy\" data-math-display=\"true\">\\[ Wy \\]<\/span><\/p>\n<p>can be interpreted as a spatially weighted measure of neighboring counties&#8217; income.<\/p>\n<h1><span class=\"ez-toc-section\" id=\"10_Fit_the_spatial_Lag_Model_in_R_using_Spatial_Regression_with_R_spdep\"><\/span>10. Fit the spatial Lag Model in R using Spatial Regression with R spdep<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>&nbsp;<\/p>\n<p>Use <code dir=\"ltr\">lagsarlm()<\/code> from <code dir=\"ltr\"><a href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-window-functions-recursive-ctes-postgis\/\">spatialreg<\/a><\/code>:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Fit Spatial Lag Model\r\nlag_model &lt;- lagsarlm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties,\r\n  listw = queen_listw\r\n)\r\n\r\n# Display model results\r\nsummary(lag_model)<\/code><\/pre>\n<\/div>\n<p>The output contains the ordinary regression coefficients plus the spatial autoregressive parameter:<\/p>\n<div>\n<pre dir=\"ltr\"><code>Rho: ...<\/code><\/pre>\n<\/div>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"101_Interpreting_rho\"><\/span>10.1 Interpreting <span data-math-source=\"\\rho\" data-math-display=\"false\">\\(\\rho\\)<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The parameter <span data-math-source=\"\\rho\" data-math-display=\"false\">\\(\\rho\\)<\/span> measures spatial dependence in the dependent variable.<\/p>\n<p>Suppose you obtain:<\/p>\n<div>\n<pre dir=\"ltr\"><code>rho = 0.42<\/code><\/pre>\n<\/div>\n<p>A positive value indicates positive spatial dependence: higher values of the dependent variable in neighboring areas are associated with higher values in the focal area, after accounting for the explanatory variables.<\/p>\n<p>A value near zero indicates relatively weak spatial lag dependence.<\/p>\n<p>A negative value indicates negative spatial dependence.<\/p>\n<p>However, <strong>do not interpret <span data-math-source=\"\\rho\" data-math-display=\"false\">\\(\\rho\\)<\/span> as an ordinary regression slope<\/strong>.<\/p>\n<p>The model contains feedback because each area&#8217;s outcome is related to neighboring outcomes, while those neighbors are themselves related to other areas.<\/p>\n<p>Therefore, interpreting SAR models through the ordinary &#8220;one-unit increase in X produces a \u03b2-unit change in Y&#8221; framework can be misleading.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"11_Calculate_Spatial_Lag_Impacts\"><\/span>11. Calculate Spatial Lag Impacts<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>For a spatial lag model, coefficient interpretation is more complicated because of spatial feedback.<\/p>\n<p><code dir=\"ltr\">spatialreg<\/code> provides impact calculations.<\/p>\n<div>\n<pre dir=\"ltr\"><code># Calculate direct, indirect, and total impacts\r\nlag_impacts &lt;- impacts(\r\n  lag_model,\r\n  listw = queen_listw,\r\n  R = 1000\r\n)\r\n\r\n# Display impacts\r\nsummary(lag_impacts)<\/code><\/pre>\n<\/div>\n<p>The results generally distinguish between:<\/p>\n<ul>\n<li><strong>Direct impacts<\/strong> \u2014 effects within the focal observation<\/li>\n<li><strong>Indirect impacts<\/strong> \u2014 spillover effects across neighboring observations<\/li>\n<li><strong>Total impacts<\/strong> \u2014 combined direct and indirect effects<\/li>\n<\/ul>\n<p>This is often more informative than interpreting the raw SAR coefficient alone.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"12_Fit_the_Spatial_Error_Model\"><\/span>12. Fit the Spatial Error Model<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The <strong>Spatial Error Model (SEM)<\/strong> assumes that spatial dependence occurs in the error process rather than directly through the dependent variable.<\/p>\n<p>The model can be expressed as:<\/p>\n<p><span data-math-source=\"y=X\\beta+u\" data-math-display=\"true\">\\[ y=X\\beta+u \\]<\/span><\/p>\n<p>where:<\/p>\n<p><span data-math-source=\"u=\\lambda Wu+\\epsilon\" data-math-display=\"true\">\\[ u=\\lambda Wu+\\epsilon \\]<\/span><\/p>\n<p>The parameter <span data-math-source=\"\\lambda\" data-math-display=\"false\">\\(\\lambda\\)<\/span> measures spatial dependence among the model errors.<\/p>\n<p>This is useful when omitted regional factors, measurement issues, institutional effects, environmental conditions, or other unobserved processes are spatially clustered.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"13_Estimate_the_Spatial_Error_Model_in_R\"><\/span>13. Estimate the Spatial Error Model in R<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>Use <code dir=\"ltr\">errorsarlm()<\/code>:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Fit Spatial Error Model\r\nerror_model &lt;- errorsarlm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties,\r\n  listw = queen_listw\r\n)\r\n\r\n# Display model results\r\nsummary(error_model)<\/code><\/pre>\n<\/div>\n<p>The output includes:<\/p>\n<div>\n<pre dir=\"ltr\"><code>Lambda: ...<\/code><\/pre>\n<\/div>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"131_Interpreting_lambda\"><\/span>13.1 Interpreting <span data-math-source=\"\\lambda\" data-math-display=\"false\">\\(\\lambda\\)<\/span><span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Suppose:<\/p>\n<div>\n<pre dir=\"ltr\"><code>lambda = 0.55<\/code><\/pre>\n<\/div>\n<p>A positive <span data-math-source=\"\\lambda\" data-math-display=\"false\">\\(\\lambda\\)<\/span> suggests that unexplained components of income are spatially correlated.<\/p>\n<p>In practical terms, neighboring counties may share unobserved characteristics that the explanatory variables do not capture.<\/p>\n<p>For example, nearby areas could share:<\/p>\n<ul>\n<li>regional economic policies<\/li>\n<li>infrastructure systems<\/li>\n<li>labor markets<\/li>\n<li>environmental conditions<\/li>\n<li>historical institutions<\/li>\n<li>unmeasured socioeconomic characteristics<\/li>\n<\/ul>\n<p>Unlike the SAR model, the SEM does <strong>not<\/strong> directly introduce neighboring values of <code dir=\"ltr\">income<\/code> into the structural equation.<\/p>\n<p>Instead, spatial dependence is modeled through the disturbance process.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"14_Compare_OLS_Spatial_Lag_and_Spatial_Error_Models\"><\/span>14. Compare OLS, Spatial Lag, and Spatial Error Models<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-1475\" src=\"https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_bxjyksbxjyksbxjy-1024x559.jpg\" alt=\"Spatial Regression with R spdep architecture chart comparing OLS, spatial lag, and spatial error models\" width=\"1024\" height=\"559\" srcset=\"https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_bxjyksbxjyksbxjy-1024x559.jpg 1024w, https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_bxjyksbxjyksbxjy-300x164.jpg 300w, https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_bxjyksbxjyksbxjy-768x419.jpg 768w, https:\/\/helpwithmygisassignment.com\/blog\/wp-content\/uploads\/2026\/10\/Gemini_Generated_Image_bxjyksbxjyksbxjy.jpg 1408w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>At this point we have three models:<\/p>\n<div>\n<pre dir=\"ltr\"><code># OLS\r\nols_model\r\n\r\n# Spatial Lag\r\nlag_model\r\n\r\n# Spatial Error\r\nerror_model<\/code><\/pre>\n<\/div>\n<p>A simple comparison can be created with:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Compare AIC values\r\nAIC(\r\n  ols_model,\r\n  lag_model,\r\n  error_model\r\n)<\/code><\/pre>\n<\/div>\n<p>You can also compare log-likelihood values:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Compare log-likelihoods\r\nlogLik(ols_model)\r\nlogLik(lag_model)\r\nlogLik(error_model)<\/code><\/pre>\n<\/div>\n<p>A convenient summary table is:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Create a simple model comparison table\r\nmodel_comparison &lt;- data.frame(\r\n  Model = c(\r\n    \"OLS\",\r\n    \"Spatial Lag\",\r\n    \"Spatial Error\"\r\n  ),\r\n  AIC = c(\r\n    AIC(ols_model),\r\n    AIC(lag_model),\r\n    AIC(error_model)\r\n  ),\r\n  LogLik = c(\r\n    as.numeric(logLik(ols_model)),\r\n    as.numeric(logLik(lag_model)),\r\n    as.numeric(logLik(error_model))\r\n  )\r\n)\r\n\r\n# Display comparison\r\nmodel_comparison<\/code><\/pre>\n<\/div>\n<p>You might obtain a table resembling:<\/p>\n<div>\n<pre dir=\"ltr\"><code>           Model      AIC    LogLik\r\n1           OLS   1250.4   -620.2\r\n2   Spatial Lag   1218.7   -602.4\r\n3 Spatial Error   1223.1   -604.6<\/code><\/pre>\n<\/div>\n<p>These numbers are illustrative; your actual results will depend on your dataset.<\/p>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"141_Interpreting_AIC\"><\/span>14.1 Interpreting AIC<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The Akaike Information Criterion balances model fit against model complexity.<\/p>\n<p>Generally:<\/p>\n<blockquote><p><strong>Lower AIC indicates a better trade-off between fit and complexity among the models being compared.<\/strong><\/p><\/blockquote>\n<p>Therefore, if the Spatial Lag Model has a substantially lower AIC than OLS, that provides evidence that explicitly modeling spatial dependence improves the model according to AIC.<\/p>\n<p>But AIC should not be used in isolation.<\/p>\n<p>You should also consider:<\/p>\n<ul>\n<li>spatial diagnostics<\/li>\n<li>theoretical justification<\/li>\n<li>coefficient stability<\/li>\n<li>residual spatial autocorrelation<\/li>\n<li>model assumptions<\/li>\n<li>the spatial process being studied<\/li>\n<\/ul>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"142_Interpreting_Log-Likelihood\"><\/span>14.2 Interpreting Log-Likelihood<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The log-likelihood measures how well the model explains the observed data under its assumed probability structure.<\/p>\n<p>For models estimated on the same dependent variable and observations, a higher log-likelihood generally indicates better fit.<\/p>\n<p>For example:<\/p>\n<div>\n<pre dir=\"ltr\"><code>OLS              -620.2\r\nSpatial Lag      -602.4\r\nSpatial Error    -604.6<\/code><\/pre>\n<\/div>\n<p>Both spatial models would have improved likelihood relative to OLS in this illustrative example.<\/p>\n<p>However, log-likelihood alone does not penalize additional model complexity. That is one reason AIC is useful for comparison.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"15_A_Complete_Reproducible_Workflow\"><\/span>15. A Complete Reproducible Workflow<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The entire workflow can be condensed into the following script.<\/p>\n<div>\n<pre dir=\"ltr\"><code># ============================================================\r\n# SPATIAL REGRESSION WITH R\r\n# OLS vs Spatial Lag vs Spatial Error\r\n# ============================================================\r\n\r\n# ------------------------------------------------------------\r\n# 1. Load packages\r\n# ------------------------------------------------------------\r\n\r\nlibrary(sf)\r\nlibrary(spdep)\r\nlibrary(spatialreg)\r\nlibrary(tmap)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 2. Load spatial polygon data\r\n# ------------------------------------------------------------\r\n\r\ncounties &lt;- st_read(\"data\/counties.shp\")\r\n\r\n# Inspect data\r\nprint(counties)\r\nnames(counties)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 3. Baseline OLS regression\r\n# ------------------------------------------------------------\r\n\r\nols_model &lt;- lm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties\r\n)\r\n\r\nsummary(ols_model)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 4. Build Queen spatial neighbors\r\n# ------------------------------------------------------------\r\n\r\nqueen_nb &lt;- poly2nb(\r\n  counties,\r\n  queen = TRUE\r\n)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 5. Convert neighbors to spatial weights\r\n# ------------------------------------------------------------\r\n\r\nqueen_listw &lt;- nb2listw(\r\n  queen_nb,\r\n  style = \"W\"\r\n)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 6. Check OLS residual spatial autocorrelation\r\n# ------------------------------------------------------------\r\n\r\nols_residuals &lt;- residuals(ols_model)\r\n\r\nmoran.test(\r\n  ols_residuals,\r\n  queen_listw\r\n)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 7. Lagrange Multiplier diagnostics\r\n# ------------------------------------------------------------\r\n\r\n# Older\/common interface\r\nlm.LMtests(\r\n  ols_model,\r\n  queen_listw,\r\n  test = \"all\"\r\n)\r\n\r\n# Newer spdep versions may use:\r\nlm.RStests(\r\n  ols_model,\r\n  queen_listw,\r\n  test = \"all\"\r\n)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 8. Spatial Lag Model\r\n# ------------------------------------------------------------\r\n\r\nlag_model &lt;- lagsarlm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties,\r\n  listw = queen_listw\r\n)\r\n\r\nsummary(lag_model)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 9. Spatial Error Model\r\n# ------------------------------------------------------------\r\n\r\nerror_model &lt;- errorsarlm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties,\r\n  listw = queen_listw\r\n)\r\n\r\nsummary(error_model)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 10. Compare model performance\r\n# ------------------------------------------------------------\r\n\r\nmodel_comparison &lt;- data.frame(\r\n  Model = c(\r\n    \"OLS\",\r\n    \"Spatial Lag\",\r\n    \"Spatial Error\"\r\n  ),\r\n\r\n  AIC = c(\r\n    AIC(ols_model),\r\n    AIC(lag_model),\r\n    AIC(error_model)\r\n  ),\r\n\r\n  LogLik = c(\r\n    as.numeric(logLik(ols_model)),\r\n    as.numeric(logLik(lag_model)),\r\n    as.numeric(logLik(error_model))\r\n  )\r\n)\r\n\r\nprint(model_comparison)\r\n\r\n\r\n# ------------------------------------------------------------\r\n# 11. Spatial impacts for SAR model\r\n# ------------------------------------------------------------\r\n\r\nlag_impacts &lt;- impacts(\r\n  lag_model,\r\n  listw = queen_listw,\r\n  R = 1000\r\n)\r\n\r\nsummary(lag_impacts)<\/code><\/pre>\n<\/div>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"16_A_Practical_Decision_Framework\"><\/span>16. A Practical Decision Framework<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>The workflow should not simply be:<\/p>\n<p><strong>OLS \u2192 SAR \u2192 SEM \u2192 choose whichever has the lowest AIC.<\/strong><\/p>\n<p>Instead, think of the process as a sequence of statistical questions.<\/p>\n<div>\n<div>\n<div>\n<table>\n<thead>\n<tr>\n<th>Stage<\/th>\n<th>Question<\/th>\n<th>R function<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>1<\/td>\n<td>What is the baseline relationship?<\/td>\n<td><code dir=\"ltr\">lm()<\/code><\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>Are residuals spatially autocorrelated?<\/td>\n<td><code dir=\"ltr\">moran.test()<\/code><\/td>\n<\/tr>\n<tr>\n<td>3<\/td>\n<td>What type of spatial dependence is suggested?<\/td>\n<td><code dir=\"ltr\">lm.LMtests()<\/code> \/ <code dir=\"ltr\">lm.RStests()<\/code><\/td>\n<\/tr>\n<tr>\n<td>4<\/td>\n<td>Is dependence associated with neighboring outcomes?<\/td>\n<td><code dir=\"ltr\">lagsarlm()<\/code><\/td>\n<\/tr>\n<tr>\n<td>5<\/td>\n<td>Is dependence primarily in unobserved errors?<\/td>\n<td><code dir=\"ltr\">errorsarlm()<\/code><\/td>\n<\/tr>\n<tr>\n<td>6<\/td>\n<td>Which model provides better fit?<\/td>\n<td><code dir=\"ltr\">AIC()<\/code>, <code dir=\"ltr\">logLik()<\/code><\/td>\n<\/tr>\n<tr>\n<td>7<\/td>\n<td>What are the SAR spillover effects?<\/td>\n<td><code dir=\"ltr\">impacts()<\/code><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<\/div>\n<p>This distinction is important because <strong>spatial autocorrelation is a property of the data-generating process, not merely a nuisance to eliminate<\/strong>.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"17_Common_Problems_and_Troubleshooting\"><\/span>17. Common Problems and Troubleshooting<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<h2><span class=\"ez-toc-section\" id=\"Problem_1_Invalid_geometries\"><\/span>Problem 1: Invalid geometries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Spatial neighbor construction can fail or behave unexpectedly when polygons contain invalid geometries.<\/p>\n<p>Check them with:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Check geometry validity\r\nsum(!st_is_valid(counties))<\/code><\/pre>\n<\/div>\n<p>Repair invalid geometries:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Repair invalid geometries\r\ncounties &lt;- st_make_valid(counties)<\/code><\/pre>\n<\/div>\n<p>Then recreate the neighbor structure.<\/p>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"Problem_2_Islands_with_no_neighbors\"><\/span>Problem 2: Islands with no neighbors<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Some polygon datasets contain isolated geographic units.<\/p>\n<p>Check:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Identify polygons without neighbors\r\nwhich(card(queen_nb) == 0)<\/code><\/pre>\n<\/div>\n<p>If an observation has no neighbors, <code dir=\"ltr\">nb2listw()<\/code> may require special handling.<\/p>\n<p>For example:<\/p>\n<div>\n<pre dir=\"ltr\"><code>queen_listw &lt;- nb2listw(\r\n  queen_nb,\r\n  style = \"W\",\r\n  zero.policy = TRUE\r\n)<\/code><\/pre>\n<\/div>\n<p>Use this deliberately rather than automatically. A zero-neighbor observation may indicate a genuine geographic characteristic or a problem with the dataset.<\/p>\n<hr \/>\n<h2><span class=\"ez-toc-section\" id=\"Problem_3_Coordinate_reference_system\"><\/span>Problem 3: Coordinate reference system<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>For polygon contiguity, the geographic CRS itself is not necessarily a problem because contiguity is based on polygon topology.<\/p>\n<p>However, if you later perform distance-based analysis, projections become much more important.<\/p>\n<p>Inspect your CRS:<\/p>\n<div>\n<pre dir=\"ltr\"><code>st_crs(counties)<\/code><\/pre>\n<\/div>\n<p>For distance-based spatial weights, consider an appropriate projected CRS.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"18_Queen_vs_Rook_Which_Should_You_Use\"><\/span>18. Queen vs Rook: Which Should You Use?<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>There is no universal winner.<\/p>\n<p>Use <strong>Queen contiguity<\/strong> when touching at a corner represents meaningful interaction.<\/p>\n<p>Use <strong>Rook contiguity<\/strong> when sharing a boundary is a more defensible definition of spatial interaction.<\/p>\n<p>You can even test the sensitivity of your conclusions to the choice of weights:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Queen weights\r\nqueen_nb &lt;- poly2nb(counties, queen = TRUE)\r\n\r\nqueen_listw &lt;- nb2listw(\r\n  queen_nb,\r\n  style = \"W\"\r\n)\r\n\r\n# Rook weights\r\nrook_nb &lt;- poly2nb(counties, queen = FALSE)\r\n\r\nrook_listw &lt;- nb2listw(\r\n  rook_nb,\r\n  style = \"W\"\r\n)<\/code><\/pre>\n<\/div>\n<p>Then estimate the same model using both:<\/p>\n<div>\n<pre dir=\"ltr\"><code># Queen-based SAR\r\nsar_queen &lt;- lagsarlm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties,\r\n  listw = queen_listw\r\n)\r\n\r\n# Rook-based SAR\r\nsar_rook &lt;- lagsarlm(\r\n  income ~ unemployment +\r\n    education +\r\n    population_density,\r\n  data = counties,\r\n  listw = rook_listw\r\n)<\/code><\/pre>\n<\/div>\n<p>If substantive conclusions change dramatically depending on the weights matrix, that sensitivity should be discussed rather than hidden.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"19_What_You_Should_Report_in_an_Applied_Study\"><\/span>19. What You Should Report in an Applied Study<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>For a thesis, research paper, or GIS analysis, reporting only the regression coefficients is usually insufficient.<\/p>\n<p>Document:<\/p>\n<ol start=\"1\">\n<li><strong>Spatial units used<\/strong><\/li>\n<li><strong>Dependent and independent variables<\/strong><\/li>\n<li><strong>OLS specification<\/strong><\/li>\n<li><strong>Spatial weights definition<\/strong><\/li>\n<li><strong>Queen or Rook contiguity<\/strong><\/li>\n<li><strong>Weight standardization method<\/strong><\/li>\n<li><strong>Moran&#8217;s I results<\/strong><\/li>\n<li><strong>LM diagnostic results<\/strong><\/li>\n<li><strong>SAR\/SEM parameter estimates<\/strong><\/li>\n<li><strong>AIC and log-likelihood<\/strong><\/li>\n<li><strong>Residual diagnostics<\/strong><\/li>\n<li><strong>Direct, indirect, and total impacts for SAR models where appropriate<\/strong><\/li>\n<\/ol>\n<p>For example, your methodology might state that polygon neighbors were defined using Queen contiguity and converted to row-standardized weights before testing OLS residuals for spatial autocorrelation and estimating alternative spatial regression specifications.<\/p>\n<hr \/>\n<h1><span class=\"ez-toc-section\" id=\"Summary\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h1>\n<p>A conventional OLS regression assumes that the unexplained component of observations is appropriately modeled without spatial dependence. Geographic data frequently violate this assumption because neighboring areas can influence one another or share unobserved characteristics.<\/p>\n<p>The <code dir=\"ltr\">spdep<\/code> and <code dir=\"ltr\">spatialreg<\/code> ecosystem provides a practical workflow for addressing this problem.<\/p>\n<p>The core process is:<\/p>\n<div>\n<pre dir=\"ltr\"><code>Spatial polygons\r\n      \u2193\r\n      sf\r\n      \u2193\r\nBaseline OLS\r\n      \u2193\r\npoly2nb()\r\n      \u2193\r\nnb2listw()\r\n      \u2193\r\nMoran's I\r\n      \u2193\r\nLM diagnostics\r\n      \u2193\r\n \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510\r\n \u2502               \u2502\r\nSAR             SEM\r\n \u2502               \u2502\r\nlagsarlm()      errorsarlm()\r\n \u2502               \u2502\r\n \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518\r\n         \u2193\r\n AIC + Log-Likelihood\r\n         \u2193\r\nModel interpretation<\/code><\/pre>\n<\/div>\n<p>The key lesson is that <strong>spatial regression is not simply OLS with a different function<\/strong>. You first need to establish how observations are connected geographically, diagnose whether spatial dependence exists, identify the likely form of that dependence, and then interpret the resulting spatial parameters appropriately.<\/p>\n<p>For a <strong>Spatial Lag Model<\/strong>, focus on <span data-math-source=\"\\rho\" data-math-display=\"false\">\\(\\rho\\)<\/span> and the resulting direct, indirect, and total impacts. For a <strong>Spatial Error Model<\/strong>, focus on <span data-math-source=\"\\lambda\" data-math-display=\"false\">\\(\\lambda\\)<\/span> and the evidence that spatial dependence remains in the disturbance process.<\/p>\n<p>Ultimately, a good spatial regression analysis combines <strong>statistical diagnostics, geographic reasoning, and model comparison<\/strong> rather than selecting a model solely because it produces a lower AIC.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spatial Regression with R: Fitting Spatial Lag and Spatial Error Models in spdep Introduction When performing spatial regression with R spdep, Ordinary Least Squares (OLS) is often the natural starting point for regression analysis. Ordinary Least Squares (OLS) is often the natural starting point for regression analysis. In a conventional linear model, we assume that &#8230; <a title=\"Spatial Regression with R: Fitting Spatial Lag and Spatial Error Models in spdep\" class=\"read-more\" href=\"https:\/\/helpwithmygisassignment.com\/blog\/spatial-regression-with-r-spdep\/\" aria-label=\"Read more about Spatial Regression with R: Fitting Spatial Lag and Spatial Error Models in spdep\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1475,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4],"tags":[],"class_list":["post-1369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized","category-gis-articles"],"_links":{"self":[{"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/posts\/1369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/comments?post=1369"}],"version-history":[{"count":5,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/posts\/1369\/revisions"}],"predecessor-version":[{"id":1477,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/posts\/1369\/revisions\/1477"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/media\/1475"}],"wp:attachment":[{"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/media?parent=1369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/categories?post=1369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/helpwithmygisassignment.com\/blog\/wp-json\/wp\/v2\/tags?post=1369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}