%\VignetteIndexEntry{Geographically Weighted Regression}
%\VignetteDepends{}
%\VignetteKeywords{spatial}
%\VignettePackage{spdep}
\documentclass[a4paper,10pt]{article} 
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage[dvips]{graphicx,color}
\usepackage{times}
\usepackage{hyperref}
\usepackage{natbib}
\usepackage[english]{babel}
\usepackage{xspace}

\usepackage{Sweave}
\usepackage{mathptm}
\usepackage{natbib}

\setkeys{Gin}{width=0.95\textwidth}
\newcommand{\strong}[1]{{\normalfont\fontseries{b}\selectfont #1}}
\let\pkg=\strong
\RequirePackage{alltt}
\newenvironment{example}{\begin{alltt}}{\end{alltt}}
\newenvironment{smallexample}{\begin{alltt}\small}{\end{alltt}}
\newcommand{\code}[1]{\texttt{\small #1}}
\def\RR{\textsf{R}\xspace}
\def\SP{\texttt{S-PLUS}\xspace}
\def\SS{\texttt{S}\xspace}
\SweaveOpts{keep.source=FALSE}

\title{Geographically Weighted Regression\footnote{This vignette formed pp. 305--308 of the first edition of Bivand, R. S.,
Pebesma, E. and G\'{o}mez-Rubio V. (2008) Applied Spatial Data Analysis with R,
Springer-Verlag, New York. It was retired from the second edition (2013) to
accommodate material on other topics, and is made available in this form
with the understanding of the publishers.}} 
\author{Roger Bivand} 

\begin{document} 

\maketitle 

<<echo=FALSE>>= 
owidth <- getOption("width")
options("width"=90)
ow <- getOption("warn")
options("warn"=-1)
.PngNo <- 0
@

<<label=afig,echo=FALSE,eval=FALSE>>= 
.PngNo <- .PngNo + 1; file <- paste("Fig-bitmap-", .PngNo, ".pdf", sep="")
pdf(file=file, width = 6.5, height = 3.5, pointsize = 12, bg = "white")
opar <- par(mar=c(3,3,1,1)+0.1)
@
<<label=afigl,echo=FALSE,eval=FALSE>>= 
.PngNo <- .PngNo + 1; file <- paste("Fig-bitmap-", .PngNo, ".pdf", sep="")
pdf(file=file, width = 6.5, height = 3.5, pointsize = 12, bg = "white")
@
<<label=bfigl,echo=FALSE,eval=FALSE>>= 
.PngNo <- .PngNo + 1; file <- paste("Fig-bitmap-", .PngNo, ".pdf", sep="")
pdf(file=file, width = 6.5, height = 5, pointsize = 12, bg = "white")
@
<<label=bfig,echo=FALSE,eval=FALSE>>= 
.PngNo <- .PngNo + 1; file <- paste("Fig-bitmap-", .PngNo, ".pdf", sep="")
pdf(file=file, width = 6.5, height = 5, pointsize = 12, bg = "white")
opar <- par(mar=c(3,3,1,1)+0.1)
@

<<label=zfig,echo=FALSE,eval=FALSE>>=
par(opar)
dev.null <- dev.off()
cat("\\includegraphics[width=0.95\\textwidth]{", file, "}\n\n", sep="")
@
<<label=zfigl,echo=FALSE,eval=FALSE>>=
dev.null <- dev.off()
cat("\\includegraphics[width=0.95\\textwidth]{", file, "}\n\n", sep="")
@

Geographically weighted regression (GWR) is an exploratory
technique mainly intended to indicate where non-stationarity is
taking place on the map, that is where locally weighted
regression coefficients move away from their global values. Its
basis is the concern that the fitted coefficient values of a
global model, fitted to all the data, may not represent detailed
local variations in the data adequately -- in this it follows
other local regression implementations. It differs, however, in
not looking for local variation in `data' space, but by moving a
weighted window over the data, estimating one set of coefficient
values at every chosen `fit' point. The fit points are very often
the points at which observations were made, but do not have to
be. If the local coefficients vary in space, it can be taken as
an indication of non-stationarity.

The technique is fully described by \citet{fotheringhametal:02}
and involves first selecting a bandwidth for an isotropic spatial
weights kernel, \hbox{typically} a Gaussian kernel with a fixed
bandwidth chosen by leave-one-out cross-validation. Choice of the
bandwidth can be very demanding, as $n$ regressions must be
fitted at each step. Alternative techniques are available, for
example for adaptive bandwidths, but they may often be even more
compute-intensive.
GWR is discussed by \citet[][pp. 316--317]{schabenberger+gotway:2005} and
\citet[][p. 434]{WallerGotway:2004}, and presented with examples by
\citet[][pp. 79--86]{lloyd:07}.

\begin{footnotesize}
<<echo=TRUE, eval=FALSE>>= 
if (require(rgdal, quietly=TRUE)) {
  NY8 <- readOGR(system.file("shapes/NY8_utm18.shp", package="spData"))
}
@
\end{footnotesize}


%FIXME
<<echo=FALSE,eval=TRUE,results=hide>>= 
load(system.file("backstore/nyGWR.RData", package="spgwr"))
#save(bwG, gwrG, gbwG, ggwrG, file="nyGWR.RData")
@
\begin{footnotesize}
<<echo=TRUE,eval=TRUE>>= 
library(spgwr)
@
<<echo=TRUE,eval=FALSE>>= 
bwG <- gwr.sel(Z~PEXPOSURE+PCTAGE65P+PCTOWNHOME, data=NY8, gweight=gwr.Gauss, verbose=FALSE)
gwrG <- gwr(Z~PEXPOSURE+PCTAGE65P+PCTOWNHOME, data=NY8, bandwidth=bwG, gweight=gwr.Gauss, hatmatrix=TRUE)
@
<<echo=TRUE,eval=TRUE>>= 
gwrG
@
\end{footnotesize}



Once the bandwidth has been found, or chosen by hand, the
\code{gwr} function may be used to fit the model with the chosen
local kernel and bandwidth. If the \code{data} argument is passed
a \code{SpatialPolygonsDataFrame} or a
\code{SpatialPointsDataFrame} object, the output object will
contain a component, which is an object of the same geometry
populated with the local coefficient estimates. If the input
objects have polygon support, the centroids of the spatial
entities are taken as the basis for analysis. The function also
takes a \code{fit.points} argument, which permits local
coefficients to be created by geographically weighted regression
for other support than the data points.


The basic GWR results are uninteresting for this data set, with
very little local variation in coefficient values; the bandwidth
is almost 180\,km. Neither \code{gwr} nor \code{gwr.sel} yet take
a \code{weights} argument, as it is unclear how non-spatial and
geographical weights should be combined. A further issue that has
arisen is that it seems that local collinearity can be induced,
or at least observed, in GWR applications. A discussion of the
issues raised is given by \citet{wheeler+tiefelsdorf:05}.

As \citet{fotheringhametal:02} describe, GWR can also be applied in a GLM
framework, and a provisional implementation permitting this has been added
to the \code{spgwr} package providing both cross-validation bandwidth
selection and geographically weighted fitting of GLM models.


\begin{footnotesize}
<<echo=TRUE,eval=FALSE>>= 
gbwG <- ggwr.sel(Cases~PEXPOSURE+PCTAGE65P+PCTOWNHOME+offset(log(POP8)), data=NY8, family="poisson", gweight=gwr.Gauss, verbose=FALSE)
ggwrG <- ggwr(Cases~PEXPOSURE+PCTAGE65P+PCTOWNHOME+offset(log(POP8)), data=NY8, family="poisson", bandwidth=gbwG, gweight=gwr.Gauss)
@
<<echo=TRUE,eval=FALSE>>= 
ggwrG
@
<<echo=FALSE,eval=TRUE>>= 
xx <- capture.output(print(ggwrG))
cat(xx[1], sep="\n")
cat(strwrap(paste(xx[2:3], collapse=" "), width=70, exdent=4), sep="\n")
cat(xx[4:11], sep="\n")
@
\end{footnotesize}

The local coefficient variation seen in this fit is not large
either, although from Fig.\,\ref{fig:PEXPOSURE} it appears that
slightly larger local coefficients for the closeness to TCE site
covariate are found farther away from TCE sites than close to
them. If, on the other hand, we consider this indication in the
light of Fig.\,\ref{fig:ggwr_pairs}, it is clear that the forcing
artefacts found by \citet{wheeler+tiefelsdorf:05} in a different
data set are replicated here.

\begin{figure}[p]
\centering
<<results=tex,echo=FALSE>>= 
<<bfig>>
spplot(ggwrG$SDF, "PEXPOSURE", col.regions=grey.colors(7, 0.95, 0.55, 2.2), cuts=6)
<<zfig>>
@ 
\caption{GWR
local coefficient estimates for the exposure to TCE site
covariate} \label{fig:PEXPOSURE}\vspace*{12pt}
<<results=tex,echo=FALSE>>= 
<<bfig>>
pairs(as(ggwrG$SDF, "data.frame")[,2:5])
<<zfig>>
@ 
\caption{Pairs
plots of GWR local coefficient estimates showing the effects of
GWR collinearity forcing} \label{fig:ggwr_pairs}\vspace*{-6pt}
\end{figure}


<<echo=FALSE>>=
options("width"=owidth)
options("warn"=ow)
@

\begin{thebibliography}{}


\bibitem[Fotheringham et~al., 2002]{fotheringhametal:02}
Fotheringham, A.~S., Brunsdon, C., and Charlton, M.~E. (2002).
 {\em Geographically Weighted Regression: The Analysis of Spatially
  Varying Relationships}.
 Wiley, Chichester.

\bibitem[Lloyd, 2007]{lloyd:07}
Lloyd, C.~D. (2007).
 {\em Local Models for Spatial Analysis}.
 CRC, Boca Raton.

\bibitem[Schabenberger and Gotway, 2005]{schabenberger+gotway:2005}
Schabenberger, O. and Gotway, C.~A. (2005).
 {\em Statistical Methods for Spatial Data Analysis}.
 Chapman \& Hall, London.

\bibitem[Waller and \hbox{Gotway}, 2004]{WallerGotway:2004}
Waller, L.~A. and Gotway, C.~A. (2004).
 {\em Applied Spatial Statistics for Public Health Data}.
 Wiley, Hoboken, NJ.

\bibitem[Wheeler and Tiefelsdorf, 2005]{wheeler+tiefelsdorf:05}
Wheeler, D. and Tiefelsdorf, M. (2005).
 Multicollinearity and correlation among local regression coefficients
  in geographically weighted regression.
 {\em Journal of Geographical Systems}, 7:161--187.


\end{thebibliography}

\end{document}

