library(tigris)
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
library(GWmodel)
## Loading required package: maptools
## Loading required package: sp
## Checking rgeos availability: FALSE
## Please note that 'maptools' will be retired by the end of 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
## Loading required package: robustbase
## Loading required package: Rcpp
## Loading required package: spatialreg
## Loading required package: spData
## To access larger datasets in this package, install the spDataLarge
## package with: `install.packages('spDataLarge',
## repos='https://nowosad.github.io/drat/', type='source')`
## Loading required package: Matrix
## Welcome to GWmodel version 2.2-8.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x tidyr::expand() masks Matrix::expand()
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## x tidyr::pack()   masks Matrix::pack()
## x tidyr::unpack() masks Matrix::unpack()
library(sf)
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(tmap)
library(rgdal)
## Please note that rgdal will be retired by the end of 2023,
## plan transition to sf/stars/terra functions using GDAL and PROJ
## at your earliest convenience.
## 
## rgdal: version: 1.5-27, (SVN revision 1148)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.2.1, released 2020/12/29
## Path to GDAL shared files: D:/Documents/R/win-library/4.1/sf/gdal
## GDAL binary built with GEOS: TRUE 
## Loaded PROJ runtime: Rel. 7.2.1, January 1st, 2021, [PJ_VERSION: 721]
## Path to PROJ shared files: D:/Documents/R/win-library/4.1/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.4-5
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
## Overwritten PROJ_LIB was D:/Documents/R/win-library/4.1/rgdal/proj
library(dplyr)
library(shinyjs)
## 
## Attaching package: 'shinyjs'
## The following object is masked from 'package:Matrix':
## 
##     show
## The following object is masked from 'package:Rcpp':
## 
##     show
## The following object is masked from 'package:sp':
## 
##     show
## The following objects are masked from 'package:methods':
## 
##     removeClass, show
library(RColorBrewer)
options(tigris_use_cache=TRUE)
cnty <- counties(cb=TRUE, resolution="20m", year=2019) %>% mutate(GEOID = as.integer(GEOID))
UA <- urban_areas(cb=TRUE, year=2019) %>% mutate(UACE10 = as.integer(UACE10))

#Create Base Data
restaurantPts= read.csv('restaurantPts.csv')
colnames(restaurantPts)
##  [1] "fn"            "gn"            "old_pct"       "bachelor_pct" 
##  [5] "clinton_pct"   "median_income" "pop_density"   "lon"          
##  [9] "lat"           "TRACT_GEOID"   "County"        "MSA_NAME"     
## [13] "UA_NAME"
print(length(unique(restaurantPts$UA_NAME)))
## [1] 3486
print(length(unique(restaurantPts$MSA_NAME)))
## [1] 919
print(length(unique(restaurantPts$County)))
## [1] 3091
#Summary Statistics for Counties
GWR_county = restaurantPts %>% 
  select(c(County, fn, gn, old_pct, bachelor_pct, clinton_pct, median_income, pop_density)) %>%
  mutate(median_income = median_income/10000, pop_density = pop_density/1000, gn = gn * 100) %>%
  group_by(County) %>% 
  summarise_all(list(mean = ~mean(.), n=~n()))

#Organize County data and remove counties with fewer than 10 restaurants.
GWR_county <- GWR_county[,-c(10:15)]
GWR_county <- filter(GWR_county, fn_n > 10)

#Join County data to shapefile
GWR_countyGeo = GWR_county %>%
  left_join(cnty %>% filter(GEOID %in% GWR_county$County) %>%
              select(c(GEOID, geometry)), by=c("County" = "GEOID"), copy=FALSE) %>% st_as_sf() %>% as_Spatial()

#Summary Statistics for UAs
GWR_UA = restaurantPts %>% 
  select(c(UA_NAME, fn, gn, old_pct, bachelor_pct, clinton_pct, median_income, pop_density)) %>%
  mutate(median_income = median_income/10000, pop_density = pop_density/1000, gn = gn * 100) %>%
  group_by(UA_NAME) %>% 
  summarise_all(list(mean = ~mean(.), n=~n())) 

#Organize UA data and remove UA with fewer than 10 restaurants.
GWR_UA <- GWR_UA[,-c(10:15)]
GWR_UA <- filter(GWR_UA, fn_n > 10)

