2. Import Data
cities = read_delim("data/aspatial/BRAZIL_CITIES.csv", ";") %>%
select(`CITY`, `IBGE_RES_POP_BRAS`, `IBGE_RES_POP_ESTR`, `IBGE_DU_URBAN`, `IBGE_DU_RURAL`, `IBGE_15-59`, `IBGE_PLANTED_AREA`, `IBGE_CROP_PRODUCTION_$`, `IDHM`, `IDHM_Renda`, `IDHM_Longevidade`, `IDHM_Educacao`, `AREA`, `GVA_AGROPEC`, `GVA_INDUSTRY`, `GVA_SERVICES`, `GVA_PUBLIC`, `TAXES`, `GDP_CAPITA`, `COMP_TOT`, `LONG`, `LAT`) %>%
rename(City = `CITY`, B_pop = `IBGE_RES_POP_BRAS`, F_pop = `IBGE_RES_POP_ESTR`, Du_urban = `IBGE_DU_URBAN`, Du_rural = `IBGE_DU_RURAL`, Economy_active = `IBGE_15-59`, Planted_area = `IBGE_PLANTED_AREA`, Crop_production = `IBGE_CROP_PRODUCTION_$`, HD_Index = `IDHM`, GNI_Index = `IDHM_Renda`, LE_Index = `IDHM_Longevidade`, E_Index = `IDHM_Educacao`, Area = `AREA`, GVA_A = `GVA_AGROPEC`, GVA_I = `GVA_INDUSTRY`, GVA_S = `GVA_SERVICES`, GVA_P = `GVA_PUBLIC`, Taxes = `TAXES`, GDP_PC = `GDP_CAPITA`, Total_companies = `COMP_TOT`, Longitude = `LONG`, Latitude = `LAT`)
These column headers are selected as it affects the GDP of Brazil.
2016 municipality boundary file
# Read all municipalities in the country at a given year
# mun <- read_municipality(code_muni="all", year=2016)
mun <- readOGR(dsn = "data/geospatial", layer = "muni_sf")
## OGR data source with driver: ESRI Shapefile
## Source: "C:\Users\Jenny\Documents\SMU Documents\YEAR 3\Year 3 Semester 2\IS415 - Geospatial Analytics and Applications\Take-home exercises\IS415_Take-home_Ex04\data\geospatial", layer: "muni_sf"
## with 5572 features
## It has 4 fields
3. Geospatial Data Wrangling
Checking rows with NA values
cities_NA <- cities[rowSums(is.na(cities)) > 0,]
cities_NA
## # A tibble: 84 x 22
## City B_pop F_pop Du_urban Du_rural Economy_active Planted_area
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Água~ 2693 14 990 NA 1592 0
## 2 Alvo~ 195483 190 60221 NA 126085 102
## 3 Arac~ 570674 475 169830 NA 388256 38
## 4 Araç~ 16964 116 4940 NA 7458 183
## 5 Arma~ 27073 487 9030 NA 18593 10
## 6 Arra~ 27655 60 8940 NA 15468 0
## 7 Baía~ 8005 7 875 NA 1918 1856
## 8 Baln~ 107010 1079 39333 NA 63678 0
## 9 Baln~ NA NA NA NA NA 93
## 10 Baru~ 239837 912 71821 NA 161351 0
## # ... with 74 more rows, and 15 more variables: Crop_production <dbl>,
## # HD_Index <dbl>, GNI_Index <dbl>, LE_Index <dbl>, E_Index <dbl>, Area <dbl>,
## # GVA_A <dbl>, GVA_I <dbl>, GVA_S <dbl>, GVA_P <dbl>, Taxes <dbl>,
## # GDP_PC <dbl>, Total_companies <dbl>, Longitude <dbl>, Latitude <dbl>
Replacing NA values in LONG LAT columns
cities$Longitude[which(cities$City == "Balneário Rincão")] <- -49.2361
cities$Latitude[which(cities$City == "Balneário Rincão")] <- -28.8344
cities$Longitude[which(cities$City == "Lagoa Dos Patos")] <- -51.4725
cities$Latitude[which(cities$City == "Lagoa Dos Patos")] <- -31.0697
cities$Longitude[which(cities$City == "Mojuí Dos Campos")] <- -54.6431
cities$Latitude[which(cities$City == "Mojuí Dos Campos")] <- -2.68472
cities$Longitude[which(cities$City == "Paraíso Das Águas")] <- -53.0102
cities$Latitude[which(cities$City == "Paraíso Das Águas")] <- -19.0257
cities$Longitude[which(cities$City == "Pescaria Brava")] <- -48.8956
cities$Latitude[which(cities$City == "Pescaria Brava")] <- -28.4247
cities$Longitude[which(cities$City == "Pinhal Da Serra")] <- -51.1733
cities$Latitude[which(cities$City == "Pinhal Da Serra")] <- -27.8747
cities$Longitude[which(cities$City == "Pinto Bandeira")] <- -51.4503
cities$Latitude[which(cities$City == "Pinto Bandeira")] <- -29.0978
cities$Longitude[which(cities$City == "Santa Terezinha")] <- -39.5184
cities$Latitude[which(cities$City == "Santa Terezinha")] <- -12.7498
cities$Longitude[which(cities$City == "São Caetano")] <- -36.1459
cities$Latitude[which(cities$City == "São Caetano")] <- -8.33
Drop GDC Per Capita with missing values
cities <- cities %>%
filter(!is.na(GDP_PC))
Replace NA values with 0
cities$B_pop[is.na(cities$B_pop)] <- 0
cities$F_pop[is.na(cities$F_pop)] <- 0
cities$Du_urban[is.na(cities$`Du_urban`)] <- 0
cities$Du_rural[is.na(cities$Du_rural)] <- 0
cities$Economy_active[is.na(cities$Economy_active)] <- 0
cities$Planted_area[is.na(cities$Planted_area)] <- 0
cities$Crop_production[is.na(cities$Crop_production)] <- 0
cities$HD_Index[is.na(cities$HD_Index)] <- 0
cities$GNI_Index[is.na(cities$GNI_Index)] <- 0
cities$LE_Index[is.na(cities$LE_Index)] <- 0
cities$E_Index[is.na(cities$E_Index)] <- 0
cities$Area[is.na(cities$Area)] <- 0
cities$GVA_A[is.na(cities$GVA_A)] <- 0
cities$GVA_I[is.na(cities$GVA_I)] <- 0
cities$GVA_S[is.na(cities$GVA_S)] <- 0
cities$GVA_P[is.na(cities$GVA_P)] <- 0
cities$Taxes[is.na(cities$Taxes)] <- 0
cities$Total_companies[is.na(cities$Total_companies)] <- 0
Check if NA values are replaced
cities_NA <- cities[rowSums(is.na(cities)) > 0,]
cities_NA
## # A tibble: 0 x 22
## # ... with 22 variables: City <chr>, B_pop <dbl>, F_pop <dbl>, Du_urban <dbl>,
## # Du_rural <dbl>, Economy_active <dbl>, Planted_area <dbl>,
## # Crop_production <dbl>, HD_Index <dbl>, GNI_Index <dbl>, LE_Index <dbl>,
## # E_Index <dbl>, Area <dbl>, GVA_A <dbl>, GVA_I <dbl>, GVA_S <dbl>,
## # GVA_P <dbl>, Taxes <dbl>, GDP_PC <dbl>, Total_companies <dbl>,
## # Longitude <dbl>, Latitude <dbl>
All NA values are handled.
Remove cities for consistency between mun and cities data
cities <- cities %>%
filter(City != "Santa Terezinha", City != "São Caetano", City != "Fernando De Noronha")
Converting aspatial data frame into a sf object
cities.sf <- st_as_sf(cities, coords = c("Longitude", "Latitude"), crs=4674) %>%
st_transform(crs=4674)
head(cities.sf)
## Simple feature collection with 6 features and 20 fields
## geometry type: POINT
## dimension: XY
## bbox: xmin: -49.44055 ymin: -19.15585 xmax: -39.04755 ymax: -1.72347
## geographic CRS: SIRGAS 2000
## # A tibble: 6 x 21
## City B_pop F_pop Du_urban Du_rural Economy_active Planted_area
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Abad~ 6876 0 1546 591 3542 319
## 2 Abad~ 6704 0 1481 847 2709 4479
## 3 Abad~ 15609 148 3233 1422 6896 10307
## 4 Abae~ 22690 0 6667 1027 11979 1862
## 5 Abae~ 141040 60 19057 12004 53516 25200
## 6 Abai~ 10496 0 1251 1540 2631 2598
## # ... with 14 more variables: Crop_production <dbl>, HD_Index <dbl>,
## # GNI_Index <dbl>, LE_Index <dbl>, E_Index <dbl>, Area <dbl>, GVA_A <dbl>,
## # GVA_I <dbl>, GVA_S <dbl>, GVA_P <dbl>, Taxes <dbl>, GDP_PC <dbl>,
## # Total_companies <dbl>, geometry <POINT [°]>
mun <- st_as_sf(mun, 4674) %>%
st_transform(crs=4674)
4. Exploratory Data Analysis
4.1 EDA using statistical graphics - Multiple Histogram Plots distribution of variables
b_pop <- ggplot(data=cities.sf, aes(x=`B_pop`)) +
geom_histogram(bins=20, color="black", fill="light blue")
f_pop <- ggplot(data=cities.sf, aes(x= `F_pop`)) +
geom_histogram(bins=20, color="black", fill="light blue")
du_urban <- ggplot(data=cities.sf, aes(x= `Du_urban`)) +
geom_histogram(bins=20, color="black", fill="light blue")
du_rural <- ggplot(data=cities.sf, aes(x= `Du_rural`)) +
geom_histogram(bins=20, color="black", fill="light blue")
ea <- ggplot(data=cities.sf, aes(x= `Economy_active`)) +
geom_histogram(bins=20, color="black", fill="light blue")
pa <- ggplot(data=cities.sf, aes(x= `Planted_area`)) +
geom_histogram(bins=20, color="black", fill="light blue")
cp <- ggplot(data=cities.sf, aes(x= `Crop_production`)) +
geom_histogram(bins=20, color="black", fill="light blue")
hd_i <- ggplot(data=cities.sf, aes(x= `HD_Index`)) +
geom_histogram(bins=20, color="black", fill="light blue")
gni_i <- ggplot(data=cities.sf, aes(x= `GNI_Index`)) +
geom_histogram(bins=20, color="black", fill="light blue")
le_i <- ggplot(data=cities.sf, aes(x= `LE_Index`)) +
geom_histogram(bins=20, color="black", fill="light blue")
e_i <- ggplot(data=cities.sf, aes(x= `E_Index`)) +
geom_histogram(bins=20, color="black", fill="light blue")
area <- ggplot(data=cities.sf, aes(x= `Area`)) +
geom_histogram(bins=20, color="black", fill="light blue")
gva_a <- ggplot(data=cities.sf, aes(x= `GVA_A`)) +
geom_histogram(bins=20, color="black", fill="light blue")
gva_i <- ggplot(data=cities.sf, aes(x= `GVA_I`)) +
geom_histogram(bins=20, color="black", fill="light blue")
gva_s <- ggplot(data=cities.sf, aes(x= `GVA_S`)) +
geom_histogram(bins=20, color="black", fill="light blue")
gva_p <- ggplot(data=cities.sf, aes(x= `GVA_P`)) +
geom_histogram(bins=20, color="black", fill="light blue")
taxes <- ggplot(data=cities.sf, aes(x= `Taxes`)) +
geom_histogram(bins=20, color="black", fill="light blue")
gdppc <- ggplot(data=cities.sf, aes(x= `GDP_PC`)) +
geom_histogram(bins=20, color="black", fill="light blue")
total_companies <- ggplot(data=cities.sf, aes(x= `Total_companies`)) +
geom_histogram(bins=20, color="black", fill="light blue")
ggarrange(b_pop, f_pop, du_urban, du_rural, ea, pa, cp, hd_i, gni_i, le_i, e_i, area, ncol = 3, nrow = 4)

