Data Generating

data_spasial <- data.frame(
  y = runif(100),
  x1 = runif(100),
  x2 = runif(100),
  longitude = runif(100, min = -100, max = -90),
  latitude = runif(100, min = 30, max = 40)
)

coordinates(data_spasial) <- ~longitude+latitude

Optimal Bandwidth

bandwidth_optimal <- gwr.sel(
  y ~ x1 + x2,
  data = data_spasial,
  coords = cbind(data_spasial$longitude, data_spasial$latitude),
  adapt = TRUE
)
## Warning in gwr.sel(y ~ x1 + x2, data = data_spasial, coords =
## cbind(data_spasial$longitude, : data is Spatial* object, ignoring coords
## argument
## Adaptive q: 0.381966 CV score: 9.280279 
## Adaptive q: 0.618034 CV score: 9.199002 
## Adaptive q: 0.763932 CV score: 9.173039 
## Adaptive q: 0.8953112 CV score: 9.159222 
## Adaptive q: 0.8451288 CV score: 9.163005 
## Adaptive q: 0.9352988 CV score: 9.152771 
## Adaptive q: 0.9600124 CV score: 9.140396 
## Adaptive q: 0.9752863 CV score: 9.138338 
## Adaptive q: 0.975013 CV score: 9.138394 
## Adaptive q: 0.9847261 CV score: 9.136052 
## Adaptive q: 0.9905602 CV score: 9.134476 
## Adaptive q: 0.9941659 CV score: 9.133368 
## Adaptive q: 0.9963943 CV score: 9.132713 
## Adaptive q: 0.9977716 CV score: 9.13232 
## Adaptive q: 0.9986228 CV score: 9.132081 
## Adaptive q: 0.9991488 CV score: 9.131935 
## Adaptive q: 0.9994739 CV score: 9.131845 
## Adaptive q: 0.9996749 CV score: 9.13179 
## Adaptive q: 0.9997991 CV score: 9.131756 
## Adaptive q: 0.9998758 CV score: 9.131735 
## Adaptive q: 0.9999232 CV score: 9.131722 
## Adaptive q: 0.9999232 CV score: 9.131722
## Warning in gwr.sel(y ~ x1 + x2, data = data_spasial, coords =
## cbind(data_spasial$longitude, : Bandwidth converged to upper bound:1

Running GWR

## Warning in gwr(y ~ x1 + x2, data = data_spasial, coords =
## cbind(data_spasial$longitude, : data is Spatial* object, ignoring coords
## argument

Koefisien pada setiap lokasi

coef_gwr <- model_gwr$SDF@data[, c("x1", "x2")]

Summary

summary(model_gwr)
##           Length Class                  Mode     
## SDF         100  SpatialPointsDataFrame S4       
## lhat      10000  -none-                 numeric  
## lm           11  -none-                 list     
## results      14  -none-                 list     
## bandwidth   100  -none-                 numeric  
## adapt         1  -none-                 numeric  
## hatmatrix     1  -none-                 logical  
## gweight       1  -none-                 character
## gTSS          1  -none-                 numeric  
## this.call     7  -none-                 call     
## fp.given      1  -none-                 logical  
## timings      12  -none-                 numeric

UTM (Universal Transverse Mercator)

proj_longlat <- CRS("+proj=longlat +datum=WGS84")
proj_utm <- CRS("+proj=utm +zone=33 +datum=WGS84")
proj4string(data_spasial) <- CRS("+proj=longlat +datum=WGS84")
data_spasial_utm <- spTransform(data_spasial, proj_utm)
bandwidth_optimal <- gwr.sel(y ~ x1 + x2, data = data_spasial_utm, adapt = TRUE)
## Adaptive q: 0.381966 CV score: 9.273989 
## Adaptive q: 0.618034 CV score: 9.201401 
## Adaptive q: 0.763932 CV score: 9.175748 
## Adaptive q: 0.9460396 CV score: 9.148428 
## Adaptive q: 0.8764807 CV score: 9.160174 
## Adaptive q: 0.9194705 CV score: 9.154606 
## Adaptive q: 0.9666507 CV score: 9.140901 
## Adaptive q: 0.979389 CV score: 9.137609 
## Adaptive q: 0.9872617 CV score: 9.135727 
## Adaptive q: 0.9921273 CV score: 9.134603 
## Adaptive q: 0.9951344 CV score: 9.133912 
## Adaptive q: 0.9969929 CV score: 9.133496 
## Adaptive q: 0.9981415 CV score: 9.133243 
## Adaptive q: 0.9988514 CV score: 9.133089 
## Adaptive q: 0.9992901 CV score: 9.132994 
## Adaptive q: 0.9995613 CV score: 9.132935 
## Adaptive q: 0.9997288 CV score: 9.132899 
## Adaptive q: 0.9998324 CV score: 9.132877 
## Adaptive q: 0.9998964 CV score: 9.132863 
## Adaptive q: 0.9999371 CV score: 9.132855 
## Adaptive q: 0.9999371 CV score: 9.132855
## Warning in gwr.sel(y ~ x1 + x2, data = data_spasial_utm, adapt = TRUE):
## Bandwidth converged to upper bound:1
model_gwr <- gwr(y ~ x1 + x2, data = data_spasial_utm, adapt = bandwidth_optimal)

##Uji Asumsi

model_ols <- lm(y ~ x1 + x2, data = data_spasial)
summary(model_ols)
## 
## Call:
## lm(formula = y ~ x1 + x2, data = data_spasial)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.55554 -0.24964  0.04195  0.25344  0.46472 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.57242    0.07654   7.478 3.39e-11 ***
## x1          -0.07505    0.10451  -0.718    0.474    
## x2           0.01479    0.10823   0.137    0.892    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.2968 on 97 degrees of freedom
## Multiple R-squared:  0.005443,   Adjusted R-squared:  -0.01506 
## F-statistic: 0.2654 on 2 and 97 DF,  p-value: 0.7674
bptest(model_ols)
## 
##  studentized Breusch-Pagan test
## 
## data:  model_ols
## BP = 0.59203, df = 2, p-value = 0.7438
AIC(model_ols)
## [1] 45.78104
model_gwr$results$AICc
## NULL
vif(model_ols)
##       x1       x2 
## 1.000364 1.000364
residual_gwr <- model_gwr$SDF$gwr.e

```