#Create table of Chainniest small town.
GWR_UA_st <- GWR_UA
colnames(GWR_UA_st)
## [1] "UA_NAME"            "fn_mean"            "gn_mean"           
## [4] "old_pct_mean"       "bachelor_pct_mean"  "clinton_pct_mean"  
## [7] "median_income_mean" "pop_density_mean"   "fn_n"
GWR_UA_st %>%
  arrange(desc(fn_mean),pop_density_mean) %>%
  select(c(UA_NAME, fn_mean, gn_mean, pop_density_mean)) %>%
  slice(1:5)
## # A tibble: 5 x 4
##   UA_NAME         fn_mean gn_mean pop_density_mean
##   <chr>             <dbl>   <dbl>            <dbl>
## 1 Thomasville, AL   5040.    68.4           0.069 
## 2 Scott City, MO    4830.    72.7           0.284 
## 3 Bells, TN         4647.    64.7           0.0865
## 4 Denton, MD        4640.    46.2           0.606 
## 5 Beebe, AR         4599.    76.5           0.208
OLSModel <- lm(formula = fn ~ old_pct + bachelor_pct + clinton_pct, data = restaurantPts)  
OLSModel
## 
## Call:
## lm(formula = fn ~ old_pct + bachelor_pct + clinton_pct, data = restaurantPts)
## 
## Coefficients:
##  (Intercept)       old_pct  bachelor_pct   clinton_pct  
##     2276.367        -3.093        -9.771       -15.901
summary(OLSModel)
## 
## Call:
## lm(formula = fn ~ old_pct + bachelor_pct + clinton_pct, data = restaurantPts)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2105.9 -1404.0 -1095.4  -671.8 21338.7 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2276.3671    16.8504 135.093  < 2e-16 ***
## old_pct        -3.0934     0.5784  -5.348 8.91e-08 ***
## bachelor_pct   -9.7705     0.3155 -30.971  < 2e-16 ***
## clinton_pct   -15.9013     0.2789 -57.013  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3831 on 793318 degrees of freedom
## Multiple R-squared:  0.009048,   Adjusted R-squared:  0.009045 
## F-statistic:  2415 on 3 and 793318 DF,  p-value: < 2.2e-16
print("Returned coefficients.")
## [1] "Returned coefficients."
#Change shapefile to a meter based projection
GWR_countyGeo <- spTransform(GWR_countyGeo, CRS("+proj=lcc +lon_0=-90 +lat_1=33 +lat_2=45"))

#Bandwidth for adaptive kernel (nearest neighbors) 
myBandwidthNeighbors_county <- bw.gwr(fn_mean ~ old_pct_mean + bachelor_pct_mean + clinton_pct_mean,
            adaptive = T,
            data = GWR_countyGeo)
## Take a cup of tea and have a break, it will take a few minutes.
##           -----A kind suggestion from GWmodel development group
## Adaptive bandwidth: 1778 CV score: 937594260 
## Adaptive bandwidth: 1107 CV score: 916219188 
## Adaptive bandwidth: 691 CV score: 892861737 
## Adaptive bandwidth: 435 CV score: 873899444 
## Adaptive bandwidth: 275 CV score: 862814121 
## Adaptive bandwidth: 178 CV score: 863593215 
## Adaptive bandwidth: 337 CV score: 866148677 
## Adaptive bandwidth: 239 CV score: 861989901 
## Adaptive bandwidth: 214 CV score: 862115357 
## Adaptive bandwidth: 251 CV score: 862185663 
## Adaptive bandwidth: 227 CV score: 862003375 
## Adaptive bandwidth: 241 CV score: 862041658 
## Adaptive bandwidth: 232 CV score: 861967351 
## Adaptive bandwidth: 233 CV score: 862042394 
## Adaptive bandwidth: 236 CV score: 861992148 
## Adaptive bandwidth: 233 CV score: 862042394 
## Adaptive bandwidth: 234 CV score: 861991447 
## Adaptive bandwidth: 233 CV score: 862042394 
## Adaptive bandwidth: 233 CV score: 862042394 
## Adaptive bandwidth: 232 CV score: 861967351
#Bandwidth for fixed kernel (distance)
myBandwidthDistance_county <- bw.gwr(fn_mean ~ old_pct_mean + bachelor_pct_mean + clinton_pct_mean,
            adaptive = F,
            data = GWR_countyGeo)
