Skip to content
Prev 29113 / 29559 Next

package spgwr: apply model parameters to a finer spatial scale

In order to apply *GWR*'s model parameters to a finer spatial scale using
the *spgwr *package:


   1. calculate *GWR *at the coarse scale
   2. apply step 1 again using the parameters* fit.points*, *predictions *and
   *fittedGWRobject*.

The code:

library(spgwr)
library(sf)
library(raster)
library(parallel)

ghs = raster("path/ghs.tif") # fine res raster
regpoints <- as.data.frame(ghs[[1]], xy = TRUE)
regpoints = na.omit(regpoints)
coordinates(regpoints) <- c("x", "y")
gridded(regpoints) <- TRUE

block.data = read.csv(file = "path/block.data.csv") # df containing the
dependent and independent coarse variables

#convert the data to spatialPointsdf
coordinates(block.data) = c("x", "y")

# specify a model equation
eq1 <- ntl ~ ghs

# find optimal ADAPTIVE kernel bandwidth using cross validation
abw <- gwr.sel(eq1,
               data = block.data,
               adapt = TRUE,
               gweight = gwr.Gauss)

# fit a gwr based on adaptive bandwidth
cl <- makeCluster(detectCores())
xx <- gwr(eq1,
              data = block.data,
              adapt = abw,
              gweight = gwr.Gauss,
              hatmatrix = TRUE,
              se.fit = TRUE,
              cl = cl)
stopCluster(cl)

# predict to a fine spatial scale
cl <- makeCluster(detectCores())
ab_gwr <- gwr(eq1,
              data = block.data,
              adapt = abw,
              gweight = gwr.Gauss,
              fit.points = regpoints,
              predictions = TRUE,
              se.fit = TRUE,
              fittedGWRobject = xx,
              cl = cl)
stopCluster(cl)

#print the results of the model
ab_gwr

sp <- ab_gwr$SDF
sf <- st_as_sf(sp)

# intercept
intercept = as.data.frame(sf$`(Intercept)`)
intercept = SpatialPointsDataFrame(data = intercept, coords = regpoints)
gridded(intercept) <- TRUE
intercept <- raster(intercept)
raster::crs(intercept) <- "EPSG:7767"

# slope
slope = as.data.frame(sf$ghs)
slope = SpatialPointsDataFrame(data = slope, coords = regpoints)
gridded(slope) <- TRUE
slope <- raster(slope)
raster::crs(slope) <- "EPSG:7767"


gwr_pred = intercept + slope * ghs

writeRaster(gwr_pred,
            "path/gwr_pred.tif",
            overwrite = TRUE)

???? ??? 10 ??? 2022 ???? 10:56 ?.?., ?/? Nikolaos Tziokas <
nikos.tziokas at gmail.com> ??????: