Read in data
boston = readOGR("boston.shp")
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/jessieshapiro/Documents/Documents/Education/University of Utah/GEOG6000/Labs/lab13/boston.shp", layer: "boston"
## with 506 features
## It has 21 fields
## Integer64 fields read as strings:  TOWNNO TRACT RAD TAX
boston$logCMEDV = log10(boston$CMEDV)
Map Boston house prices
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(boston) +
  tm_fill(col ="logCMEDV", palette = "-magma", main = "Boston Housing Prices") 
Basic linear regression

For the basic linear regression, crime rate, average number of rooms, and Boston employment centers were chosen against housing prices.

bos.lm = lm(logCMEDV ~ CRIM+RM+DIS, data=boston)
summary(bos.lm)
## 
## Call:
## lm(formula = logCMEDV ~ CRIM + RM + DIS, data = boston)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.43809 -0.04990  0.00440  0.06035  0.58582 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.475523   0.048737   9.757  < 2e-16 ***
## CRIM        -0.007826   0.000662 -11.821  < 2e-16 ***
## RM           0.133990   0.007661  17.490  < 2e-16 ***
## DIS          0.007529   0.002696   2.793  0.00542 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1169 on 502 degrees of freedom
## Multiple R-squared:  0.5677, Adjusted R-squared:  0.5651 
## F-statistic: 219.7 on 3 and 502 DF,  p-value: < 2.2e-16
Test spatial autocorrelation in the residuals
boscoords = coordinates(boston)
bos.knn = knearneigh(boscoords, k=4)
bos.nb = knn2nb(bos.knn)
plot(bos.nb, boscoords)

bos.listw = nb2listw(bos.nb)
Moran’s test
moran.test(residuals(bos.lm), bos.listw)
## 
##  Moran I test under randomisation
## 
## data:  residuals(bos.lm)  
## weights: bos.listw    
## 
## Moran I statistic standard deviate = 19.865, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##      0.5749040636     -0.0019801980      0.0008433165
moran.mc(residuals(bos.lm), listw=bos.listw, nsim=999)
## 
##  Monte-Carlo simulation of Moran I
## 
## data:  residuals(bos.lm) 
## weights: bos.listw  
## number of simulations + 1: 1000 
## 
## statistic = 0.5749, observed rank = 1000, p-value = 0.001
## alternative hypothesis: greater

From Moran’s I statistic of 0.5749 and p-value of 2.2e-16, there is significant autocorrelation in the residuals of this model.

Spatial regression model
summary(lm.LMtests(bos.lm, bos.listw, test=c("LMerr","LMlag")))
##  Lagrange multiplier diagnostics for spatial dependence
## data:  
## model: lm(formula = logCMEDV ~ CRIM + RM + DIS, data = boston)
## weights: bos.listw
##  
##       statistic parameter   p.value    
## LMerr    384.87         1 < 2.2e-16 ***
## LMlag    368.87         1 < 2.2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(lm.LMtests(bos.lm, bos.listw, test=c("RLMerr","RLMlag")))
##  Lagrange multiplier diagnostics for spatial dependence
## data:  
## model: lm(formula = logCMEDV ~ CRIM + RM + DIS, data = boston)
## weights: bos.listw
##  
##        statistic parameter   p.value    
## RLMerr    55.154         1 1.115e-13 ***
## RLMlag    39.152         1 3.920e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
bos.sf = SpatialFiltering(bos.lm, nb = bos.nb, style = "C",alpha = 0.25, 
                          ExactEV = FALSE, data=boston)