## Take a cup of tea and have a break, it will take a few minutes.
##           -----A kind suggestion from GWmodel development group
## Fixed bandwidth: 2822927 CV score: 989790993 
## Fixed bandwidth: 1745014 CV score: 943220953 
## Fixed bandwidth: 1078827 CV score: 909492711 
## Fixed bandwidth: 667100.5 CV score: 882730896 
## Fixed bandwidth: 412639.7 CV score: 871354444 
## Fixed bandwidth: 255374.3 CV score: 937868165 
## Fixed bandwidth: 509835.1 CV score: 874570875 
## Fixed bandwidth: 352569.6 CV score: 876062519 
## Fixed bandwidth: 449765 CV score: 872209641 
## Fixed bandwidth: 389695 CV score: 871788558 
## Fixed bandwidth: 426820.3 CV score: 871533169 
## Fixed bandwidth: 403875.6 CV score: 871405725 
## Fixed bandwidth: 418056.2 CV score: 871389409 
## Fixed bandwidth: 409292.1 CV score: 871357822 
## Fixed bandwidth: 414708.6 CV score: 871361994 
## Fixed bandwidth: 411361 CV score: 871353364 
## Fixed bandwidth: 410570.8 CV score: 871354132 
## Fixed bandwidth: 411849.4 CV score: 871353447 
## Fixed bandwidth: 411059.2 CV score: 871353525 
## Fixed bandwidth: 411547.6 CV score: 871353346 
## Fixed bandwidth: 411662.9 CV score: 871353365 
## Fixed bandwidth: 411476.3 CV score: 871353346 
## Fixed bandwidth: 411432.3 CV score: 871353350 
## Fixed bandwidth: 411503.6 CV score: 871353345 
## Fixed bandwidth: 411520.4 CV score: 871353345 
## Fixed bandwidth: 411493.2 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411513.9 CV score: 871353345 
## Fixed bandwidth: 411507.5 CV score: 871353345 
## Fixed bandwidth: 411511.5 CV score: 871353345 
## Fixed bandwidth: 411509 CV score: 871353345 
## Fixed bandwidth: 411510.6 CV score: 871353345 
## Fixed bandwidth: 411509.6 CV score: 871353345 
## Fixed bandwidth: 411510.2 CV score: 871353345 
## Fixed bandwidth: 411509.8 CV score: 871353345 
## Fixed bandwidth: 411510.1 CV score: 871353345 
## Fixed bandwidth: 411509.9 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345 
## Fixed bandwidth: 411510 CV score: 871353345
#---- Prepare data for on the County level ----#

## Retrieve a county level shapefile from the census. the resolution is 20 m, which means that it is not a big dataset, it is generalized. Make GEOID an integer, because we will be joining it to an integer field below.

options(tigris_use_cache=TRUE)
cnty <- counties(cb=TRUE, resolution="20m", year=2019)  %>% mutate(GEOID = as.integer(GEOID))

## Produce summary statistics on our data on the field "COUNTY" 
GWR_input = restaurantPts %>% 
  select(c(County, fn, gn, old_pct, bachelor_pct, clinton_pct, median_income, pop_density)) %>%
  mutate(median_income = median_income/10000, pop_density = pop_density/1000, gn = gn * 100) %>%
  group_by(County) %>% 
  summarise_all(list(mean = ~mean(.), n=~n())) 

## Delete those extra hanging columns---they all say the same thing and delete counties w/ fewer than 10 restaurants.
GWR_input <- GWR_input[,-c(10:15)]
GWR_input <- filter(GWR_input, fn_n > 10) 

## Join the data to the county shapefile!
GWR_inputGeo = GWR_input %>% 
  left_join(cnty %>% filter(GEOID %in% GWR_input$County) %>% 
              select(c(GEOID, geometry)), by=c('County' = 'GEOID'), copy=FALSE) %>% st_as_sf() %>% as_Spatial() 

##Let's take a look at colors (uncomment me!)
install.packages("shinyjs")
## Warning: package 'shinyjs' is in use and will not be installed
library(shinyjs)
tmaptools::palette_explorer() 
## Loading required namespace: shiny

Shiny applications not supported in static R Markdown documents

library(RColorBrewer)

## Make your map: You can ignore the warnings.
 
  
#------END CNTY -----------------#
######## Modified For Assignment Q 8 ##########

##Prints in the console!

########### We need to change the projection to a meter based projection. W are using lambert conformal conic

