I'm trying to perform geographically weighted regression (gwr) by following this example using my data. My data are raster
files which I converted them to a df
, so now I work with the latter (based on this example where they use the meuse data set). When I try to plot
the residuals using this command
map.resids <- SpatialPointsDataFrame(data=data.frame(resids),coords=cbind(x,y))
I am getting this error
Error in dimnames(coords@coords) <-
*vtmp*
:length of 'dimnames' 1 not equal to array extent`.
Any idea on how to add coordinates to the residuals? Below is my code.
ntl = read.csv(file = "path/ntl_df.csv")
pop = read.csv(file = "path/pop_df.csv")
evi = read.csv(file = "path/evi_df.csv")
ntl_coords = read.csv(file = "path/ntl_df_coords.csv")
all = cbind(ntl, nir, pop, evi, tir)
#global linear model
model1 <- lm(ntl_clip ~ pop_clip + evi_clip, data = all)
summary(model1)
plot(model1, which=3)
resids<-residuals(model1)
colours <- c("dark blue", "blue", "red", "dark red")
map.resids <- SpatialPointsDataFrame(data=data.frame(resids), coords=ntl_coords)
My data can be found here.
####### EDIT #######
I found a work around to extract the coordinates from my original raster by using
ntl_coords = xyFromCell(ntl_r, 1:ncell(ntl_r))
ntl_coords = as.data.frame(ntl_coords)
but the resulted df
has 14145 rows while my raster
, ntl_clip, (after converting to a df
) has 4243 rows.
Also, here is an image that shows my dependent variable (ntl_clip), the study area (red polygon) and the residuals (orange dots). As can be seen, the residuals do extent outside of the study area and they do not cover the whole area. residuals