#first plot our data

ggplot()+
  geom_sf(data = chicago, aes(fill=as.numeric(poverty)))+
  scale_fill_steps(
    name = "poverty",
    low = "white",
    high = "maroon",
    n.breaks = 5,
    show.limits = T)+
  theme_void()

## 
## Call:
## lm(formula = poverty ~ unemployed + without_hs, data = model)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.9658  -5.0431   0.1463   4.2113  15.9993 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  3.30152    2.02635   1.629   0.1082    
## unemployed   1.03616    0.12222   8.478 5.23e-12 ***
## without_hs   0.13101    0.07166   1.828   0.0722 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 6.614 on 63 degrees of freedom
## Multiple R-squared:  0.6146, Adjusted R-squared:  0.6024 
## F-statistic: 50.24 on 2 and 63 DF,  p-value: 9.007e-14
## 
##  Global Moran I for regression residuals
## 
## data:  
## model: lm(formula = poverty ~ unemployed + without_hs, data = model)
## weights: chicago_list
## 
## Moran I statistic standard deviate = 5.4579, p-value = 2.409e-08
## alternative hypothesis: greater
## sample estimates:
## Observed Moran I      Expectation         Variance 
##      0.407123871     -0.034255539      0.006539907
#Lagrange multiplier test

lm.RStests(lm, chicago_list, test = "all")
## 
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## 
## data:  
## model: lm(formula = poverty ~ unemployed + without_hs, data = model)
## test weights: chicago_list
## 
## RSerr = 22.405, df = 1, p-value = 2.208e-06
## 
## 
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## 
## data:  
## model: lm(formula = poverty ~ unemployed + without_hs, data = model)
## test weights: chicago_list
## 
## RSlag = 10.447, df = 1, p-value = 0.001229
## 
## 
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## 
## data:  
## model: lm(formula = poverty ~ unemployed + without_hs, data = model)
## test weights: chicago_list
## 
## adjRSerr = 12.374, df = 1, p-value = 0.0004354
## 
## 
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## 
## data:  
## model: lm(formula = poverty ~ unemployed + without_hs, data = model)
## test weights: chicago_list
## 
## adjRSlag = 0.41501, df = 1, p-value = 0.5194
## 
## 
##  Rao's score (a.k.a Lagrange multiplier) diagnostics for spatial
##  dependence
## 
## data:  
## model: lm(formula = poverty ~ unemployed + without_hs, data = model)
## test weights: chicago_list
## 
## SARMA = 22.82, df = 2, p-value = 1.108e-05
# here we see that three out of the five lagrange multiplier tests (Rserr, RSlag, SARMA) are significant since their p-value's are <0.05. This is evidence of spatial dependence. Because the spatial error model is significant for both tests, we will use that. 

lm_error <- errorsarlm(poverty ~ unemployed + without_hs,
              data = model,
              listw = chicago_list,
              zero.policy = TRUE, 
              na.action = na.omit)

summary(lm_error)
## 
## Call:errorsarlm(formula = poverty ~ unemployed + without_hs, data = model, 
##     listw = chicago_list, na.action = na.omit, zero.policy = TRUE)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.3775  -3.5106  -1.0496   2.6220  12.4427 
## 
## Type: error 
## Coefficients: (asymptotic standard errors) 
##             Estimate Std. Error z value  Pr(>|z|)
## (Intercept) 2.595454   2.916467  0.8899    0.3735
## unemployed  0.827768   0.140147  5.9064 3.496e-09
## without_hs  0.335110   0.075589  4.4333 9.280e-06
## 
## Lambda: 0.71704, LR test value: 24.683, p-value: 6.7583e-07
## Asymptotic standard error: 0.090613
##     z-value: 7.9133, p-value: 2.4425e-15
## Wald statistic: 62.62, p-value: 2.5535e-15
## 
## Log likelihood: -204.461 for error model
## ML residual variance (sigma squared): 24.56, (sigma: 4.9558)
## Number of observations: 66 
## Number of parameters estimated: 5 
## AIC: 418.92, (AIC for lm: 441.6)
#Here we see the lamda value is .71704, which is statistically significant and suggests spatial autocorrelation in the residuals. 

#We can see from the model that unemployment and no highschool degree has a positive statistically significant impact on poverty