Professor: Dohyeong Kim, Ph.D
According to an analysis by Morgan Stanley, the trend of relocating companies near their target markets, known as nearshoring, will prop up Mexican exports with an estimated 94.2 billion additional dollars in five years
The objective of this project is to perform an Exploratory Spatial Data Analysis (ESDA) to
# file.choose()
db <- read.csv("F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring\\nearshoring.csv")
summary (db)
## id state region land_area
## Min. : 888 Length:32 Length:32 Min. : 1499
## 1st Qu.:1047 Class :character Class :character 1st Qu.: 23861
## Median :1081 Mode :character Mode :character Median : 58978
## Mean :1219 Mean : 60038
## 3rd Qu.:1118 3rd Qu.: 73730
## Max. :2357 Max. :247087
## border_distance fdi_2020 fdi_2021 fdi_2022
## Min. : 8.83 Min. : 85.0 Min. : 69.00 Min. : 61.00
## 1st Qu.: 613.26 1st Qu.: 112.8 1st Qu.: 97.75 1st Qu.: 78.25
## Median : 751.64 Median : 182.5 Median : 159.50 Median : 134.00
## Mean : 704.92 Mean : 264.7 Mean : 252.47 Mean : 197.50
## 3rd Qu.: 875.76 3rd Qu.: 324.5 3rd Qu.: 300.50 3rd Qu.: 253.00
## Max. :1252.66 Max. :1429.0 Max. :1542.00 Max. :1020.00
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## Min. : 944 Min. : 10.36 Min. : 10.69 Min. : 4.994
## 1st Qu.: 1295 1st Qu.: 37.49 1st Qu.: 37.92 1st Qu.: 18.063
## Median : 2142 Median : 64.91 Median : 66.36 Median : 26.793
## Mean : 3544 Mean : 306.43 Mean : 308.31 Mean : 143.559
## 3rd Qu.: 3454 3rd Qu.: 173.08 3rd Qu.: 174.65 3rd Qu.: 68.429
## Max. :30639 Max. :6135.57 Max. :6145.37 Max. :2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## Min. : 5.478 Min. :0.00100 Min. :0.00100 Min. :20.52
## 1st Qu.: 18.936 1st Qu.:0.00875 1st Qu.:0.00775 1st Qu.:20.52
## Median : 28.520 Median :0.01700 Median :0.01600 Median :20.52
## Mean : 145.644 Mean :0.02497 Mean :0.02284 Mean :20.52
## 3rd Qu.: 69.803 3rd Qu.:0.03050 3rd Qu.:0.02650 3rd Qu.:20.52
## Max. :3020.262 Max. :0.09100 Max. :0.08800 Max. :20.52
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## Min. :19.18 Min. :253.7 Min. :250.0 Min. : 0.00
## 1st Qu.:19.18 1st Qu.:291.7 1st Qu.:286.3 1st Qu.: 0.00
## Median :19.18 Median :312.3 Median :310.3 Median : 0.00
## Mean :19.18 Mean :318.0 Mean :312.4 Mean : 24.53
## 3rd Qu.:19.18 3rd Qu.:336.1 3rd Qu.:333.5 3rd Qu.: 16.75
## Max. :19.18 Max. :441.9 Max. :428.4 Max. :204.00
## industrial_parks crime_rate_2018 crime_rate_2019
## Min. : 0.000 Min. : 1.99 Min. : 3.08
## 1st Qu.: 0.000 1st Qu.:13.46 1st Qu.:10.90
## Median : 0.000 Median :18.98 Median :19.25
## Mean : 8.781 Mean :29.96 Mean :29.95
## 3rd Qu.: 9.500 3rd Qu.:42.97 3rd Qu.:43.02
## Max. :51.000 Max. :97.19 Max. :91.69
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
head(db)
## id state region land_area border_distance fdi_2020
## 1 1057 Aguascalientes Centro-Norte 5589 625.59 204
## 2 2304 Baja California Norte 70113 8.83 564
## 3 2327 Baja California Sur Norte-Occidente 73677 800.32 154
## 4 1086 Campeche Sur 51833 978.33 85
## 5 1182 Chiapas Sur 73887 1111.82 107
## 6 888 Chihuahua Norte 247087 350.02 375
## fdi_2021 fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019
## 1 166 151 1820 246.541 253.853
## 2 553 494 5752 53.469 53.716
## 3 153 103 4577 10.363 10.685
## 4 69 67 1142 17.042 17.709
## 5 93 74 1213 72.414 74.052
## 6 384 311 3390 14.913 15.059
## jobdensity_2018 jobdensity_2019 patentsrate_2016 patentsrate_2017
## 1 103.343 104.754 0.073 0.036
## 2 24.594 25.108 0.017 0.020
## 3 4.994 5.478 0.005 0.010
## 4 7.467 7.954 0.017 0.016
## 5 26.168 28.252 0.007 0.002
## 6 6.705 6.733 0.019 0.016
## exchangerate_2018 exchangerate_2019 realwages_2016 realwages_2017
## 1 20.521 19.181 313.673 314.075
## 2 20.521 19.181 333.210 333.984
## 3 20.521 19.181 317.766 311.398
## 4 20.521 19.181 423.567 396.244
## 5 20.521 19.181 307.359 296.384
## 6 20.521 19.181 318.540 320.650
## automotive_industry industrial_parks crime_rate_2018 crime_rate_2019
## 1 14 0 6.77 5.85
## 2 7 51 73.34 77.30
## 3 0 0 12.07 8.61
## 4 0 0 8.61 7.06
## 5 0 0 11.33 9.60
## 6 19 32 71.65 91.69
count(db, region, sort = TRUE)
## region n
## 1 Centro 8
## 2 Sur 8
## 3 Norte 6
## 4 Centro-Norte 5
## 5 Norte-Occidente 5
hist(db$land_area)
boxplot(db$land_area, horizontal = TRUE)
boxplot(db$land_area,plot=FALSE)$out
## [1] 247087 151571 184934
db %>% filter_all(any_vars(. %in% c(247087)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 888 Chihuahua Norte 247087 350.02 375 384 311
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 3390 14.913 15.059 6.705 6.733
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.019 0.016 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 318.54 320.65 19 32
## crime_rate_2018 crime_rate_2019
## 1 71.65 91.69
db %>% filter_all(any_vars(. %in% c(151571)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 933 Coahuila Centro-Norte 151571 367.26 308 273
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 245 2547 20.166 20.612 8.603
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 8.929 0.032 0.037 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 335.838 333.337 55
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 31 8.93 7.27
db %>% filter_all(any_vars(. %in% c(184934)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 2303 Sonora Norte 184934 251.32 246 220 187
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 2758 15.719 15.906 7.141 7.351
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.015 0.021 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 294.761 293.158 1 0
## crime_rate_2018 crime_rate_2019
## 1 46.47 52.8
hist(db$border_distance)
boxplot(db$border_distance, horizontal = TRUE)
boxplot(db$border_distance,plot=FALSE)$out
## [1] 8.83
db %>% filter_all(any_vars(. %in% c(8.83)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 2304 Baja California Norte 70113 8.83 564 553
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 494 5752 53.469 53.716 24.594
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 25.108 0.017 0.02 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 333.21 333.984 7
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 51 73.34 77.3
hist(db$fdi_2020)
boxplot(db$fdi_2020, horizontal = TRUE)
boxplot(db$fdi_2020,plot=FALSE)$out
## [1] 1429
db %>% filter_all(any_vars(. %in% c(1429)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
hist(db$fdi_2021)
boxplot(db$fdi_2021, horizontal = TRUE)
boxplot(db$fdi_2021,plot=FALSE)$out
## [1] 1542
db %>% filter_all(any_vars(. %in% c(1542)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
hist(db$fdi_1999_2022)
boxplot(db$fdi_1999_2022, horizontal = TRUE)
boxplot(db$fdi_1999_2022,plot=FALSE)$out
## [1] 30639
db %>% filter_all(any_vars(. %in% c(30639)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
hist(db$popdensity_2018)
boxplot(db$popdensity_2018, horizontal = TRUE)
boxplot(db$popdensity_2018,plot=FALSE)$out
## [1] 6135.566 551.082 397.481
db %>% filter_all(any_vars(. %in% c(6135.566)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
db %>% filter_all(any_vars(. %in% c(551.082)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1067 Estado de Mexico Centro 30589 647.48 428 399
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 323 6091 551.082 554.086 85.763
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 87.566 0.062 0.069 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 285.788 286.906 77
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 40 19.02 17.92
db %>% filter_all(any_vars(. %in% c(397.481)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1117 Morelos Centro 4941 795.6 126 99 95
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 1403 397.481 399.635 166.558 169.288
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.03 0.026 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 342.864 334.042 0 0
## crime_rate_2018 crime_rate_2019
## 1 53.18 49.87
hist(db$popdensity_2019)
boxplot(db$popdensity_2019, horizontal = TRUE)
boxplot(db$popdensity_2019,plot=FALSE)$out
## [1] 6145.374 554.086 399.635
db %>% filter_all(any_vars(. %in% c(6145.374)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
db %>% filter_all(any_vars(. %in% c(554.086)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1067 Estado de Mexico Centro 30589 647.48 428 399
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 323 6091 551.082 554.086 85.763
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 87.566 0.062 0.069 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 285.788 286.906 77
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 40 19.02 17.92
db %>% filter_all(any_vars(. %in% c(399.635)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1117 Morelos Centro 4941 795.6 126 99 95
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 1403 397.481 399.635 166.558 169.288
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.03 0.026 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 342.864 334.042 0 0
## crime_rate_2018 crime_rate_2019
## 1 53.18 49.87
hist(db$jobdensity_2018)
boxplot(db$jobdensity_2018, horizontal = TRUE)
boxplot(db$jobdensity_2018,plot=FALSE)$out
## [1] 2990.353 340.456 166.558
db %>% filter_all(any_vars(. %in% c(2990.353)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
db %>% filter_all(any_vars(. %in% c(340.456)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1098 Jalisco Centro 21461 767.1 484 470 352
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 5919 383.722 389.537 340.456 340.224
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.018 0.016 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 337.018 328.755 10 19
## crime_rate_2018 crime_rate_2019
## 1 29.68 25.76
db %>% filter_all(any_vars(. %in% c(166.558)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1117 Morelos Centro 4941 795.6 126 99 95
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 1403 397.481 399.635 166.558 169.288
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.03 0.026 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 342.864 334.042 0 0
## crime_rate_2018 crime_rate_2019
## 1 53.18 49.87
hist(db$jobdensity_2019)
boxplot(db$jobdensity_2019, horizontal = TRUE)
boxplot(db$jobdensity_2019,plot=FALSE)$out
## [1] 3020.262 340.224 169.288
db %>% filter_all(any_vars(. %in% c(3020.262)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
db %>% filter_all(any_vars(. %in% c(340.224)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1098 Jalisco Centro 21461 767.1 484 470 352
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 5919 383.722 389.537 340.456 340.224
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.018 0.016 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 337.018 328.755 10 19
## crime_rate_2018 crime_rate_2019
## 1 29.68 25.76
db %>% filter_all(any_vars(. %in% c(169.288)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1117 Morelos Centro 4941 795.6 126 99 95
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 1403 397.481 399.635 166.558 169.288
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.03 0.026 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 342.864 334.042 0 0
## crime_rate_2018 crime_rate_2019
## 1 53.18 49.87
hist(db$patentsrate_2016)
boxplot(db$patentsrate_2016, horizontal = TRUE)
boxplot(db$patentsrate_2016,plot=FALSE)$out
## [1] 0.073 0.091 0.065
db %>% filter_all(any_vars(. %in% c(0.073)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1057 Aguascalientes Centro-Norte 5589 625.59 204 166
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 151 1820 246.541 253.853 103.343
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 104.754 0.073 0.036 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 313.673 314.075 14
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 6.77 5.85
db %>% filter_all(any_vars(. %in% c(0.091)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
db %>% filter_all(any_vars(. %in% c(0.065)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 976 Nuevo Leon Norte 64555 216.33 579 591 472
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 6511 85.521 88.557 38.326 40.043
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.065 0.064 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 381.792 375.977 165 49
## crime_rate_2018 crime_rate_2019
## 1 17.16 15.62
hist(db$patentsrate_2017)
boxplot(db$patentsrate_2017, horizontal = TRUE)
boxplot(db$patentsrate_2017,plot=FALSE)$out
## [1] 0.088 0.069 0.069 0.064 0.061
db %>% filter_all(any_vars(. %in% c(0.088)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
db %>% filter_all(any_vars(. %in% c(0.069)))
## id state region land_area border_distance fdi_2020
## 1 1067 Estado de Mexico Centro 30589 647.48 428
## 2 1051 Hidalgo Centro-Norte 80137 798.67 106
## fdi_2021 fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019
## 1 399 323 6091 551.082 554.086
## 2 97 79 1124 37.991 38.296
## jobdensity_2018 jobdensity_2019 patentsrate_2016 patentsrate_2017
## 1 85.763 87.566 0.062 0.069
## 2 45.976 47.676 0.060 0.069
## exchangerate_2018 exchangerate_2019 realwages_2016 realwages_2017
## 1 20.521 19.181 285.788 286.906
## 2 20.521 19.181 340.683 334.774
## automotive_industry industrial_parks crime_rate_2018 crime_rate_2019
## 1 77 40 19.02 17.92
## 2 0 0 15.58 11.76
db %>% filter_all(any_vars(. %in% c(0.064)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 976 Nuevo Leon Norte 64555 216.33 579 591 472
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 6511 85.521 88.557 38.326 40.043
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.065 0.064 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 381.792 375.977 165 49
## crime_rate_2018 crime_rate_2019
## 1 17.16 15.62
db %>% filter_all(any_vars(. %in% c(0.061)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1069 Queretaro Centro 11769 652.31 391 333 273
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 3644 195.397 201.383 77.325 78.519
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.061 0.061 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 381.937 377.635 20 0
## crime_rate_2018 crime_rate_2019
## 1 9.32 9.42
hist(db$realwages_2016)
boxplot(db$realwages_2016, horizontal = TRUE)
boxplot(db$realwages_2016,plot=FALSE)$out
## [1] 423.567 441.882
db %>% filter_all(any_vars(. %in% c(423.567)))
## id state region land_area border_distance fdi_2020 fdi_2021 fdi_2022
## 1 1086 Campeche Sur 51833 978.33 85 69 67
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018 jobdensity_2019
## 1 1142 17.042 17.709 7.467 7.954
## patentsrate_2016 patentsrate_2017 exchangerate_2018 exchangerate_2019
## 1 0.017 0.016 20.521 19.181
## realwages_2016 realwages_2017 automotive_industry industrial_parks
## 1 423.567 396.244 0 0
## crime_rate_2018 crime_rate_2019
## 1 8.61 7.06
db %>% filter_all(any_vars(. %in% c(441.882)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
hist(db$realwages_2017)
boxplot(db$realwages_2017, horizontal = TRUE)
boxplot(db$realwages_2017,plot=FALSE)$out
## [1] 428.362
db %>% filter_all(any_vars(. %in% c(428.362)))
## id state region land_area border_distance fdi_2020 fdi_2021
## 1 1114 Ciudad de Mexico Norte 1499 738.2 1429 1542
## fdi_2022 fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## 1 1020 30639 6135.566 6145.374 2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## 1 3020.262 0.091 0.088 20.521
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## 1 19.181 441.882 428.362 95
## industrial_parks crime_rate_2018 crime_rate_2019
## 1 0 13.93 14.44
Mexico City deviate markedly from other observations.
library("mxmaps")
summary(df_mxstate_2020)
## region state_name state_name_official state_abbr
## Length:32 Length:32 Length:32 Length:32
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## state_abbr_official year pop pop_male
## Length:32 Min. :2020 Min. : 731391 Min. : 360622
## Class :character 1st Qu.:2020 1st Qu.: 1851651 1st Qu.: 928801
## Mode :character Median :2020 Median : 3054892 Median :1488097
## Mean :2020 Mean : 3937938 Mean :1921043
## 3rd Qu.:2020 3rd Qu.: 4947592 3rd Qu.:2406242
## Max. :2020 Max. :16992418 Max. :8251295
## pop_female afromexican indigenous_language
## Min. : 370769 Min. : 10416 Min. : 2539
## 1st Qu.: 926140 1st Qu.: 34747 1st Qu.: 30331
## Median :1557615 Median : 50479 Median : 73507
## Mean :2016895 Mean : 80507 Mean : 230145
## 3rd Qu.:2541349 3rd Qu.:100404 3rd Qu.: 264067
## Max. :8741123 Max. :303923 Max. :1459648
df_mxstate_2020$value <- db$automotive_industry
mxstate_choropleth(df_mxstate_2020, title = "Automotive Supply Chain Companies Distribution in Mexico")
require(proj4) || install.packages("proj4", dependencies = T)
## Loading required package: proj4
## [1] TRUE
require(rgdal) || install.packages("rgdal", dependencies = T)
## Loading required package: rgdal
## Loading required package: sp
## 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-32, (SVN revision 1176)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 3.4.3, released 2022/04/22
## Path to GDAL shared files: C:/Users/raulc/AppData/Local/R/win-library/4.2/rgdal/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: C:/Users/raulc/AppData/Local/R/win-library/4.2/rgdal/proj
## PROJ CDN enabled: FALSE
## Linking to sp version:1.5-0
## To mute warnings of possible GDAL/OSR exportToProj4() degradation,
## use options("rgdal_show_exportToProj4_warnings"="none") before loading sp or rgdal.
##
## Attaching package: 'rgdal'
## The following object is masked from 'package:proj4':
##
## project
## [1] TRUE
require(sp) || install.packages("sp", dependencies = T)
## [1] TRUE
require(spdep) || install.packages("spdep", dependencies = T)
## Loading required package: spdep
## 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: sf
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
## [1] TRUE
require(ape) || install.packages("ape", dependencies = T)
## Loading required package: ape
## Registered S3 method overwritten by 'ape':
## method from
## plot.mst spdep
## [1] TRUE
require(gstat) || install.packages("gstat", dependencies = T)
## Loading required package: gstat
## [1] TRUE
require(MASS) || install.packages("MASS", dependencies = T)
## Loading required package: MASS
##
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
##
## select
## [1] TRUE
require(devtools) || install.packages("devtools")
## Loading required package: devtools
## Loading required package: usethis
## [1] TRUE
require(spgwr) || install.packages("spgwr", dependencies = T)
## Loading required package: spgwr
## NOTE: This package does not constitute approval of GWR
## as a method of spatial analysis; see example(gwr)
## [1] TRUE
require(spData) || install.packages("spData", dependencies = T)
## [1] TRUE
require(Matrix) || install.packages("Matrix", dependencies = T)
## Loading required package: Matrix
## [1] TRUE
require(psych) || install.packages("psych", dependencies = T)
## Loading required package: psych
## [1] TRUE
require(tmap) || install.packages("tmap")
## Loading required package: tmap
## [1] TRUE
require(readr) || install.packages("readr")
## Loading required package: readr
## [1] TRUE
require(stargazer) || install.packages("stargazer")
## Loading required package: stargazer
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
## [1] TRUE
require(automap) || install.packages("automap")
## Loading required package: automap
## [1] TRUE
require(spatialEco) || install.packages("spatialEco")
## Loading required package: spatialEco
##
## Attaching package: 'spatialEco'
## The following object is masked from 'package:dplyr':
##
## combine
## [1] TRUE
require(SpatialKDE) || install.packages("SpatialKDE")
## Loading required package: SpatialKDE
## [1] TRUE
require(smacpod) || install.packages("smacpod")
## Loading required package: smacpod
## [1] TRUE
require(lme4) || install.packages("lme4")
## Loading required package: lme4
## [1] TRUE
setwd("F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring")
getwd()
## [1] "F:/TEC/2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R/Proyecto/database_nearshoring"
library(maptools)
## Checking rgeos availability: TRUE
## 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'.
library(sp)
library(readr)
nearshoring_map2 <- readShapePoly("nearshoring_map.shp")
## Warning: readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
nearshoring.csv <- read_csv("nearshoring.csv")
## Rows: 32 Columns: 23
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): state, region
## dbl (21): id, land_area, border_distance, fdi_2020, fdi_2021, fdi_2022, fdi_...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
nearshoring_map.sp <- merge(nearshoring_map2,nearshoring.csv)
save(nearshoring_map.sp, file="nearshoring_map.RData")
library(rgdal)
st <- readOGR("nearshoring_map.shp")
## OGR data source with driver: ESRI Shapefile
## Source: "F:\TEC\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\Proyecto\database_nearshoring\nearshoring_map.shp", layer: "nearshoring_map"
## with 32 features
## It has 30 fields
## Integer64 fields read as strings: OBJECTID id land_area fdi_2020 fdi_2021 fdi_2022 fdi_1999_2
st1 <- data.frame(st)
library(sf)
cc = st_read("nearshoring_map.shp")
## Reading layer `nearshoring_map' from data source
## `F:\TEC\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\Proyecto\database_nearshoring\nearshoring_map.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 30 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -118.4042 ymin: 14.55055 xmax: -86.73862 ymax: 32.71846
## Geodetic CRS: WGS 84
summary(db)
## id state region land_area
## Min. : 888 Length:32 Length:32 Min. : 1499
## 1st Qu.:1047 Class :character Class :character 1st Qu.: 23861
## Median :1081 Mode :character Mode :character Median : 58978
## Mean :1219 Mean : 60038
## 3rd Qu.:1118 3rd Qu.: 73730
## Max. :2357 Max. :247087
## border_distance fdi_2020 fdi_2021 fdi_2022
## Min. : 8.83 Min. : 85.0 Min. : 69.00 Min. : 61.00
## 1st Qu.: 613.26 1st Qu.: 112.8 1st Qu.: 97.75 1st Qu.: 78.25
## Median : 751.64 Median : 182.5 Median : 159.50 Median : 134.00
## Mean : 704.92 Mean : 264.7 Mean : 252.47 Mean : 197.50
## 3rd Qu.: 875.76 3rd Qu.: 324.5 3rd Qu.: 300.50 3rd Qu.: 253.00
## Max. :1252.66 Max. :1429.0 Max. :1542.00 Max. :1020.00
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## Min. : 944 Min. : 10.36 Min. : 10.69 Min. : 4.994
## 1st Qu.: 1295 1st Qu.: 37.49 1st Qu.: 37.92 1st Qu.: 18.063
## Median : 2142 Median : 64.91 Median : 66.36 Median : 26.793
## Mean : 3544 Mean : 306.43 Mean : 308.31 Mean : 143.559
## 3rd Qu.: 3454 3rd Qu.: 173.08 3rd Qu.: 174.65 3rd Qu.: 68.429
## Max. :30639 Max. :6135.57 Max. :6145.37 Max. :2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## Min. : 5.478 Min. :0.00100 Min. :0.00100 Min. :20.52
## 1st Qu.: 18.936 1st Qu.:0.00875 1st Qu.:0.00775 1st Qu.:20.52
## Median : 28.520 Median :0.01700 Median :0.01600 Median :20.52
## Mean : 145.644 Mean :0.02497 Mean :0.02284 Mean :20.52
## 3rd Qu.: 69.803 3rd Qu.:0.03050 3rd Qu.:0.02650 3rd Qu.:20.52
## Max. :3020.262 Max. :0.09100 Max. :0.08800 Max. :20.52
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## Min. :19.18 Min. :253.7 Min. :250.0 Min. : 0.00
## 1st Qu.:19.18 1st Qu.:291.7 1st Qu.:286.3 1st Qu.: 0.00
## Median :19.18 Median :312.3 Median :310.3 Median : 0.00
## Mean :19.18 Mean :318.0 Mean :312.4 Mean : 24.53
## 3rd Qu.:19.18 3rd Qu.:336.1 3rd Qu.:333.5 3rd Qu.: 16.75
## Max. :19.18 Max. :441.9 Max. :428.4 Max. :204.00
## industrial_parks crime_rate_2018 crime_rate_2019
## Min. : 0.000 Min. : 1.99 Min. : 3.08
## 1st Qu.: 0.000 1st Qu.:13.46 1st Qu.:10.90
## Median : 0.000 Median :18.98 Median :19.25
## Mean : 8.781 Mean :29.96 Mean :29.95
## 3rd Qu.: 9.500 3rd Qu.:42.97 3rd Qu.:43.02
## Max. :51.000 Max. :97.19 Max. :91.69
summary(nearshoring_map.sp)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
## min max
## x -118.40417 -86.73862
## y 14.55055 32.71846
## Is projected: NA
## proj4string : [NA]
## Data attributes:
## id state region land_area
## Min. : 888 Aguascalientes : 1 Centro :8 Min. : 1499
## 1st Qu.:1047 Baja California : 1 Centro-Norte :5 1st Qu.: 23861
## Median :1081 Baja California Sur: 1 Norte :6 Median : 58978
## Mean :1219 Campeche : 1 Norte-Occidente:5 Mean : 60038
## 3rd Qu.:1118 Chiapas : 1 Sur :8 3rd Qu.: 73730
## Max. :2357 Chihuahua : 1 Max. :247087
## (Other) :26
## fdi_2020 fdi_2021 fdi_2022 OBJECTID
## Min. : 85.0 Min. : 69.00 Min. : 61.00 Min. : 888
## 1st Qu.: 112.8 1st Qu.: 97.75 1st Qu.: 78.25 1st Qu.:1047
## Median : 182.5 Median : 159.50 Median : 134.00 Median :1081
## Mean : 264.7 Mean : 252.47 Mean : 197.50 Mean :1219
## 3rd Qu.: 324.5 3rd Qu.: 300.50 3rd Qu.: 253.00 3rd Qu.:1118
## Max. :1429.0 Max. :1542.00 Max. :1020.00 Max. :2357
##
## GMI_ADMIN SQKM SQMI COLOR_MAP Shape_Leng
## MEX-AGS: 1 Min. : 1343 Min. : 518.4 3 : 4 Min. : 1.313
## MEX-BCN: 1 1st Qu.: 23484 1st Qu.: 9067.4 7 : 4 1st Qu.: 8.544
## MEX-BCS: 1 Median : 58628 Median :22636.4 8 : 4 Median :13.341
## MEX-CDZ: 1 Mean : 61289 Mean :23663.8 2 : 3 Mean :13.385
## MEX-CHH: 1 3rd Qu.: 74078 3rd Qu.:28601.3 6 : 3 3rd Qu.:18.266
## MEX-CHP: 1 Max. :247935 Max. :95727.7 1 : 2 Max. :26.273
## (Other):26 (Other):12
## Shape_Area longitude latitude border_dis
## Min. : 0.115 Min. :-115.10 Min. :16.50 Min. : 8.83
## 1st Qu.: 2.000 1st Qu.:-103.70 1st Qu.:19.07 1st Qu.: 613.26
## Median : 5.126 Median : -99.87 Median :20.69 Median : 751.64
## Mean : 5.441 Mean :-100.50 Mean :21.76 Mean : 704.92
## 3rd Qu.: 6.662 3rd Qu.: -98.08 3rd Qu.:24.46 3rd Qu.: 875.76
## Max. :22.891 Max. : -88.27 Max. :30.58 Max. :1252.66
##
## fdi_1999_2 popdensity popdensi_1 jobdensity
## Min. : 944 Min. : 10.36 Min. : 10.69 Min. : 4.99
## 1st Qu.: 1295 1st Qu.: 37.49 1st Qu.: 37.92 1st Qu.: 18.07
## Median : 2142 Median : 64.91 Median : 66.36 Median : 26.80
## Mean : 3544 Mean : 306.43 Mean : 308.31 Mean : 143.56
## 3rd Qu.: 3454 3rd Qu.: 173.08 3rd Qu.: 174.65 3rd Qu.: 68.43
## Max. :30639 Max. :6135.57 Max. :6145.37 Max. :2990.35
##
## jobdensi_1 patentsrat patentsr_1 exchangera
## Min. : 5.48 Min. :0.00000 Min. :0.00000 Min. :20.52
## 1st Qu.: 18.93 1st Qu.:0.01000 1st Qu.:0.01000 1st Qu.:20.52
## Median : 28.52 Median :0.02000 Median :0.02000 Median :20.52
## Mean : 145.64 Mean :0.02625 Mean :0.02406 Mean :20.52
## 3rd Qu.: 69.81 3rd Qu.:0.03000 3rd Qu.:0.03000 3rd Qu.:20.52
## Max. :3020.26 Max. :0.09000 Max. :0.09000 Max. :20.52
##
## exchange_1 realwages_ realwages1 crime_rate
## Min. :19.18 Min. :253.7 Min. :250.0 Min. : 1.99
## 1st Qu.:19.18 1st Qu.:291.7 1st Qu.:286.3 1st Qu.:13.46
## Median :19.18 Median :312.2 Median :310.3 Median :18.98
## Mean :19.18 Mean :318.0 Mean :312.4 Mean :29.96
## 3rd Qu.:19.18 3rd Qu.:336.1 3rd Qu.:333.5 3rd Qu.:42.97
## Max. :19.18 Max. :441.9 Max. :428.4 Max. :97.19
##
## crime_ra_1 border_distance fdi_1999_2022 popdensity_2018
## Min. : 3.08 Min. : 8.83 Min. : 944 Min. : 10.36
## 1st Qu.:10.90 1st Qu.: 613.26 1st Qu.: 1295 1st Qu.: 37.49
## Median :19.25 Median : 751.64 Median : 2142 Median : 64.91
## Mean :29.95 Mean : 704.92 Mean : 3544 Mean : 306.43
## 3rd Qu.:43.02 3rd Qu.: 875.76 3rd Qu.: 3454 3rd Qu.: 173.08
## Max. :91.69 Max. :1252.66 Max. :30639 Max. :6135.57
##
## popdensity_2019 jobdensity_2018 jobdensity_2019 patentsrate_2016
## Min. : 10.69 Min. : 4.994 Min. : 5.478 Min. :0.00100
## 1st Qu.: 37.92 1st Qu.: 18.063 1st Qu.: 18.936 1st Qu.:0.00875
## Median : 66.36 Median : 26.793 Median : 28.520 Median :0.01700
## Mean : 308.31 Mean : 143.559 Mean : 145.644 Mean :0.02497
## 3rd Qu.: 174.65 3rd Qu.: 68.429 3rd Qu.: 69.803 3rd Qu.:0.03050
## Max. :6145.37 Max. :2990.353 Max. :3020.262 Max. :0.09100
##
## patentsrate_2017 exchangerate_2018 exchangerate_2019 realwages_2016
## Min. :0.00100 Min. :20.52 Min. :19.18 Min. :253.7
## 1st Qu.:0.00775 1st Qu.:20.52 1st Qu.:19.18 1st Qu.:291.7
## Median :0.01600 Median :20.52 Median :19.18 Median :312.3
## Mean :0.02284 Mean :20.52 Mean :19.18 Mean :318.0
## 3rd Qu.:0.02650 3rd Qu.:20.52 3rd Qu.:19.18 3rd Qu.:336.1
## Max. :0.08800 Max. :20.52 Max. :19.18 Max. :441.9
##
## realwages_2017 automotive_industry industrial_parks crime_rate_2018
## Min. :250.0 Min. : 0.00 Min. : 0.000 Min. : 1.99
## 1st Qu.:286.3 1st Qu.: 0.00 1st Qu.: 0.000 1st Qu.:13.46
## Median :310.3 Median : 0.00 Median : 0.000 Median :18.98
## Mean :312.4 Mean : 24.53 Mean : 8.781 Mean :29.96
## 3rd Qu.:333.5 3rd Qu.: 16.75 3rd Qu.: 9.500 3rd Qu.:42.97
## Max. :428.4 Max. :204.00 Max. :51.000 Max. :97.19
##
## crime_rate_2019
## Min. : 3.08
## 1st Qu.:10.90
## Median :19.25
## Mean :29.95
## 3rd Qu.:43.02
## Max. :91.69
##
summary(cc)
## OBJECTID GMI_ADMIN SQKM SQMI
## Min. : 888 Length:32 Min. : 1343 Min. : 518.4
## 1st Qu.:1047 Class :character 1st Qu.: 23484 1st Qu.: 9067.4
## Median :1081 Mode :character Median : 58628 Median :22636.4
## Mean :1219 Mean : 61289 Mean :23663.8
## 3rd Qu.:1118 3rd Qu.: 74078 3rd Qu.:28601.3
## Max. :2357 Max. :247935 Max. :95727.7
## COLOR_MAP Shape_Leng Shape_Area longitude
## Length:32 Min. : 1.313 Min. : 0.115 Min. :-115.10
## Class :character 1st Qu.: 8.544 1st Qu.: 2.000 1st Qu.:-103.70
## Mode :character Median :13.341 Median : 5.126 Median : -99.87
## Mean :13.385 Mean : 5.441 Mean :-100.50
## 3rd Qu.:18.266 3rd Qu.: 6.662 3rd Qu.: -98.08
## Max. :26.273 Max. :22.891 Max. : -88.27
## latitude id state region
## Min. :16.50 Min. : 888 Length:32 Length:32
## 1st Qu.:19.07 1st Qu.:1047 Class :character Class :character
## Median :20.69 Median :1081 Mode :character Mode :character
## Mean :21.76 Mean :1219
## 3rd Qu.:24.46 3rd Qu.:1118
## Max. :30.58 Max. :2357
## land_area border_dis fdi_2020 fdi_2021
## Min. : 1499 Min. : 8.83 Min. : 85.0 Min. : 69.00
## 1st Qu.: 23861 1st Qu.: 613.26 1st Qu.: 112.8 1st Qu.: 97.75
## Median : 58978 Median : 751.64 Median : 182.5 Median : 159.50
## Mean : 60038 Mean : 704.92 Mean : 264.7 Mean : 252.47
## 3rd Qu.: 73730 3rd Qu.: 875.76 3rd Qu.: 324.5 3rd Qu.: 300.50
## Max. :247087 Max. :1252.66 Max. :1429.0 Max. :1542.00
## fdi_2022 fdi_1999_2 popdensity popdensi_1
## Min. : 61.00 Min. : 944 Min. : 10.36 Min. : 10.69
## 1st Qu.: 78.25 1st Qu.: 1295 1st Qu.: 37.49 1st Qu.: 37.92
## Median : 134.00 Median : 2142 Median : 64.91 Median : 66.36
## Mean : 197.50 Mean : 3544 Mean : 306.43 Mean : 308.31
## 3rd Qu.: 253.00 3rd Qu.: 3454 3rd Qu.: 173.08 3rd Qu.: 174.65
## Max. :1020.00 Max. :30639 Max. :6135.57 Max. :6145.37
## jobdensity jobdensi_1 patentsrat patentsr_1
## Min. : 4.99 Min. : 5.48 Min. :0.00000 Min. :0.00000
## 1st Qu.: 18.07 1st Qu.: 18.93 1st Qu.:0.01000 1st Qu.:0.01000
## Median : 26.80 Median : 28.52 Median :0.02000 Median :0.02000
## Mean : 143.56 Mean : 145.64 Mean :0.02625 Mean :0.02406
## 3rd Qu.: 68.43 3rd Qu.: 69.81 3rd Qu.:0.03000 3rd Qu.:0.03000
## Max. :2990.35 Max. :3020.26 Max. :0.09000 Max. :0.09000
## exchangera exchange_1 realwages_ realwages1
## Min. :20.52 Min. :19.18 Min. :253.7 Min. :250.0
## 1st Qu.:20.52 1st Qu.:19.18 1st Qu.:291.7 1st Qu.:286.3
## Median :20.52 Median :19.18 Median :312.2 Median :310.3
## Mean :20.52 Mean :19.18 Mean :318.0 Mean :312.4
## 3rd Qu.:20.52 3rd Qu.:19.18 3rd Qu.:336.1 3rd Qu.:333.5
## Max. :20.52 Max. :19.18 Max. :441.9 Max. :428.4
## crime_rate crime_ra_1 geometry
## Min. : 1.99 Min. : 3.08 MULTIPOLYGON :32
## 1st Qu.:13.46 1st Qu.:10.90 epsg:4326 : 0
## Median :18.98 Median :19.25 +proj=long...: 0
## Mean :29.96 Mean :29.95
## 3rd Qu.:42.97 3rd Qu.:43.02
## Max. :97.19 Max. :91.69
db_data <- data.frame(db)
nearshoring_map_data <- data.frame(nearshoring_map.sp)
plot(nearshoring_map2, pbg="blue", axes = T)
title("Spatial polygon plot")
file.choose()
## [1] "F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring\\assembly_plants.csv"
#assembly_plants <- read.csv(F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\assembly_plants.csv)
#plot(assembly_plants, axes=T, lwd = 1, pch=16, cex=0.1)
#title("Spatial Points Plot")
library(dplyr) # for data manipulation
library(ggplot2) # for visualiziation
##
## Attaching package: 'ggplot2'
## The following objects are masked from 'package:psych':
##
## %+%, alpha
library(maptools) # for mapping in R
library(ggmap) # for using Google Map with API key
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
register_google(key = "AIzaSyDJcqoz9ff23VlhazQyZthFpruiOpIVTHE")
audi_puebla.location <- data.frame(addr = c("75012 San José Chiapa, Pue."), stringsAsFactors = FALSE)
audi_puebla.latlon <- audi_puebla.location %>% mutate_geocode(addr)
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=75012+San+Jos%C3%A9+Chiapa,+Pue.&key=xxx
audi_puebla.latlon
## addr lon lat
## 1 75012 San José Chiapa, Pue. -97.76884 19.21945
bmw_slp.location <- data.frame(addr = c("Boulevard BMW No. 655 Parque Industrial Desarrollo Logistik II, 79526 S.L.P."), stringsAsFactors = FALSE)
bmw_slp.latlon <- bmw_slp.location %>% mutate_geocode(addr)
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Boulevard+BMW+No.+655+Parque+Industrial+Desarrollo+Logistik+II,+79526+S.L.P.&key=xxx
## "Boulevard BMW No...." not uniquely geocoded, using "av. america 102, 79525 laguna de san vicente, s.l.p., mexico"
bmw_slp.latlon
## addr
## 1 Boulevard BMW No. 655 Parque Industrial Desarrollo Logistik II, 79526 S.L.P.
## lon lat
## 1 -100.8647 21.97944
audi_puebla.cen <- c(mean(audi_puebla.latlon$lon), mean(audi_puebla.latlon$lat))
audi_puebla <- audi_puebla.latlon[,2:3]
bmw_slp.cen <- c(mean(bmw_slp.latlon$lon), mean(bmw_slp.latlon$lat))
bmw_slp <- bmw_slp.latlon[,2:3]
audi_puebla.road_map <- get_googlemap(center = audi_puebla.cen, maptype = "roadmap", zoom=12, markers = audi_puebla)
## Source : https://maps.googleapis.com/maps/api/staticmap?center=19.219454,-97.768836&zoom=12&size=640x640&scale=2&maptype=roadmap&markers=19.219454,-97.768836&key=xxx
ggmap(audi_puebla.road_map)
setwd("F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring")
getwd
## function ()
## .Internal(getwd())
## <bytecode: 0x0000026db10b8ec8>
## <environment: namespace:base>
assembly_plants.data <- read.csv('assembly_plants.csv', header=T, encoding = 'utf-8' )
names(assembly_plants.data)
## [1] "id" "addr" "name"
assembly_plants.data$addr <- enc2utf8(assembly_plants.data$addr)
assembly_plants.latlon <- assembly_plants.data %>% mutate_geocode(addr)
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=75012+San+Jos%C3%A9+Chiapa,+Pue.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Boulevard+BMW+No.+655+Parque+Industrial+Desarrollo+Logistik+II,+79526+S.L.P.&key=xxx
## "Boulevard BMW No...." not uniquely geocoded, using "av. america 102, 79525 laguna de san vicente, s.l.p., mexico"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Boulevard+Miguel+Ramos+Arizpe+No+124+Sur,+Saltillo,+Zona+Industrial,+25900+Ramos+Arizpe,+Coah.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Km+60.5,+Carr+Toluca+-+M%C3%A9xico,+Delegaci%C3%B3n+Sta+Ana+Tlapaltitl%C3%A1n,+50160+Toluca+de+Lerdo,+M%C3%A9x.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=KM+4.5,+Carr.+a+la+Colorada,+Parque+Industrial,+83299+Hermosillo,+Son.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Quer%C3%A9taro+%E2%80%93+Mexico+Highway+Km+36.5,+Lomas+del+Salitre,+54730+Cuautitl%C3%A1n+Izcalli&key=xxx
## "Querétaro – Mexic..." not uniquely geocoded, using "54730 cuautitlán izcalli, state of mexico, mexico"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Carretera+a+Ciudad+Juarez+Km.+11.5,+Complejo+Industrial+Chihuahua,+31000+Chihuahua&key=xxx
## "Carretera a Ciuda..." not uniquely geocoded, using "complejo industrial chihuahua, chihuahua, chih., mexico"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=R%C3%ADo+San+Lorenzo+1475,+Parque+Industrial,+36814+Castro+del+R%C3%ADo,+Gto.&key=xxx
## "Río San Lorenzo 1..." not uniquely geocoded, using "guanajuato, mexico"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=25934+Ramos+Arizpe,+Coah.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Carr.+Guanajuato+-+Silao+Kil%C3%B3metro+3.8,+36100+Silao,+Gto.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=M%C3%A9xico+120,+Central,+78390+San+Luis,+S.L.P.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=C.+Industria+Minera+700,+Delegaci%C3%B3n+Sta+Ana+Tlapaltitl%C3%A1n,+50160+Toluca+de+Lerdo,+M%C3%A9x.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Parque+Industrial,+Carretera+a+El+Castillo+7250,+45680+El+Salto,+Jal.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=La+Luz+Sur,+38140+Celaya,+Gto.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Kia+Motors+Avenue,+66664+N.L.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=36875+Salamanca,+Gto.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Carretera+Federal+Lagos+de+Moreno+Km+75,+20290+Aguascalientes,+Ags.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Carretera+Federal+Cuernavaca+-+Cuatla+Km.+4.5,+Civac,+62578+Jiutepec,+Mor.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Carretera+Libre,+Tijuana+-+Tecate+33143,+El+Realito,+22550+Tecate,+B.C.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Quer%C3%A9taro+-+Celaya+KM+45,+38160+Apaseo+el+Grande,+Gto.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Av.+San+Lorenzo+Almecatla+16,+Sanctorum,+72730+Sanctorum,+Pue.&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ave.+Mineral+de+Valenciana+Puerto,+Interior+611,+36275+Silao,+Gto.&key=xxx
head(assembly_plants.latlon)
## id
## 1 1
## 2 2
## 3 3
## 4 4
## 5 5
## 6 6
## addr
## 1 75012 San José Chiapa, Pue.
## 2 Boulevard BMW No. 655 Parque Industrial Desarrollo Logistik II, 79526 S.L.P.
## 3 Boulevard Miguel Ramos Arizpe No 124 Sur, Saltillo, Zona Industrial, 25900 Ramos Arizpe, Coah.
## 4 Km 60.5, Carr Toluca - México, Delegación Sta Ana Tlapaltitlán, 50160 Toluca de Lerdo, Méx.
## 5 KM 4.5, Carr. a la Colorada, Parque Industrial, 83299 Hermosillo, Son.
## 6 Querétaro – Mexico Highway Km 36.5, Lomas del Salitre, 54730 Cuautitlán Izcalli
## name lon lat
## 1 audi_pue -97.76884 19.21945
## 2 bmw_slp -100.86473 21.97944
## 3 fca_coa -100.94397 25.53833
## 4 fca_edm -99.62117 19.28812
## 5 ford_son -110.91624 29.00784
## 6 ford_edm -99.19544 19.66013
basemap <- qmap('mexico', zoom = 4, maptype = "roadmap")
## Source : https://maps.googleapis.com/maps/api/staticmap?center=mexico&zoom=4&size=640x640&scale=2&maptype=roadmap&language=en-EN&key=xxx
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=mexico&key=xxx
basemap + geom_point(data = assembly_plants.latlon, aes(lon, lat), size = 1.5, colour= "red")
library(ggplot2)
library(spdep)
library(MASS)
library(spmoran)
library(spatialreg)
library(sphet)
library(maptools)
library(sp)
library(sf)
library(maps)
library(rgeos)
library(ggmap)
library(mapproj)
library(RColorBrewer)
library(ggsn)
### importing spatial data
map<-readShapePoly("F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring\\nearshoring_map.shp",IDvar="id",proj4string=CRS("+proj=longlat"))
lmat<-coordinates(map)
map.centroid<-coordinates(map)
summary(map)
## Object of class SpatialPolygonsDataFrame
## Coordinates:
## min max
## x -118.40417 -86.73862
## y 14.55055 32.71846
## Is projected: FALSE
## proj4string : [+proj=longlat +datum=WGS84 +no_defs]
## Data attributes:
## OBJECTID GMI_ADMIN SQKM SQMI COLOR_MAP
## Min. : 888 MEX-AGS: 1 Min. : 1343 Min. : 518.4 3 : 4
## 1st Qu.:1047 MEX-BCN: 1 1st Qu.: 23484 1st Qu.: 9067.4 7 : 4
## Median :1081 MEX-BCS: 1 Median : 58628 Median :22636.4 8 : 4
## Mean :1219 MEX-CDZ: 1 Mean : 61289 Mean :23663.8 2 : 3
## 3rd Qu.:1118 MEX-CHH: 1 3rd Qu.: 74078 3rd Qu.:28601.3 6 : 3
## Max. :2357 MEX-CHP: 1 Max. :247935 Max. :95727.7 1 : 2
## (Other):26 (Other):12
## Shape_Leng Shape_Area longitude latitude
## Min. : 1.313 Min. : 0.115 Min. :-115.10 Min. :16.50
## 1st Qu.: 8.544 1st Qu.: 2.000 1st Qu.:-103.70 1st Qu.:19.07
## Median :13.341 Median : 5.126 Median : -99.87 Median :20.69
## Mean :13.385 Mean : 5.441 Mean :-100.50 Mean :21.76
## 3rd Qu.:18.266 3rd Qu.: 6.662 3rd Qu.: -98.08 3rd Qu.:24.46
## Max. :26.273 Max. :22.891 Max. : -88.27 Max. :30.58
##
## id state region land_area
## Min. : 888 Aguascalientes : 1 Centro :8 Min. : 1499
## 1st Qu.:1047 Baja California : 1 Centro-Norte :5 1st Qu.: 23861
## Median :1081 Baja California Sur: 1 Norte :6 Median : 58978
## Mean :1219 Campeche : 1 Norte-Occidente:5 Mean : 60038
## 3rd Qu.:1118 Chiapas : 1 Sur :8 3rd Qu.: 73730
## Max. :2357 Chihuahua : 1 Max. :247087
## (Other) :26
## border_dis fdi_2020 fdi_2021 fdi_2022
## Min. : 8.83 Min. : 85.0 Min. : 69.00 Min. : 61.00
## 1st Qu.: 613.26 1st Qu.: 112.8 1st Qu.: 97.75 1st Qu.: 78.25
## Median : 751.64 Median : 182.5 Median : 159.50 Median : 134.00
## Mean : 704.92 Mean : 264.7 Mean : 252.47 Mean : 197.50
## 3rd Qu.: 875.76 3rd Qu.: 324.5 3rd Qu.: 300.50 3rd Qu.: 253.00
## Max. :1252.66 Max. :1429.0 Max. :1542.00 Max. :1020.00
##
## fdi_1999_2 popdensity popdensi_1 jobdensity
## Min. : 944 Min. : 10.36 Min. : 10.69 Min. : 4.99
## 1st Qu.: 1295 1st Qu.: 37.49 1st Qu.: 37.92 1st Qu.: 18.07
## Median : 2142 Median : 64.91 Median : 66.36 Median : 26.80
## Mean : 3544 Mean : 306.43 Mean : 308.31 Mean : 143.56
## 3rd Qu.: 3454 3rd Qu.: 173.08 3rd Qu.: 174.65 3rd Qu.: 68.43
## Max. :30639 Max. :6135.57 Max. :6145.37 Max. :2990.35
##
## jobdensi_1 patentsrat patentsr_1 exchangera
## Min. : 5.48 Min. :0.00000 Min. :0.00000 Min. :20.52
## 1st Qu.: 18.93 1st Qu.:0.01000 1st Qu.:0.01000 1st Qu.:20.52
## Median : 28.52 Median :0.02000 Median :0.02000 Median :20.52
## Mean : 145.64 Mean :0.02625 Mean :0.02406 Mean :20.52
## 3rd Qu.: 69.81 3rd Qu.:0.03000 3rd Qu.:0.03000 3rd Qu.:20.52
## Max. :3020.26 Max. :0.09000 Max. :0.09000 Max. :20.52
##
## exchange_1 realwages_ realwages1 crime_rate
## Min. :19.18 Min. :253.7 Min. :250.0 Min. : 1.99
## 1st Qu.:19.18 1st Qu.:291.7 1st Qu.:286.3 1st Qu.:13.46
## Median :19.18 Median :312.2 Median :310.3 Median :18.98
## Mean :19.18 Mean :318.0 Mean :312.4 Mean :29.96
## 3rd Qu.:19.18 3rd Qu.:336.1 3rd Qu.:333.5 3rd Qu.:42.97
## Max. :19.18 Max. :441.9 Max. :428.4 Max. :97.19
##
## crime_ra_1
## Min. : 3.08
## 1st Qu.:10.90
## Median :19.25
## Mean :29.95
## 3rd Qu.:43.02
## Max. :91.69
##
### create spatial connectivity matrix (swm)
map.link<-poly2nb(map,queen=T) ### set SWM to queen
map.linkW<-nb2listw(map.link, style="W") ### SWM queen standardized
plot(map,border="blue",axes=FALSE,las=1)
plot(map,col="grey",border=grey(0.9),axes=T,add=T)
plot(map.linkW,coords=map.centroid,pch=19,cex=0.1,col="red",add=T) ### plot SWM queen standarized
title("Spatial Connectivity Matrix - Contiguity Case")
### Testing for Spatial Autocorrelation using Moran's I
# detecting spatial autocorrelation in main variable of interest
moran.mc(map$fdi_2020,map.linkW,nsim=9999) ### no statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$fdi_2020
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = 0.047224, observed rank = 8177, p-value = 0.1823
## alternative hypothesis: greater
moran.mc(map$fdi_2021,map.linkW,nsim=9999) ### no statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$fdi_2021
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = 0.032588, observed rank = 7836, p-value = 0.2164
## alternative hypothesis: greater
moran.mc(map$fdi_2022,map.linkW,nsim=9999) ### no statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$fdi_2022
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = 0.037805, observed rank = 7787, p-value = 0.2213
## alternative hypothesis: greater
moran.mc(map$fdi_1999_2,map.linkW,nsim=9999) ### no statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$fdi_1999_2
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = 0.032409, observed rank = 8648, p-value = 0.1352
## alternative hypothesis: greater
# detecting spatial autocorrelation in control variable - job density
moran.mc(map$jobdensity,map.linkW,nsim=9999) ### statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$jobdensity
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = 0.08376, observed rank = 9984, p-value = 0.0016
## alternative hypothesis: greater
moran.mc(map$jobdensi_1,map.linkW,nsim=9999) ### statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$jobdensi_1
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = 0.083154, observed rank = 9980, p-value = 0.002
## alternative hypothesis: greater
moran.mc(map$crime_rate,map.linkW,nsim=9999) ### statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$crime_rate
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = -0.018213, observed rank = 5792, p-value = 0.4208
## alternative hypothesis: greater
moran.mc(map$crime_ra_1,map.linkW,nsim=9999) ### statistically significant
##
## Monte-Carlo simulation of Moran I
##
## data: map$crime_ra_1
## weights: map.linkW
## number of simulations + 1: 10000
##
## statistic = -0.070396, observed rank = 4016, p-value = 0.5984
## alternative hypothesis: greater
# Lets use GeoDa to conduct cluster analysis
# install.packages("rgeoda")
library(rgeoda)
## Loading required package: digest
##
## Attaching package: 'rgeoda'
## The following object is masked from 'package:spdep':
##
## skater
map_shapefile<-st_read("F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring\\nearshoring_map.shp")
## Reading layer `nearshoring_map' from data source
## `F:\TEC\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\Proyecto\database_nearshoring\nearshoring_map.shp'
## using driver `ESRI Shapefile'
## Simple feature collection with 32 features and 30 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -118.4042 ymin: 14.55055 xmax: -86.73862 ymax: 32.71846
## Geodetic CRS: WGS 84
# create spatial weights matrix object (contiguity case)
queen_w<-queen_weights(map_shapefile)
# calculate LISA as per GEODA
lisa_dv<-local_moran(queen_w, map_shapefile["fdi_2022"])
lisa_cv1<-local_moran(queen_w, map_shapefile["jobdensity"])
lisa_cv2<-local_moran(queen_w, map_shapefile["patentsrat"])
lisa_cv3<-local_moran(queen_w, map_shapefile["crime_rate"])
# add cluster calculations to dataset
map_shapefile$cluster_dv<-as.factor(lisa_dv$GetClusterIndicators())
levels(map_shapefile$cluster_dv)<-lisa_dv$GetLabels() ### dependent variable
map_shapefile$cluster_cv1<-as.factor(lisa_cv1$GetClusterIndicators())
levels(map_shapefile$cluster_cv1)<-lisa_cv1$GetLabels() ### job density
map_shapefile$cluster_cv2<-as.factor(lisa_cv2$GetClusterIndicators())
levels(map_shapefile$cluster_cv2)<-lisa_cv2$GetLabels() ### patents rate
map_shapefile$cluster_cv3<-as.factor(lisa_cv3$GetClusterIndicators())
levels(map_shapefile$cluster_cv3)<-lisa_cv3$GetLabels() ### crime rate
# visualize clusters using GeoDa view
ggplot(data=map_shapefile) +
geom_sf(aes(fill=cluster_dv)) +
ggtitle(label = "Foreign Direct Investment 2022", subtitle = "Number of Companies - Recipients of FDI's Inflows")
ggplot(data=map_shapefile) +
geom_sf(aes(fill=cluster_cv1)) +
ggtitle(label = "Job Density 2018", subtitle = "Population per km2")
ggplot(data=map_shapefile) +
geom_sf(aes(fill=cluster_cv2)) +
ggtitle(label = "R&D Activity 2016", subtitle = "Patents per 100,000 Population")
ggplot(data=map_shapefile) +
geom_sf(aes(fill=cluster_cv3)) +
ggtitle(label = "Crime 2018", subtitle = "Homicide Rate per 100,000 Population")
### Bivariate Moran's I
lisa_bi <- local_bimoran(queen_w,map_shapefile[c('fdi_2022','jobdensi_1')])
bivariate_m <- lisa_values(lisa_bi)
bivariate_m
## [1] -0.149741647 -0.060446836 -0.357083500 -0.061212625 0.120402093
## [6] 0.158797531 0.129265178 0.027608417 0.113414498 0.054051392
## [11] 0.053969583 -0.133997214 -0.027204696 0.099445131 0.056132536
## [16] 0.032506321 0.151446874 0.002192283 0.040700508 0.472159801
## [21] -0.024360918 0.135208601 0.889435209 -0.733999145 -0.008395307
## [26] 0.114141307 0.115150229 0.136668836 0.013246490 -0.409137378
## [31] 0.112890258 0.166408594
map_shapefile$bivariate<-as.factor(lisa_bi$GetClusterIndicators())
levels(map_shapefile$bivariate)<-lisa_bi$GetLabels() ### bivariate moran
# visualize Bivariate Moran's i using GeoDa view
ggplot(data=map_shapefile) +
geom_sf(aes(fill=bivariate)) +
ggtitle(label = "Foreign Direct Investment & Job Density", subtitle = "Bivariate Local's Moran's I")
# lets import dataset
data=read.csv("F:\\TEC\\2023-01-09 Lunes a 13 Viernes Curso Análisis Espacial de Datos usando R\\Proyecto\\database_nearshoring\\nearshoring.csv")
summary(data)
## id state region land_area
## Min. : 888 Length:32 Length:32 Min. : 1499
## 1st Qu.:1047 Class :character Class :character 1st Qu.: 23861
## Median :1081 Mode :character Mode :character Median : 58978
## Mean :1219 Mean : 60038
## 3rd Qu.:1118 3rd Qu.: 73730
## Max. :2357 Max. :247087
## border_distance fdi_2020 fdi_2021 fdi_2022
## Min. : 8.83 Min. : 85.0 Min. : 69.00 Min. : 61.00
## 1st Qu.: 613.26 1st Qu.: 112.8 1st Qu.: 97.75 1st Qu.: 78.25
## Median : 751.64 Median : 182.5 Median : 159.50 Median : 134.00
## Mean : 704.92 Mean : 264.7 Mean : 252.47 Mean : 197.50
## 3rd Qu.: 875.76 3rd Qu.: 324.5 3rd Qu.: 300.50 3rd Qu.: 253.00
## Max. :1252.66 Max. :1429.0 Max. :1542.00 Max. :1020.00
## fdi_1999_2022 popdensity_2018 popdensity_2019 jobdensity_2018
## Min. : 944 Min. : 10.36 Min. : 10.69 Min. : 4.994
## 1st Qu.: 1295 1st Qu.: 37.49 1st Qu.: 37.92 1st Qu.: 18.063
## Median : 2142 Median : 64.91 Median : 66.36 Median : 26.793
## Mean : 3544 Mean : 306.43 Mean : 308.31 Mean : 143.559
## 3rd Qu.: 3454 3rd Qu.: 173.08 3rd Qu.: 174.65 3rd Qu.: 68.429
## Max. :30639 Max. :6135.57 Max. :6145.37 Max. :2990.353
## jobdensity_2019 patentsrate_2016 patentsrate_2017 exchangerate_2018
## Min. : 5.478 Min. :0.00100 Min. :0.00100 Min. :20.52
## 1st Qu.: 18.936 1st Qu.:0.00875 1st Qu.:0.00775 1st Qu.:20.52
## Median : 28.520 Median :0.01700 Median :0.01600 Median :20.52
## Mean : 145.644 Mean :0.02497 Mean :0.02284 Mean :20.52
## 3rd Qu.: 69.803 3rd Qu.:0.03050 3rd Qu.:0.02650 3rd Qu.:20.52
## Max. :3020.262 Max. :0.09100 Max. :0.08800 Max. :20.52
## exchangerate_2019 realwages_2016 realwages_2017 automotive_industry
## Min. :19.18 Min. :253.7 Min. :250.0 Min. : 0.00
## 1st Qu.:19.18 1st Qu.:291.7 1st Qu.:286.3 1st Qu.: 0.00
## Median :19.18 Median :312.3 Median :310.3 Median : 0.00
## Mean :19.18 Mean :318.0 Mean :312.4 Mean : 24.53
## 3rd Qu.:19.18 3rd Qu.:336.1 3rd Qu.:333.5 3rd Qu.: 16.75
## Max. :19.18 Max. :441.9 Max. :428.4 Max. :204.00
## industrial_parks crime_rate_2018 crime_rate_2019
## Min. : 0.000 Min. : 1.99 Min. : 3.08
## 1st Qu.: 0.000 1st Qu.:13.46 1st Qu.:10.90
## Median : 0.000 Median :18.98 Median :19.25
## Mean : 8.781 Mean :29.96 Mean :29.95
## 3rd Qu.: 9.500 3rd Qu.:42.97 3rd Qu.:43.02
## Max. :51.000 Max. :97.19 Max. :91.69
# lag_model
model1<-lagsarlm(log(fdi_2022)~log(jobdensity_2019)+patentsrate_2017+log(crime_rate_2019)+log(realwages_2017)+log(border_distance),
data=data,listw=map.linkW, zero.policy=TRUE, na.action=na.omit)
# summary(model1)
# error_model
model2<-errorsarlm(log(fdi_2022)~log(jobdensity_2019)+patentsrate_2017+log(crime_rate_2019)+log(realwages_2017)+log(border_distance),
data=data,listw=map.linkW, zero.policy=TRUE, na.action=na.omit)
# summary(model2)
# durbin_model
model3<-lagsarlm(log(fdi_2022)~log(jobdensity_2019)+patentsrate_2017+log(crime_rate_2019)+log(realwages_2017)+log(border_distance), data=data, listw=map.linkW, Durbin=TRUE)
# summary(model3)
# compare spatial regression results
# install.packages("huxtable")
library(jtools)
jtools::export_summs(model1, model2, model3)
## Registered S3 methods overwritten by 'broom':
## method from
## tidy.glht jtools
## tidy.summary.glht jtools
| Model 1 | Model 2 | Model 3 | |
|---|---|---|---|
| rho | -0.27 | -0.60 ** | |
| (0.19) | (0.22) | ||
| (Intercept) | 2.32 | 3.27 | 6.37 |
| (5.14) | (4.94) | (13.09) | |
| log(jobdensity_2019) | 0.07 | 0.03 | 0.07 |
| (0.08) | (0.07) | (0.07) | |
| patentsrate_2017 | 12.99 * | 14.03 ** | 11.38 * |
| (5.05) | (4.97) | (5.79) | |
| log(crime_rate_2019) | 0.13 | 0.11 | 0.11 |
| (0.11) | (0.09) | (0.10) | |
| log(realwages_2017) | 0.90 | 0.58 | 0.77 |
| (0.85) | (0.82) | (0.88) | |
| log(border_distance) | -0.33 ** | -0.37 *** | -0.35 *** |
| (0.11) | (0.10) | (0.10) | |
| lambda | -0.61 ** | ||
| (0.22) | |||
| lag.log(jobdensity_2019) | -0.08 | ||
| (0.16) | |||
| lag.patentsrate_2017 | 7.28 | ||
| (10.10) | |||
| lag.log(crime_rate_2019) | 0.16 | ||
| (0.23) | |||
| lag.log(realwages_2017) | 0.19 | ||
| (1.69) | |||
| lag.log(border_distance) | -0.45 | ||
| (0.29) | |||
| nobs | 32 | 32 | 32 |
| r.squared | 0.59 | 0.65 | 0.68 |
| AIC | 56.94 | 54.81 | 61.49 |
| BIC | 68.66 | 66.54 | 80.55 |
| deviance | 6.62 | 5.74 | 5.20 |
| logLik | -20.47 | -19.41 | -17.75 |
| nobs.1 | 32.00 | 32.00 | 32.00 |
| *** p < 0.001; ** p < 0.01; * p < 0.05. | |||