ggarrange(gva_a, gva_i, gva_s, gva_p, taxes, gdppc, total_companies, ncol = 3, nrow = 3)

The figure above reveals that most variables are a right skewed distribution. This means that more cities has low GDP per capita. Furthermore, HD_Index, GNI_Index and E_Index are normally distributed and LE_Index follows a left skewed distribution.
6. Multiple Linear Regression Method
corrplot(cor(cities[, 2:20]), diag = FALSE, order = "alphabet", tl.pos = "td", tl.cex = 0.50, number.cex= 10.5/ncol(cities), method = "number", type = "upper")

From the scatterplot matrix, it is clear that:
1. B_pop is highly correlated to Du_urban, Economy_active, F_pop, GWA_I, GWA_P, GWA_S, Taxes, Total_companies
2. Corp_production is highly correlated to Planted_area, GWA_A
3. Du_urban is highly correlated to Economy_active, F_pop, GWA_I, GWA_P, GWA_S, Taxes, Total_companies
4. E_Index is highly correlated to GNI_Index, HD_Index
5. Economy_active is highly correlated to F_pop, GWA_I, GWA_P, GWA_S, Taxes, Total_companies
6. F_pop is highly correlated to GWA_I, GWA_P, GWA_S, Taxes, Total_companies
7. GNI_Index is highly correlated to HD_Index, LE_Index
8. GWA_I is highly correlated to GWA_S, Total_companies
9. GWA_P is highly correlated to GWA_S, Taxes, Total_companies
10. GWA_S is highly correlated to Taxes, Total_companies
11. HD_Index is highly correlated to LE_Index
12. Taxes is highly correlated to Total_companies
In view of this, it is wiser to only include either one of them in the subsequent model building.
6.1 Building a model using multiple linear regression method
cities.mlr <- lm(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production + HD_Index + Area, data=cities_mun)
summary(cities.mlr)
##
## Call:
## lm(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production +
## HD_Index + Area, data = cities_mun)
##
## Residuals:
## Min 1Q Median 3Q Max
## -57427 -6925 -2944 1896 282886
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.540e+04 2.264e+03 -24.471 < 2e-16 ***
## Du_rural -8.618e-01 1.489e-01 -5.787 7.55e-09 ***
## Economy_active 5.171e-03 1.960e-03 2.639 0.00834 **
## Crop_production 2.962e-02 1.635e-03 18.117 < 2e-16 ***
## HD_Index 1.152e+05 3.354e+03 34.357 < 2e-16 ***
## Area 6.322e-02 4.286e-02 1.475 0.14032
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 17320 on 5559 degrees of freedom
## Multiple R-squared: 0.2756, Adjusted R-squared: 0.275
## F-statistic: 423 on 5 and 5559 DF, p-value: < 2.2e-16
With reference to the report above:
1. It is clear that not all the indepent variables are statistically significant. We will revised the model by removing those variables which are not statistically significant before doing further analysis.
2. Confidence interval: 95%; alpha = 0.05
3. We will be comparing the p-value with the alpha value to find out which vairable is not statistically significant. Based on the p-value, we identified that the variable Area is not statistially significant as it is not in the confidence interval. Therefore, we will exclude Area from the model.
4. Based on the Adjusted R - It is not a good model as it can account for only 27.5% of the variation of the GDP Per Capita.
6.2 Revised Model
cities.mlr1 <- lm(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production + HD_Index, data=cities_mun)
ols_regress(cities.mlr1)
## Model Summary
## ---------------------------------------------------------------------
## R 0.525 RMSE 17319.474
## R-Squared 0.275 Coef. Var 81.972
## Adj. R-Squared 0.275 MSE 299964193.152
## Pred R-Squared 0.273 MAE 8359.148
## ---------------------------------------------------------------------
## RMSE: Root Mean Square Error
## MSE: Mean Square Error
## MAE: Mean Absolute Error
##
## ANOVA
## ------------------------------------------------------------------------------------
## Sum of
## Squares DF Mean Square F Sig.
## ------------------------------------------------------------------------------------
## Regression 633652250017.661 4 158413062504.415 528.107 0.0000
## Residual 1.667801e+12 5560 299964193.152
## Total 2.301453e+12 5564
## ------------------------------------------------------------------------------------
##
## Parameter Estimates
## ---------------------------------------------------------------------------------------------------------
## model Beta Std. Error Std. Beta t Sig lower upper
## ---------------------------------------------------------------------------------------------------------
## (Intercept) -54943.818 2242.791 -24.498 0.000 -59340.566 -50547.071
## Du_rural -0.841 0.148 -0.070 -5.671 0.000 -1.131 -0.550
## Economy_active 0.005 0.002 0.032 2.639 0.008 0.001 0.009
## Crop_production 0.030 0.002 0.218 18.524 0.000 0.027 0.033
## HD_Index 114591.604 3326.924 0.420 34.444 0.000 108069.533 121113.675
## ---------------------------------------------------------------------------------------------------------
With reference to the report above:
1. When we revise the model, we can see that there is no change in the adjusted r-squared
2. Even when we reduce the number of variables/statistically insignificant variables, there is not much difference between the 2 models. Therefore, there isn’t a great impact on the explanatory power.
6.3 Checking for multicolinearity
ols_vif_tol(cities.mlr1)
## Variables Tolerance VIF
## 1 Du_rural 0.8574340 1.166271
## 2 Economy_active 0.8954046 1.116814
## 3 Crop_production 0.9374714 1.066699
## 4 HD_Index 0.8755146 1.142185
Since the VIF of the independent variables are less than 10. We can safely conclude that there are no sign of multicollinearity among the independent variables.
6.4 Test for Non-Linearity
ols_plot_resid_fit(cities.mlr1)