GWR_inputGeo <- spTransform(GWR_inputGeo, CRS("+proj=lcc +lon_0=-90 +lat_1=33 +lat_2=45")) 

########## Let's figure out our bandwidth: first let's use number of neighbors

myBandwidthKNeighbors <- bw.gwr(fn_mean ~ old_pct_mean + bachelor_pct_mean + clinton_pct_mean + pop_density_mean,
             adaptive = T,
             data=GWR_inputGeo) 
## Take a cup of tea and have a break, it will take a few minutes.
##           -----A kind suggestion from GWmodel development group
## Adaptive bandwidth: 1778 CV score: 927535707 
## Adaptive bandwidth: 1107 CV score: 901988561 
## Adaptive bandwidth: 691 CV score: 875659434 
## Adaptive bandwidth: 435 CV score: 857611318 
## Adaptive bandwidth: 275 CV score: 847961793 
## Adaptive bandwidth: 178 CV score: 851746244 
## Adaptive bandwidth: 337 CV score: 850008595 
## Adaptive bandwidth: 239 CV score: 848248465 
## Adaptive bandwidth: 300 CV score: 848306252 
## Adaptive bandwidth: 262 CV score: 848110863 
## Adaptive bandwidth: 285 CV score: 848124577 
## Adaptive bandwidth: 270 CV score: 848053080 
## Adaptive bandwidth: 279 CV score: 848021268 
## Adaptive bandwidth: 273 CV score: 848008471 
## Adaptive bandwidth: 276 CV score: 847973182 
## Adaptive bandwidth: 273 CV score: 848008471 
## Adaptive bandwidth: 274 CV score: 847965834 
## Adaptive bandwidth: 273 CV score: 848008471 
## Adaptive bandwidth: 273 CV score: 848008471 
## Adaptive bandwidth: 272 CV score: 848036537 
## Adaptive bandwidth: 272 CV score: 848036537 
## Adaptive bandwidth: 271 CV score: 848053189 
## Adaptive bandwidth: 271 CV score: 848053189 
## Adaptive bandwidth: 270 CV score: 848053080 
## Adaptive bandwidth: 270 CV score: 848053080 
## Adaptive bandwidth: 269 CV score: 848076693 
## Adaptive bandwidth: 269 CV score: 848076693 
## Adaptive bandwidth: 268 CV score: 848085575 
## Adaptive bandwidth: 268 CV score: 848085575 
## Adaptive bandwidth: 267 CV score: 848054197 
## Adaptive bandwidth: 267 CV score: 848054197 
## Adaptive bandwidth: 266 CV score: 848037813 
## Adaptive bandwidth: 266 CV score: 848037813 
## Adaptive bandwidth: 265 CV score: 848079069 
## Adaptive bandwidth: 265 CV score: 848079069 
## Adaptive bandwidth: 264 CV score: 848108494 
## Adaptive bandwidth: 264 CV score: 848108494
######### Let's figure out our bandwidth: second, let's use distance!

myBandwidthDistance <- bw.gwr(fn_mean ~ old_pct_mean + bachelor_pct_mean + clinton_pct_mean + pop_density_mean,
             adaptive = F,
             data=GWR_inputGeo) 