bos.sf
##    Step SelEvec      Eval         MinMi    ZMinMi       Pr(ZI)        R2
## 0     0       0 0.0000000  0.5749040636 20.017256 3.895945e-89 0.5676826
## 1     1      11 1.0205129  0.5346936825 18.757007 1.696894e-78 0.6034648
## 2     2      14 0.9928590  0.5000211025 17.675955 6.424370e-70 0.6313622
## 3     3      13 1.0033869  0.4677681883 16.676651 1.937834e-62 0.6535601
## 4     4       7 1.0392995  0.4418124330 15.900666 6.269464e-57 0.6686100
## 5     5       9 1.0266850  0.4143362516 15.064555 2.770243e-51 0.6834795
## 6     6       5 1.0584546  0.3883026553 14.281828 2.840498e-46 0.6957755
## 7     7      12 1.0107744  0.3698522377 13.751751 4.971139e-43 0.7045333
## 8     8      84 0.5415220  0.3531625987 13.197556 9.062758e-40 0.7307132
## 9     9      58 0.6827058  0.3357418624 12.639939 1.271522e-36 0.7442338
## 10   10       6 1.0473615  0.3194770526 12.189763 3.524467e-34 0.7499490
## 11   11      33 0.8550902  0.3055981404 11.785080 4.659800e-32 0.7562647
## 12   12       4 1.0655597  0.2911311893 11.398787 4.239849e-30 0.7608179
## 13   13      50 0.7336844  0.2760824047 10.929390 8.340709e-28 0.7686837
## 14   14       8 1.0362027  0.2615373828 10.530866 6.225880e-26 0.7730269
## 15   15       1 1.0827534  0.2487786134 10.203571 1.911143e-24 0.7764993
## 16   16      64 0.6506355  0.2378040124  9.864453 5.935728e-23 0.7824407
## 17   17       3 1.0720486  0.2278183874  9.632514 5.828568e-22 0.7850141
## 18   18      52 0.7143072  0.2174991287  9.325682 1.102720e-20 0.7894796
## 19   19      45 0.7675871  0.2068856344  9.016212 1.947056e-19 0.7934645
## 20   20      23 0.9223578  0.1961895720  8.728575 2.579007e-18 0.7965066
## 21   21      39 0.8262096  0.1859834901  8.441963 3.120524e-17 0.7997506
## 22   22      72 0.6005394  0.1759384027  8.127314 4.389063e-16 0.8044880
## 23   23      29 0.8793717  0.1662698486  7.867813 3.608948e-15 0.8071389
## 24   24      61 0.6644406  0.1570832978  7.592995 3.125957e-14 0.8106310
## 25   25      25 0.9036433  0.1477163980  7.347174 2.024406e-13 0.8129775
## 26   26      59 0.6737397  0.1392155266  7.098228 1.263667e-12 0.8159518
## 27   27      18 0.9655385  0.1308542044  6.898060 5.271743e-12 0.8177955
## 28   28      56 0.6988219  0.1227871683  6.667899 2.594905e-11 0.8203472
## 29   29       2 1.0758956  0.1145773655  6.490163 8.574380e-11 0.8218814
## 30   30      60 0.6729859  0.1065689701  6.257634 3.908620e-10 0.8243998
## 31   31      70 0.6151645  0.0991390447  6.038881 1.551865e-09 0.8269281
## 32   32      62 0.6563519  0.0915248468  5.818672 5.931711e-09 0.8292613
## 33   33     119 0.3538594  0.0843430007  5.578281 2.429069e-08 0.8338110
## 34   34      15 0.9799485  0.0778626884  5.446094 5.148795e-08 0.8350048
## 35   35      44 0.7833107  0.0713426604  5.283411 1.268006e-07 0.8365158
## 36   36      40 0.8135684  0.0649083280  5.127896 2.929985e-07 0.8379209
## 37   37      81 0.5606683  0.0584003456  4.936309 7.961513e-07 0.8400209
## 38   38      90 0.5039176  0.0525465388  4.762753 1.909693e-06 0.8420957
## 39   39      65 0.6416532  0.0470056372  4.617890 3.876621e-06 0.8435670
## 40   40      89 0.5131401  0.0415870156  4.462187 8.112733e-06 0.8453646
## 41   41     124 0.3219451  0.0363228645  4.291480 1.774865e-05 0.8482146
## 42   42     102 0.4380054  0.0310091113  4.131857 3.598444e-05 0.8501963
## 43   43     101 0.4470471  0.0256380883  3.971416 7.144657e-05 0.8521056
## 44   44      46 0.7575672  0.0203063568  3.849316 1.184481e-04 0.8531752
## 45   45      88 0.5204514  0.0149301759  3.697135 2.180460e-04 0.8547367
## 46   46      98 0.4607320  0.0099351253  3.552935 3.809580e-04 0.8563462
## 47   47      77 0.5782490  0.0054914852  3.442947 5.754111e-04 0.8574608
## 48   48      73 0.5945932  0.0009809377  3.332447 8.608600e-04 0.8585438
## 49   49      47 0.7526706 -0.0035646092  3.239392 1.197846e-03 0.8593941
## 50   50      22 0.9388001 -0.0081101407  3.169793 1.525476e-03 0.8600691
## 51   51     123 0.3355831 -0.0125783781  3.032737 2.423467e-03 0.8618649
## 52   52      49 0.7437649 -0.0170903723  2.939801 3.284233e-03 0.8626841
## 53   53      32 0.8581019 -0.0215661134  2.862236 4.206641e-03 0.8633827
## 54   54      82 0.5572487 -0.0260976236  2.746797 6.018032e-03 0.8644440
## 55   55      35 0.8461651 -0.0305487231  2.668517 7.618684e-03 0.8651322
## 56   56     127 0.3082003 -0.0350628220  2.527177 1.149835e-02 0.8669058
## 57   57     133 0.2824200 -0.0392840920  2.394959 1.662223e-02 0.8686522
## 58   58      43 0.7875121 -0.0435104341  2.317902 2.045463e-02 0.8693202
## 59   59     138 0.2650972 -0.0478016124  2.181620 2.913756e-02 0.8711124
## 60   60      54 0.7065054 -0.0519687441  2.097379 3.596000e-02 0.8718205
## 61   61     143 0.2455568 -0.0561393140  1.964313 4.949383e-02 0.8735924
## 62   62      57 0.6911548 -0.0603272800  1.877538 6.044441e-02 0.8742969
## 63   63     132 0.2841528 -0.0645789593  1.745638 8.087389e-02 0.8758294
## 64   64      42 0.7939878 -0.0688823151  1.665763 9.576070e-02 0.8764487
## 65   65     131 0.2951878 -0.0732279407  1.531600 1.256212e-01 0.8779061
## 66   66      19 0.9563929 -0.0772416776  1.481006 1.386048e-01 0.8783802
## 67   67     122 0.3396238 -0.0812310570  1.365094 1.722234e-01 0.8795330
## 68   68     104 0.4263612 -0.0851818250  1.259714 2.077724e-01 0.8804634
## 69   69      41 0.7979380 -0.0890986686  1.194681 2.322117e-01 0.8809912
## 70   70     153 0.1954427 -0.0930059321  1.068389 2.853453e-01 0.8826033
##          gamma
## 0   0.00000000
## 1   0.75372958
## 2   0.66552505
## 3  -0.59366170
## 4   0.48882002
## 5  -0.48588217
## 6   0.44183876
## 7   0.37288941
## 8   0.64471316
## 9  -0.46332005
## 10 -0.30122935
## 11  0.31666041
## 12 -0.26886825
## 13 -0.35338945
## 14 -0.26259458
## 15  0.23479977
## 16  0.30713543
## 17  0.20212892
## 18  0.26626707
## 19 -0.25153185
## 20  0.21977243
## 21 -0.22694491
## 22  0.27425492
## 23 -0.20515124
## 24  0.23546378
## 25 -0.19301675
## 26 -0.21730889
## 27  0.17109000
## 28 -0.20127726
## 29  0.15607463
## 30  0.19995937
## 31  0.20035569
## 32 -0.19246424
## 33  0.26876576
## 34  0.13767583
## 35 -0.15488615
## 36  0.14935845
## 37  0.18260031
## 38 -0.18149571
## 39 -0.15284099
## 40 -0.16893737
## 41 -0.21271843
## 42 -0.17737930
## 43 -0.17410863
## 44  0.13031123
## 45 -0.15745246
## 46  0.15986008
## 47  0.13302247
## 48  0.13113328
## 49 -0.11618714
## 50  0.10351950
## 51  0.16885624
## 52  0.11404276
## 53  0.10532125
## 54  0.12980563
## 55 -0.10453136
## 56  0.16780641
## 57  0.16651574
## 58  0.10298401
## 59  0.16868366
## 60  0.10603184
## 61 -0.16772763
## 62 -0.10575753
## 63 -0.15598757
## 64 -0.09915685
## 65 -0.15211189
## 66 -0.08676019
## 67 -0.13529172
## 68  0.12153915
## 69  0.09154407
## 70  0.15998308