The figure above reveals that most of the data poitns are scattered around the 0 line, hence we can safely conclude that the relationships between the dependent variable and independent variables are linear.
6.5 Test for Normality Assumption
ols_plot_resid_hist(cities.mlr1)

The figure reveals that the residual of the multiple linear regression model (i.e. cities.mlr1) is resemble normal distribution.
6.6 Testing for Spatial Autocorrelation
cities.point.sf <- st_as_sf(cities_mun, coords=c("Longitude", "Latitude"), crs=4674) %>%
st_transform(crs=4674)
summary(cities.point.sf)
## code_mn name_mn cod_stt abbrv_s
## Min. :1100015 Bom Jesus : 5 31 : 852 MG : 852
## 1st Qu.:2512077 São Domingos : 5 35 : 645 SP : 645
## Median :3146404 Bonito : 4 43 : 498 RS : 498
## Mean :3253584 Planalto : 4 29 : 416 BA : 416
## 3rd Qu.:4119202 São Francisco: 4 41 : 399 PR : 399
## Max. :5300108 Santa Helena : 4 42 : 294 SC : 294
## (Other) :5539 (Other):2461 (Other):2461
## City B_pop F_pop Du_urban
## Length:5565 Min. : 0 Min. : 0.0 Min. : 0
## Class :character 1st Qu.: 5217 1st Qu.: 0.0 1st Qu.: 872
## Mode :character Median : 10925 Median : 0.0 Median : 1844
## Mean : 34193 Mean : 77.5 Mean : 8855
## 3rd Qu.: 23390 3rd Qu.: 10.0 3rd Qu.: 4621
## Max. :11133776 Max. :119727.0 Max. :3548433
##
## Du_rural Economy_active Planted_area Crop_production
## Min. : 0 Min. : 0 Min. : 0 Min. : 0
## 1st Qu.: 471 1st Qu.: 1732 1st Qu.: 911 1st Qu.: 2333
## Median : 916 Median : 3838 Median : 3473 Median : 13846
## Mean : 1442 Mean : 18210 Mean : 14183 Mean : 57399
## 3rd Qu.: 1812 3rd Qu.: 9628 3rd Qu.: 11174 3rd Qu.: 55608
## Max. :33809 Max. :7058221 Max. :1205669 Max. :3274885
##
## HD_Index GNI_Index LE_Index E_Index
## Min. :0.0000 Min. :0.0000 Min. :0.0000 Min. :0.0000
## 1st Qu.:0.5990 1st Qu.:0.5720 1st Qu.:0.7690 1st Qu.:0.4900
## Median :0.6650 Median :0.6540 Median :0.8080 Median :0.5600
## Mean :0.6586 Mean :0.6423 Mean :0.8009 Mean :0.5586
## 3rd Qu.:0.7180 3rd Qu.:0.7070 3rd Qu.:0.8360 3rd Qu.:0.6310
## Max. :0.8620 Max. :0.8910 Max. :0.8940 Max. :0.8250
##
## Area GVA_A GVA_I GVA_S
## Min. : 0.0 Min. : 0 Min. : 1 Min. : 2
## 1st Qu.: 204.4 1st Qu.: 4193 1st Qu.: 1724 1st Qu.: 10105
## Median : 415.8 Median : 20434 Median : 7432 Median : 31212
## Mean : 1515.5 Mean : 47293 Mean : 176081 Mean : 489857
## 3rd Qu.: 1026.4 3rd Qu.: 51238 3rd Qu.: 41311 3rd Qu.: 115521
## Max. :159533.3 Max. :1402282 Max. :63306755 Max. :464656988
##
## GVA_P Taxes GDP_PC Total_companies
## Min. : 9 Min. : -14159 Min. : 3191 Min. : 6.0
## 1st Qu.: 17260 1st Qu.: 1305 1st Qu.: 9062 1st Qu.: 68.0
## Median : 35867 Median : 5107 Median : 15866 Median : 162.0
## Mean : 123851 Mean : 118966 Mean : 21128 Mean : 907.5
## 3rd Qu.: 89316 3rd Qu.: 22209 3rd Qu.: 26155 3rd Qu.: 449.0
## Max. :41902893 Max. :117125387 Max. :314638 Max. :530446.0
##
## geometry
## MULTIPOLYGON :5565
## epsg:4674 : 0
## +proj=long...: 0
##
##
##
##
# export the residual
mlr.output <- as.data.frame(cities.mlr1$residuals)
# join the newly created data frame with cities.sf object
cities.res.sf <- cbind(cities.point.sf, cities.mlr1$residuals) %>%
rename(MLR_RES = `cities.mlr1.residuals`)
# convert cities.res.sf simple feature object into a SpatialPointsDataFrame
cities.sp <- as_Spatial(cities.res.sf)
cities.sp
## class : SpatialPolygonsDataFrame
## features : 5565
## extent : -73.99045, -28.83594, -33.75118, 5.271841 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
## variables : 25
## names : code_mn, name_mn, cod_stt, abbrv_s, City, B_pop, F_pop, Du_urban, Du_rural, Economy_active, Planted_area, Crop_production, HD_Index, GNI_Index, LE_Index, ...
## min values : 1100015, Ângulo, 11, AC, Abadia De Goiás, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ...
## max values : 5300108, Zortéa, 53, TO, Zortéa, 11133776, 119727, 3548433, 33809, 7058221, 1205669, 3274885, 0.862, 0.891, 0.894, ...
6.7 Choropleth map showing the distribution of the residual of the GDP per capita
tm_shape(st_make_valid(cities.res.sf))+
tm_fill("MLR_RES",
n = 6,
style = "fisher",
midpoint = NA)