## Take a cup of tea and have a break, it will take a few minutes.
##           -----A kind suggestion from GWmodel development group
## Fixed bandwidth: 2822927 CV score: 986105758 
## Fixed bandwidth: 1745014 CV score: 936047750 
## Fixed bandwidth: 1078827 CV score: 897789065 
## Fixed bandwidth: 667100.5 CV score: 866110509 
## Fixed bandwidth: 412639.7 CV score: 860061367 
## Fixed bandwidth: 255374.3 CV score: 970183994 
## Fixed bandwidth: 509835.1 CV score: 859122810 
## Fixed bandwidth: 569905.2 CV score: 861077382 
## Fixed bandwidth: 472709.8 CV score: 858604841 
## Fixed bandwidth: 449765 CV score: 858911948 
## Fixed bandwidth: 486890.4 CV score: 858697141 
## Fixed bandwidth: 463945.7 CV score: 858668506 
## Fixed bandwidth: 478126.3 CV score: 858612660 
## Fixed bandwidth: 469362.2 CV score: 858618929 
## Fixed bandwidth: 474778.7 CV score: 858603472 
## Fixed bandwidth: 476057.3 CV score: 858605384 
## Fixed bandwidth: 473988.4 CV score: 858603347 
## Fixed bandwidth: 473500 CV score: 858603671 
## Fixed bandwidth: 474290.3 CV score: 858603298 
## Fixed bandwidth: 474476.8 CV score: 858603327 
## Fixed bandwidth: 474175 CV score: 858603303 
## Fixed bandwidth: 474361.5 CV score: 858603304 
## Fixed bandwidth: 474246.2 CV score: 858603298 
## Fixed bandwidth: 474219 CV score: 858603299 
## Fixed bandwidth: 474263.1 CV score: 858603298 
## Fixed bandwidth: 474273.5 CV score: 858603298 
## Fixed bandwidth: 474256.6 CV score: 858603298 
## Fixed bandwidth: 474267 CV score: 858603298 
## Fixed bandwidth: 474260.6 CV score: 858603298 
## Fixed bandwidth: 474264.6 CV score: 858603298 
## Fixed bandwidth: 474265.5 CV score: 858603298 
## Fixed bandwidth: 474264 CV score: 858603298 
## Fixed bandwidth: 474264.9 CV score: 858603298
#Creating the model
myGWR_model <- gwr.basic(fn_mean ~ old_pct_mean + bachelor_pct_mean + clinton_pct_mean + pop_density_mean,
                adaptive = T,
                data = GWR_inputGeo,
                bw = myBandwidthKNeighbors)  
## Warning in proj4string(data): CRS object has comment, which is lost in output
#7) A map of coefficients for County Clinton Votes on g(n). Remember your descriptive caption.
tm_shape(myGWR_model$SDF) +
  tm_polygons("clinton_pct_mean", size = 0.1, midpoint=NA,
          border.alpha = 0, palette = "BrBG", n = 5, 
          palette="Spectral", 
          title='G(n) Coeff: Clinton voters') +
  tm_shape(cnty) +  
  tm_polygons(alpha=0) + 
  tm_layout(legend.text.size = 0.6, title.size = 0.5, legend.position = c(0.01, 0.01))
## Warning in sp::proj4string(obj): CRS object has comment, which is lost in output

#8) A map of coefficients for Mean County Population Density on g(n) Remember your descriptive caption.
tm_shape(myGWR_model$SDF) +
  tm_polygons("pop_density_mean", size = 0.1, midpoint=NA,
          border.alpha = 0, palette = "BrBG", n = 5, 
          palette="Spectral", 
          title='G(n) Coeff: Mean Population Density') +
  tm_shape(cnty) +  
  tm_polygons(alpha=0) + 
  tm_layout(legend.text.size = 0.6, title.size = 0.5, legend.position = c(0.01, 0.01))
## Warning in sp::proj4string(obj): CRS object has comment, which is lost in output

#9) An r squared map of your choice (you must say what the model was, e.g. “I am looking at mean(f(n) as a function of Clinton votes and Percent Elderly”)


myBandwidthKNeighbors2 <- bw.gwr(fn_mean ~ clinton_pct_mean + pop_density_mean,
             adaptive = T,
             data=GWR_inputGeo) 
