[R-sig-Geo] Geographically weighted regression

Roger Bivand Roger@B|v@nd @end|ng |rom nhh@no
Sat Feb 23 21:20:18 CET 2019


On Sat, 23 Feb 2019, f-c-b using web.de wrote:

> Thank you for your answer.
>  I tried to do the transformations before the calculation, but the 
> Problem is still the same. I think the problem lies in the factor. But 
> in my model I need the factor, because there is no linear influence of 
> the year.
>  
> Is there any solution for this Problem?
>  https://www.dropbox.com/s/fbhwsy3sd333ung/Example.zip?dl=0 The zip-file 
> in the link contains a picture of str(Daten90) and str(Daten10) and also 
> my data and R-skript. Var4 is log(Var2).
>  

I see:

> gwrGp <- gwr(formula=Ziel~ Var1 + Var3 + Var4, data = Daten90,
+ bandwidth = bwG, gweight = gwr.Gauss,hatmatrix = TRUE,
+ fit.points=Daten10, predictions=TRUE)
Warning message:
In gwr(formula = Ziel ~ Var1 + Var3 + Var4, data = Daten90, bandwidth = 
bwG,  :
   standard errors set to NA, normalised RSS not available

but using your gwrG object and:

> gwrGp <- gwr(formula=Ziel~ Var1 + Var3 + Var4, data = Daten90,
+ bandwidth = bwG, gweight = gwr.Gauss,hatmatrix = TRUE,
+ fit.points=Daten10, predictions=TRUE, fittedGWRobject=gwrG)

I think the model matrix handling of the factor is OK. The problem in the 
GWmodel::gwr.predict() approach is that the full model matrix approach is 
used on the formula and data arguments, but not on predictdata, which is 
coerced to data frame but never regularised by going through model.matrix, 
hence the error message:

Browse[2]>
debug: if (any((inde_vars %in% names(predictdata)) == F)) stop("All the 
independent variables should be included in the predictdata")
Browse[2]> inde_vars
[1] "Var12006" "Var12007" "Var12008" "Var12009" "Var3"     "Var4"
Browse[2]> names(predictdata)
  [1] "OBJECTID"   "Rechtswert" "Hochwert"   "Var1"       "Var2"
  [6] "Var3"       "Ziel"       "Var4"       "coords.x1"  "coords.x2"

Had the proper approach been used, the names would have been the same.

The relevant part of spgwr::gwr() in R/gwr.R is:

 	if (predictions) {
             t1 <- try(slot(fit.points, "data"), silent=TRUE)
 	    if (class(t1) == "try-error")
 			stop("No data slot in fit.points")
             predx <- try(model.matrix(delete.response(mt), fit.points))
             if (class(predx) == "try-error")
 			stop("missing RHS variable in fit.points")
             if (ncol(predx) != ncol(x))
 			stop("new data matrix columns mismatch")
         }

(lines 71-80)

which uses model.matrix() and uses try() to catch mis-matches.

Hope this helps,

Roger

> Christoph
>  
>  
>  
>  
> The first step should be to look at
> 
> str(Daten90)
> str(Daten10)
> 
> and if that doesn't solve the problem, then consider a reproducible
> example, or at the very least posting the results of the above to this
> list.
> 
> Sarah
> 
> On Fri, Feb 22, 2019 at 7:38 AM <f-c-b using web.de> wrote:
> >
> > Dear all,
> >
> > I am currently working out a geographically weighted regression, in which
> 90% of the data set the model should be calculated and for 10% of the values
> to be predicted. For the prediction I use the function gwr.predict from the
> package GWModel:
> >
> > Erg<-gwr.predict(formula=Ziel~ as.factor(Var1) + log(Var2, base = exp(1))
> + Var3, data = Daten90,predictdata = Daten10,bw = bwG, kernel =
> "gaussian",adaptive = FALSE, p = 2, theta = 0, longlat = FALSE)
> >
> > I always get this error, although Daten10 and Daten90 have the same
> structure:
> > Error in gwr.predict(formula = Ziel~ as.factor(Var1) + log(Var2, base =
> exp(1)) + Var3, :
> > All the independent variables should be included in the predictdata.
> >
> > Can you tell me what the problem with this code is?
> > Or is there any other way for a GWR and the prediction?
> >
> > Thank you,
> > Christoph
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo using r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> 
> 
> 
> --
> Sarah Goslee (she/her)
> http://www.numberwright.com
> 
>

-- 
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: Roger.Bivand using nhh.no
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en


More information about the R-sig-Geo mailing list