The non-robust and robust version of the multiplier test with spatial error and lag models indicated significant autocorrelation. The spatial filtering model will be used on this dataset.

Adding a spatial filter
E.sel <- fitted(bos.sf)
lm.sf <- lm(logCMEDV ~ CRIM+RM+DIS+E.sel, data=boston)
summary(bos.lm)
## 
## Call:
## lm(formula = logCMEDV ~ CRIM + RM + DIS, data = boston)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.43809 -0.04990  0.00440  0.06035  0.58582 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.475523   0.048737   9.757  < 2e-16 ***
## CRIM        -0.007826   0.000662 -11.821  < 2e-16 ***
## RM           0.133990   0.007661  17.490  < 2e-16 ***
## DIS          0.007529   0.002696   2.793  0.00542 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.1169 on 502 degrees of freedom
## Multiple R-squared:  0.5677, Adjusted R-squared:  0.5651 
## F-statistic: 219.7 on 3 and 502 DF,  p-value: < 2.2e-16
summary(lm.sf)
## 
## Call:
## lm(formula = logCMEDV ~ CRIM + RM + DIS + E.sel, data = boston)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.33617 -0.02975  0.00077  0.03556  0.45634 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.4755229  0.0273775  17.369  < 2e-16 ***
## CRIM        -0.0078260  0.0003719 -21.044  < 2e-16 ***
## RM           0.1339896  0.0043033  31.136  < 2e-16 ***
## DIS          0.0075290  0.0015144   4.972 9.59e-07 ***
## E.selvec11   0.7537296  0.0656854  11.475  < 2e-16 ***
## E.selvec14   0.6655250  0.0656854  10.132  < 2e-16 ***
## E.selvec13  -0.5936617  0.0656854  -9.038  < 2e-16 ***
## E.selvec7    0.4888200  0.0656854   7.442 5.41e-13 ***
## E.selvec9   -0.4858822  0.0656854  -7.397 7.31e-13 ***
## E.selvec5    0.4418388  0.0656854   6.727 5.53e-11 ***
## E.selvec12   0.3728894  0.0656854   5.677 2.52e-08 ***
## E.selvec84   0.6447132  0.0656854   9.815  < 2e-16 ***
## E.selvec58  -0.4633201  0.0656854  -7.054 6.97e-12 ***
## E.selvec6   -0.3012294  0.0656854  -4.586 5.93e-06 ***
## E.selvec33   0.3166604  0.0656854   4.821 1.98e-06 ***
## E.selvec4   -0.2688682  0.0656854  -4.093 5.08e-05 ***
## E.selvec50  -0.3533895  0.0656854  -5.380 1.22e-07 ***
## E.selvec8   -0.2625946  0.0656854  -3.998 7.52e-05 ***
## E.selvec1    0.2347998  0.0656854   3.575 0.000390 ***
## E.selvec64   0.3071354  0.0656854   4.676 3.92e-06 ***
## E.selvec3    0.2021289  0.0656854   3.077 0.002222 ** 
## E.selvec52   0.2662671  0.0656854   4.054 5.98e-05 ***
## E.selvec45  -0.2515318  0.0656854  -3.829 0.000147 ***
## E.selvec23   0.2197724  0.0656854   3.346 0.000892 ***
## E.selvec39  -0.2269449  0.0656854  -3.455 0.000605 ***
## E.selvec72   0.2742549  0.0656854   4.175 3.60e-05 ***
## E.selvec29  -0.2051512  0.0656854  -3.123 0.001909 ** 
## E.selvec61   0.2354638  0.0656854   3.585 0.000376 ***
## E.selvec25  -0.1930168  0.0656854  -2.939 0.003475 ** 
## E.selvec59  -0.2173089  0.0656854  -3.308 0.001017 ** 
## E.selvec18   0.1710900  0.0656854   2.605 0.009513 ** 
## E.selvec56  -0.2012773  0.0656854  -3.064 0.002319 ** 
## E.selvec2    0.1560746  0.0656854   2.376 0.017932 *  
## E.selvec60   0.1999594  0.0656854   3.044 0.002476 ** 
## E.selvec70   0.2003557  0.0656854   3.050 0.002427 ** 
## E.selvec62  -0.1924642  0.0656854  -2.930 0.003568 ** 
## E.selvec119  0.2687658  0.0656854   4.092 5.11e-05 ***
## E.selvec15   0.1376758  0.0656854   2.096 0.036664 *  
## E.selvec44  -0.1548862  0.0656854  -2.358 0.018818 *  
## E.selvec40   0.1493585  0.0656854   2.274 0.023465 *  
## E.selvec81   0.1826003  0.0656854   2.780 0.005675 ** 
## E.selvec90  -0.1814957  0.0656854  -2.763 0.005970 ** 
## E.selvec65  -0.1528410  0.0656854  -2.327 0.020434 *  
## E.selvec89  -0.1689374  0.0656854  -2.572 0.010447 *  
## E.selvec124 -0.2127184  0.0656854  -3.238 0.001295 ** 
## E.selvec102 -0.1773793  0.0656854  -2.700 0.007197 ** 
## E.selvec101 -0.1741086  0.0656854  -2.651 0.008329 ** 
## E.selvec46   0.1303112  0.0656854   1.984 0.047903 *  
## E.selvec88  -0.1574525  0.0656854  -2.397 0.016951 *  
## E.selvec98   0.1598601  0.0656854   2.434 0.015349 *  
## E.selvec77   0.1330225  0.0656854   2.025 0.043467 *  
## E.selvec73   0.1311333  0.0656854   1.996 0.046519 *  
## E.selvec47  -0.1161871  0.0656854  -1.769 0.077625 .  
## E.selvec22   0.1035195  0.0656854   1.576 0.115760    
## E.selvec123  0.1688562  0.0656854   2.571 0.010484 *  
## E.selvec49   0.1140428  0.0656854   1.736 0.083242 .  
## E.selvec32   0.1053213  0.0656854   1.603 0.109573    
## E.selvec82   0.1298056  0.0656854   1.976 0.048771 *  
## E.selvec35  -0.1045314  0.0656854  -1.591 0.112252    
## E.selvec127  0.1678064  0.0656854   2.555 0.010970 *  
## E.selvec133  0.1665157  0.0656854   2.535 0.011595 *  
## E.selvec43   0.1029840  0.0656854   1.568 0.117651    
## E.selvec138  0.1686837  0.0656854   2.568 0.010562 *  
## E.selvec54   0.1060318  0.0656854   1.614 0.107206    
## E.selvec143 -0.1677276  0.0656854  -2.554 0.011007 *  
## E.selvec57  -0.1057575  0.0656854  -1.610 0.108115    
## E.selvec132 -0.1559876  0.0656854  -2.375 0.017996 *  
## E.selvec42  -0.0991569  0.0656854  -1.510 0.131883    
## E.selvec131 -0.1521119  0.0656854  -2.316 0.021039 *  
## E.selvec19  -0.0867602  0.0656854  -1.321 0.187252    
## E.selvec122 -0.1352917  0.0656854  -2.060 0.040026 *  
## E.selvec104  0.1215391  0.0656854   1.850 0.064950 .  
## E.selvec41   0.0915441  0.0656854   1.394 0.164132    
## E.selvec153  0.1599831  0.0656854   2.436 0.015271 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.06569 on 432 degrees of freedom
## Multiple R-squared:  0.8826, Adjusted R-squared:  0.8628 
## F-statistic: 44.49 on 73 and 432 DF,  p-value: < 2.2e-16
Testing residuals for remaining spatial autocorrelation
moran.test(residuals(lm.sf), bos.listw)
## 
##  Moran I test under randomisation
## 
## data:  residuals(lm.sf)  
## weights: bos.listw    
## 
## Moran I statistic standard deviate = -3.1488, p-value = 0.9992
## alternative hypothesis: greater
## sample estimates:
## Moran I statistic       Expectation          Variance 
##     -0.0930059321     -0.0019801980      0.0008356692

The Moran’s I statistic of -0.093 and p-value of 0.9975 show that the spatial filtering approached removed spatial autocorrelation effects on the residuals of the model. Thus, there is no evidence to reject the null hypothesis that there is no spatial autocorrelation.

Summary

This model is a good fit and adequately explains variation in house prices, evidenced by the R-squared value of 0.8826, F-statistic of 44.49 and p-value of 2.2e-16. The model shows that houses are more expensive in areas with low crime rate, houses with more rooms and further distances from city centers.