## Take a cup of tea and have a break, it will take a few minutes.
##           -----A kind suggestion from GWmodel development group
## Adaptive bandwidth: 1778 CV score: 1035068060 
## Adaptive bandwidth: 1107 CV score: 999055115 
## Adaptive bandwidth: 691 CV score: 966755999 
## Adaptive bandwidth: 435 CV score: 942313493 
## Adaptive bandwidth: 275 CV score: 924108985 
## Adaptive bandwidth: 178 CV score: 917795508 
## Adaptive bandwidth: 116 CV score: 915430592 
## Adaptive bandwidth: 80 CV score: 915636934 
## Adaptive bandwidth: 141 CV score: 915422813 
## Adaptive bandwidth: 153 CV score: 915980825 
## Adaptive bandwidth: 129 CV score: 915435159 
## Adaptive bandwidth: 143 CV score: 915557213 
## Adaptive bandwidth: 134 CV score: 915464970 
## Adaptive bandwidth: 139 CV score: 915465434 
## Adaptive bandwidth: 135 CV score: 915519648 
## Adaptive bandwidth: 137 CV score: 915566074 
## Adaptive bandwidth: 135 CV score: 915519648 
## Adaptive bandwidth: 136 CV score: 915512220 
## Adaptive bandwidth: 135 CV score: 915519648 
## Adaptive bandwidth: 135 CV score: 915519648 
## Adaptive bandwidth: 134 CV score: 915464970 
## Adaptive bandwidth: 134 CV score: 915464970 
## Adaptive bandwidth: 133 CV score: 915408963 
## Adaptive bandwidth: 140 CV score: 915502201 
## Adaptive bandwidth: 140 CV score: 915502201 
## Adaptive bandwidth: 139 CV score: 915465434 
## Adaptive bandwidth: 139 CV score: 915465434 
## Adaptive bandwidth: 138 CV score: 915475899 
## Adaptive bandwidth: 138 CV score: 915475899 
## Adaptive bandwidth: 137 CV score: 915566074 
## Adaptive bandwidth: 137 CV score: 915566074 
## Adaptive bandwidth: 136 CV score: 915512220 
## Adaptive bandwidth: 136 CV score: 915512220 
## Adaptive bandwidth: 135 CV score: 915519648 
## Adaptive bandwidth: 135 CV score: 915519648 
## Adaptive bandwidth: 134 CV score: 915464970 
## Adaptive bandwidth: 134 CV score: 915464970
myBandwidthDistance2 <- bw.gwr(fn_mean ~ clinton_pct_mean + pop_density_mean,
             adaptive = F,
             data=GWR_inputGeo) 
## Take a cup of tea and have a break, it will take a few minutes.
##           -----A kind suggestion from GWmodel development group
## Fixed bandwidth: 2822927 CV score: 1129946314 
## Fixed bandwidth: 1745014 CV score: 1049415271 
## Fixed bandwidth: 1078827 CV score: 987605543 
## Fixed bandwidth: 667100.5 CV score: 946979963 
## Fixed bandwidth: 412639.7 CV score: 918246622 
## Fixed bandwidth: 255374.3 CV score: 924793178 
## Fixed bandwidth: 509835.1 CV score: 930078609 
## Fixed bandwidth: 352569.6 CV score: 910952095 
## Fixed bandwidth: 315444.3 CV score: 909995673 
## Fixed bandwidth: 292499.6 CV score: 912495512 
## Fixed bandwidth: 329624.9 CV score: 909984907 
## Fixed bandwidth: 338389 CV score: 910242324 
## Fixed bandwidth: 324208.4 CV score: 909904553 
## Fixed bandwidth: 320860.8 CV score: 909896642 
## Fixed bandwidth: 318791.9 CV score: 909916439 
## Fixed bandwidth: 322139.5 CV score: 909894118 
## Fixed bandwidth: 322929.7 CV score: 909896050 
## Fixed bandwidth: 321651.1 CV score: 909894235 
## Fixed bandwidth: 322441.3 CV score: 909894549 
## Fixed bandwidth: 321952.9 CV score: 909894041 
## Fixed bandwidth: 321837.6 CV score: 909894069 
## Fixed bandwidth: 322024.2 CV score: 909894053 
## Fixed bandwidth: 321908.9 CV score: 909894045 
## Fixed bandwidth: 321980.1 CV score: 909894043 
## Fixed bandwidth: 321936.1 CV score: 909894042 
## Fixed bandwidth: 321963.3 CV score: 909894042 
## Fixed bandwidth: 321946.5 CV score: 909894041 
## Fixed bandwidth: 321956.9 CV score: 909894041 
## Fixed bandwidth: 321950.5 CV score: 909894041 
## Fixed bandwidth: 321949 CV score: 909894041 
## Fixed bandwidth: 321951.4 CV score: 909894041 
## Fixed bandwidth: 321949.9 CV score: 909894041 
## Fixed bandwidth: 321950.8 CV score: 909894041 
## Fixed bandwidth: 321951.1 CV score: 909894041
myGWR_model2 <- gwr.basic(fn_mean ~ clinton_pct_mean + pop_density_mean,
                adaptive = T,
                data = GWR_inputGeo,
                bw = myBandwidthKNeighbors2)
## Warning in proj4string(data): CRS object has comment, which is lost in output
tm_shape(myGWR_model2$SDF) + 
  tm_polygons(c("Local_R2"),
               title='Mean(f(n) as a function of Clinton votes and Population density') +
  tm_shape(cnty) +
  tm_polygons(alpha=0)
## Warning in sp::proj4string(obj): CRS object has comment, which is lost in output