The figure above reveal that there is sign of spatial autocorrelation. To proof that our observation is indeed true, we will be performing the Moran’s I test.
6.7.1 Moran’s I test
Null hypothesis: Residual for regression model is randomly distributed
Alternative hypothesis: Residual for regression model is not randomly distributed
Confident interval: 0.95
# Determine the upper limit
coords <- coordinates(cities.sp)
k <- knn2nb(knearneigh(coords))
kdists <- unlist(nbdists(k, coords, longlat=FALSE))
summary(kdists)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.0000 0.1090 0.1469 0.1957 0.2200 2.0972
The largest nearest neighbour distance is 2.1. Using this value as the upper threshold gives us certainty that all units will have at least one neighbour.
# compute the distance-based weight matrix
nb <- dnearneigh(coordinates(cities.sp), 0, 2.1, longlat = FALSE)
# convert the output neighbours lists (i.e. nb) into a spatial weights
nb_lw <- nb2listw(nb, style = 'W')
summary(nb_lw)
## Characteristics of weights list object:
## Neighbour list object:
## Number of regions: 5565
## Number of nonzero links: 1379706
## Percentage nonzero weights: 4.455087
## Average number of links: 247.9256
## Link number distribution:
##
## 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
## 4 5 10 12 15 10 15 11 9 13 9 13 8 8 13 10 11 11 11 7
## 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
## 5 13 5 12 8 6 7 6 5 11 8 8 14 8 16 10 6 12 10 15
## 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
## 11 7 3 3 2 8 9 11 5 7 12 11 2 8 9 5 10 10 7 6
## 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
## 8 13 16 12 12 15 15 19 8 18 17 9 7 9 15 8 16 4 9 15
## 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
## 14 9 11 10 9 13 12 10 9 8 9 2 5 10 10 6 7 10 9 10
## 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
## 8 3 9 3 7 6 7 9 13 10 9 7 9 10 11 12 12 11 8 12
## 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
## 15 12 12 10 10 11 19 11 13 14 8 13 10 16 8 12 10 10 17 14
## 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
## 18 14 11 15 15 18 8 14 15 10 17 21 14 18 11 14 17 17 13 16
## 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
## 14 24 20 16 21 18 34 23 11 16 21 14 12 12 10 12 10 14 9 11
## 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
## 16 8 10 12 14 7 7 7 11 16 11 8 11 9 8 10 8 15 10 10
## 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
## 21 8 13 7 7 7 12 11 14 13 13 9 11 11 12 11 8 8 12 11
## 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
## 13 7 7 6 7 10 9 11 14 8 8 11 6 8 9 9 10 13 7 14
## 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
## 11 12 13 12 9 11 15 9 15 7 16 12 11 9 14 7 12 11 13 12
## 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
## 13 9 12 10 12 9 14 13 10 7 13 12 11 8 9 12 14 12 11 5
## 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
## 10 7 12 11 13 13 10 21 6 13 14 6 18 11 12 13 15 18 11 14
## 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
## 12 12 13 15 17 9 22 15 15 15 20 19 21 12 19 17 15 15 19 9
## 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
## 12 16 15 17 14 19 18 17 16 12 17 12 15 11 23 19 16 20 18 12
## 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
## 13 14 11 15 18 15 16 13 12 15 17 14 15 10 10 14 11 12 20 14
## 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
## 7 6 16 18 10 13 23 11 10 11 17 12 13 14 16 17 18 18 13 16
## 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
## 10 13 13 18 10 13 15 16 9 15 11 15 14 16 21 12 15 13 15 12
## 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
## 8 19 13 24 14 10 25 14 14 11 14 11 10 13 19 12 11 13 12 13
## 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
## 14 8 13 11 10 9 11 6 7 8 12 9 11 7 11 10 11 7 9 8
## 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
## 11 12 13 10 7 9 9 4 3 6 8 3 6 13 3 6 9 7 7 4
## 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
## 6 6 7 6 8 5 3 1 6 8 4 5 3 2 3 2 3 1 3 4
## 482 483 484 485 486 487 488 489 490 491 492 493 495 496 497 498 501 502 504 505
## 5 3 3 3 2 3 5 3 2 5 1 2 1 1 1 1 1 1 4 1
## 506 507 508 510 513 514 517 519 520 524 525 526 528 529 530 532 533 536 537 538
## 1 2 1 1 1 2 1 3 2 1 2 2 1 1 1 1 2 1 1 1
## 539 541 546 549
## 1 1 1 2
## 4 least connected regions:
## 82 124 126 159 with 1 link
## 2 most connected regions:
## 1436 1443 with 549 links
##
## Weights style: W
## Weights constants summary:
## n nn S0 S1 S2
## W 5565 30969225 5565 112.3816 22384.06
lm.morantest(cities.mlr1, nb_lw)
##
## Global Moran I for regression residuals
##
## data:
## model: lm(formula = GDP_PC ~ Du_rural + Economy_active +
## Crop_production + HD_Index, data = cities_mun)
## weights: nb_lw
##
## Moran I statistic standard deviate = 18.663, p-value < 2.2e-16
## alternative hypothesis: greater
## sample estimates:
## Observed Moran I Expectation Variance
## 3.446810e-02 -3.530833e-04 3.481083e-06
The Global Moran’s I test for residual spatial autocorrelation shows that it’s p-value is 0.00000000000000022 which is less than the alpha value of 0.05. Hence, we will reject the null hypothesis that the residuals are randomly distributed. Since the Observed Global Moran I = 3.446810 which is greater than 0, we can infer than the residuals resemble cluster distribution.
7. Building GWmodel
7.1 Building Fixed Bandwidth GWR Model
7.1.1 Computing fixed bandwith
bw.fixed <- bw.gwr(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production + HD_Index, data=cities.sp, approach="CV", kernel="gaussian", adaptive=FALSE, longlat=FALSE)
## Take a cup of tea and have a break, it will take a few minutes.
## -----A kind suggestion from GWmodel development group
## Fixed bandwidth: 33.62568 CV score: 1.670366e+12
## Fixed bandwidth: 20.78597 CV score: 1.665971e+12
## Fixed bandwidth: 12.85059 CV score: 1.65666e+12
## Fixed bandwidth: 7.946257 CV score: 1.638718e+12
## Fixed bandwidth: 4.915213 CV score: 1.616839e+12
## Fixed bandwidth: 3.041924 CV score: 1.589646e+12
## Fixed bandwidth: 1.884168 CV score: 1.563693e+12
## Fixed bandwidth: 1.168636 CV score: 1.604593e+12
## Fixed bandwidth: 2.326392 CV score: 1.572266e+12
## Fixed bandwidth: 1.610859 CV score: 1.567696e+12
## Fixed bandwidth: 2.053083 CV score: 1.566108e+12
## Fixed bandwidth: 1.779774 CV score: 1.563617e+12
## Fixed bandwidth: 1.715254 CV score: 1.564453e+12
## Fixed bandwidth: 1.819649 CV score: 1.563466e+12
## Fixed bandwidth: 1.844293 CV score: 1.56349e+12
## Fixed bandwidth: 1.804418 CV score: 1.563494e+12
## Fixed bandwidth: 1.829062 CV score: 1.563465e+12
## Fixed bandwidth: 1.83488 CV score: 1.563471e+12
## Fixed bandwidth: 1.825467 CV score: 1.563464e+12
## Fixed bandwidth: 1.823245 CV score: 1.563464e+12
## Fixed bandwidth: 1.82684 CV score: 1.563464e+12
## Fixed bandwidth: 1.824618 CV score: 1.563464e+12
## Fixed bandwidth: 1.824093 CV score: 1.563464e+12
## Fixed bandwidth: 1.824942 CV score: 1.563464e+12
## Fixed bandwidth: 1.825142 CV score: 1.563464e+12
## Fixed bandwidth: 1.824818 CV score: 1.563464e+12
## Fixed bandwidth: 1.824742 CV score: 1.563464e+12
## Fixed bandwidth: 1.824866 CV score: 1.563464e+12
## Fixed bandwidth: 1.824895 CV score: 1.563464e+12
The result shows that the recommended bandwidth is 1.824895 metres.
7.1.2 GWModel method - fixed bandwith
gwr.fixed <- gwr.basic(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production + HD_Index, data=cities.sp, bw=bw.fixed, kernel = 'gaussian', longlat = FALSE)
gwr.fixed
## ***********************************************************************
## * Package GWmodel *
## ***********************************************************************
## Program starts at: 2020-05-31 23:18:47
## Call:
## gwr.basic(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production +
## HD_Index, data = cities.sp, bw = bw.fixed, kernel = "gaussian",
## longlat = FALSE)
##
## Dependent (y) variable: GDP_PC
## Independent variables: Du_rural Economy_active Crop_production HD_Index
## Number of data points: 5565
## ***********************************************************************
## * Results of Global Regression *
## ***********************************************************************
##
## Call:
## lm(formula = formula, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -58025 -6953 -2951 1899 283047
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.494e+04 2.243e+03 -24.498 < 2e-16 ***
## Du_rural -8.406e-01 1.482e-01 -5.671 1.49e-08 ***
## Economy_active 5.172e-03 1.960e-03 2.639 0.00834 **
## Crop_production 2.997e-02 1.618e-03 18.524 < 2e-16 ***
## HD_Index 1.146e+05 3.327e+03 34.444 < 2e-16 ***
##
## ---Significance stars
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Residual standard error: 17320 on 5560 degrees of freedom
## Multiple R-squared: 0.2753
## Adjusted R-squared: 0.2748
## F-statistic: 528.1 on 4 and 5560 DF, p-value: < 2.2e-16
## ***Extra Diagnostic information
## Residual sum of squares: 1.667801e+12
## Sigma(hat): 17314.8
## AIC: 124424
## AICc: 124424
## ***********************************************************************
## * Results of Geographically Weighted Regression *
## ***********************************************************************
##
## *********************Model calibration information*********************
## Kernel function: gaussian
## Fixed bandwidth: 1.824866
## Regression points: the same locations as observations are used.
## Distance metric: Euclidean distance metric is used.
##
## ****************Summary of GWR coefficient estimates:******************
## Min. 1st Qu. Median 3rd Qu. Max.
## Intercept -1.5727e+05 -5.7548e+04 -3.0411e+04 -1.5710e+04 8.4608e+04
## Du_rural -9.0929e+00 -1.4262e+00 -4.5751e-01 -4.2204e-04 1.4473e+00
## Economy_active -2.6057e-01 3.6459e-03 8.7188e-03 1.4260e-02 1.8470e-01
## Crop_production -1.3250e-02 1.3136e-02 2.2911e-02 3.4718e-02 1.2260e-01
## HD_Index -7.1498e+04 4.6193e+04 7.8587e+04 1.1611e+05 2.6008e+05
## ************************Diagnostic information*************************
## Number of data points: 5565
## Effective number of parameters (2trace(S) - trace(S'S)): 184.784
## Effective degrees of freedom (n-2trace(S) + trace(S'S)): 5380.216
## AICc (GWR book, Fotheringham, et al. 2002, p. 61, eq 2.33): 124054.8
## AIC (GWR book, Fotheringham, et al. 2002,GWR p. 96, eq. 4.22): 123910.6
## Residual sum of squares: 1.487472e+12
## R-square value: 0.3536813
## Adjusted R-square value: 0.3314793
##
## ***********************************************************************
## Program stops at: 2020-05-31 23:18:58
The result shows that the Adjusted R-squared is 0.3314793.
7.2 Building Adaptive Bandwidth GWR Model
7.2.1 Computing the adaptive bandwidth
# calculate the distance vector between any GW model calibration point(s) and the data points
dv <- gw.dist(dp.locat = coordinates(cities.sp))
bw.adaptive <- bw.gwr(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production + HD_Index, data=cities.sp, approach="CV", kernel="gaussian", adaptive=TRUE, longlat=FALSE, dMat=dv)
## Take a cup of tea and have a break, it will take a few minutes.
## -----A kind suggestion from GWmodel development group
## Adaptive bandwidth: 3446 CV score: 1.661612e+12
## Adaptive bandwidth: 2138 CV score: 1.650116e+12
## Adaptive bandwidth: 1327 CV score: 1.630983e+12
## Adaptive bandwidth: 829 CV score: 1.614275e+12
## Adaptive bandwidth: 517 CV score: 1.596307e+12
## Adaptive bandwidth: 329 CV score: 1.579968e+12
## Adaptive bandwidth: 207 CV score: 1.569933e+12
## Adaptive bandwidth: 138 CV score: 1.570825e+12
## Adaptive bandwidth: 256 CV score: 1.573341e+12
## Adaptive bandwidth: 183 CV score: 1.569522e+12
## Adaptive bandwidth: 161 CV score: 1.569228e+12
## Adaptive bandwidth: 155 CV score: 1.569691e+12
## Adaptive bandwidth: 172 CV score: 1.5691e+12
## Adaptive bandwidth: 172 CV score: 1.5691e+12
The result shows that the recommended number of data points to be used is 172.
7.2.2 Constructing the adaptive bandwidth gwr model
gwr.adaptive <- gwr.basic(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production + HD_Index, data=cities.sp, bw=bw.adaptive, kernel = 'gaussian', adaptive=TRUE, longlat = FALSE)
gwr.adaptive
## ***********************************************************************
## * Package GWmodel *
## ***********************************************************************
## Program starts at: 2020-05-31 23:20:33
## Call:
## gwr.basic(formula = GDP_PC ~ Du_rural + Economy_active + Crop_production +
## HD_Index, data = cities.sp, bw = bw.adaptive, kernel = "gaussian",
## adaptive = TRUE, longlat = FALSE)
##
## Dependent (y) variable: GDP_PC
## Independent variables: Du_rural Economy_active Crop_production HD_Index
## Number of data points: 5565
## ***********************************************************************
## * Results of Global Regression *
## ***********************************************************************
##
## Call:
## lm(formula = formula, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -58025 -6953 -2951 1899 283047
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -5.494e+04 2.243e+03 -24.498 < 2e-16 ***
## Du_rural -8.406e-01 1.482e-01 -5.671 1.49e-08 ***
## Economy_active 5.172e-03 1.960e-03 2.639 0.00834 **
## Crop_production 2.997e-02 1.618e-03 18.524 < 2e-16 ***
## HD_Index 1.146e+05 3.327e+03 34.444 < 2e-16 ***
##
## ---Significance stars
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## Residual standard error: 17320 on 5560 degrees of freedom
## Multiple R-squared: 0.2753
## Adjusted R-squared: 0.2748
## F-statistic: 528.1 on 4 and 5560 DF, p-value: < 2.2e-16
## ***Extra Diagnostic information
## Residual sum of squares: 1.667801e+12
## Sigma(hat): 17314.8
## AIC: 124424
## AICc: 124424
## ***********************************************************************
## * Results of Geographically Weighted Regression *
## ***********************************************************************
##
## *********************Model calibration information*********************
## Kernel function: gaussian
## Adaptive bandwidth: 172 (number of nearest neighbours)
## Regression points: the same locations as observations are used.
## Distance metric: Euclidean distance metric is used.
##
## ****************Summary of GWR coefficient estimates:******************
## Min. 1st Qu. Median 3rd Qu. Max.
## Intercept -2.0694e+05 -5.6432e+04 -3.4987e+04 -1.7787e+04 6.2953e+04
## Du_rural -5.1314e+00 -1.6540e+00 -4.6722e-01 -4.1023e-02 1.6545e+00
## Economy_active -7.3025e-02 4.2950e-03 8.3686e-03 1.2680e-02 2.9300e-02
## Crop_production -2.1249e-02 1.3604e-02 2.2951e-02 3.4820e-02 9.4000e-02
## HD_Index -4.4175e+04 5.1822e+04 8.5439e+04 1.1568e+05 3.3137e+05
## ************************Diagnostic information*************************
## Number of data points: 5565
## Effective number of parameters (2trace(S) - trace(S'S)): 104.1464
## Effective degrees of freedom (n-2trace(S) + trace(S'S)): 5460.854
## AICc (GWR book, Fotheringham, et al. 2002, p. 61, eq 2.33): 123984.1
## AIC (GWR book, Fotheringham, et al. 2002,GWR p. 96, eq. 4.22): 123905.4
## Residual sum of squares: 1.502418e+12
## R-square value: 0.3471874
## Adjusted R-square value: 0.334735
##
## ***********************************************************************
## Program stops at: 2020-05-31 23:20:50
The result shows that the Adjusted R-squared is 0.334735.
Comparing the adjusted R-square value of the fixed (0.3314793) and adaptive method (0.334735), the adaptive method produced a better adjusted R-square value. Therefore, we will be using the adaptive method for the subsequent analysis.
8. Visualising GWR Output
8.1 Converting SDF into sf data.frame
cities.sf.adaptive <- st_as_sf(gwr.adaptive$SDF) %>%
st_transform(crs=4674)
cities.sf.adaptive.transform <- st_transform(cities.sf.adaptive, 4674)
gwr.adaptive.output <- as.data.frame(gwr.adaptive$SDF)
cities.sf.adaptive <- cbind(cities.res.sf, as.matrix(gwr.adaptive.output))
summary(gwr.adaptive$SDF$yhat)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -32889 10419 19921 21197 30276 136227
8.2 Visualising local R2
tm_shape(st_make_valid(cities.sf.adaptive))+
tm_fill("Local_R2",
n = 6,
style = "fisher",
midpoint = NA)

Local R2 indicate how well the local regression model fits observed y values. We can observe that at the lower portion of the map, the values are relatively lower and this indicates that the local model is performing poorly and the relationship between GDP_PC and the independent variables are weaker. There may be important variables that are missing from the regression model and hence, causing the model to be performing poorly. However, at the top right portion of the map, the values are higher and this allows us to infer that the model is performing well and the relationship between GDP_PC and the independent variables are stronger.
8.3 Visualising Coefficient Standard Error (Intercept_SE)
tm_shape(st_make_valid(cities.sf.adaptive))+
tm_fill("Intercept_SE",
n = 6,
style = "fisher")

Coefficient Standard Error measure the reliability of each coefficient estimate. Confidence in those estimates are higher when standard errors are small in relation to the actual coefficient values. Overall, we can observed that our intercept_SE is relatively low, which means that there is no signs of local collinearity issue in the model. However, there are some municipalities in the map, mainly the eastern and southern part of Brazil have a higher intercept_SE. The large standard errors may indicate problems with local collinearity.
8.4 Visualising predicted values (yhat)
tm_shape(st_make_valid(cities.sf.adaptive))+
tm_fill("yhat",
n = 6,
style = "fisher",
midpoint = NA)

Y-hat values represents the predicted equation for a line of best fit in linear regression. It is used to differentiate between the predicted data and the observed data. Based on the map above, we can see that there are significantly more municipalities with lower y-hat and municipalities with greater y-hat values are located at the central region of the map. This allows us to infer that our model is performing well.
8.5 Visualising residual
cities.sf.adaptive <- cities.sf.adaptive %>%
mutate(`log_residual` = log(residual))
cities.sf.adaptive$log_residual[is.nan(cities.sf.adaptive$log_residual)] <- 0
tm_shape(st_make_valid(cities.sf.adaptive))+
tm_fill("log_residual",
n = 6,
style = "fisher",
midpoint = NA)

Residuals is the subtraction of the fitted y values from the observed y values. Based on the map above, we observed that most municipalities has a residual of 0. This means that there is no difference bewteen the observed and predicted values of GDP_PC. Hence, we can infer that the model is performing well.