# [R] A question about pairs()

Ian Sue Wing isw at bu.edu
Thu Oct 30 23:11:46 CET 2008

```Greetings R users,

I am an R graphics newbie trying to produce a custom trellis plot using
pairs() with R 2.7.2.

I have spatial data on which I run a geographically weighted regression
(gwr, using the -spgwr- package). I want to check the gwr coefficients
for multicollinearity and spatial association, following Wheeler and
Tiefelsdorf (2005), and I would like to summarize the results of this
exercise using -pairs-.

My problem is that I want to generate figures for two groups of
variables and then assemble them into a panel display. The first set of
variables is the gwr coefficients, for which I want to generate x-y
scatterplots. This is straightforward, and works fine. The second set of
variables is the local correlation between the gwr coefficient
estimates, which I want to plot as kernel densities. Is there any way to
do this? I can do one or the other, but not both together.

For example:

data(columbus)
# compute gwr bandwidth
col.bw <- gwr.sel(crime ~ income + housing, data=columbus,
coords=cbind(columbus\$x, columbus\$y))
# estimate gwr
col.gauss <- gwr(crime ~ income + housing, data=columbus,
coords=cbind(columbus\$x, columbus\$y), bandwidth=col.bw, hatmatrix=TRUE)
# summarize gwr results
col.gauss
# compute local correlation matrix among gwr coefs
col.cor <- gw.cov(col.gauss\$SDF,vars=2:4,bw=col.bw,cor=T,longlat=F)
# extract gwr coefficients and local correlations
col.gauss.mat <- as(col.gauss\$SDF, "data.frame")[,2:4]
col.cor.mat <- as(col.cor\$SDF, "data.frame")[,16:18]
# define correlation function for use by -pairs-
panel.cor <- function(x,y,digits=4)
{
r = (cor(x,y,use="pairwise"))
txt <- format(c(r, 0.123456789), digits=digits)
text(0.5, 0.5, txt)
}
# show gwr results: works fine, except that text positioning is screwed up
pairs(col.gauss.mat,
upper.panel=panel.smooth,
lower.panel=panel.cor,
labels=c("Intercept","Income","Housing"))
# show gwr local correlations: works fine, except that text positioning
is screwed up
pairs(col.cor.mat,
upper.panel=panel.smooth,
lower.panel=panel.cor,
labels=c("Intercept","Income","Housing"))

# My problem is with the following function...

panel.dis <- function(...)
{
if(panel.number()==4) dist <- lines(density(col.cor.mat[,1]))
if(panel.number()==7) dist <- lines(density(col.cor.mat[,2]))
if(panel.number()==8) dist <- lines(density(col.cor.mat[,3]))
}

pairs(col.gauss.mat,
upper.panel=panel.smooth,
lower.panel=panel.dis,
labels=c("Intercept","Income","Housing"))

The last -pairs- call terminates with the following error:

Error in if (panel.number() == 4) dist <- lines(density(col.cor.mat[,  :
argument is of length zero

I thought I could address panels in this way using the panel.number()
function. I would very much appreciate it if someone could let me know
what I am doing wrong. Should I not use -pairs- at all? The problem is
that I'd like the plots to line up nicely.

-i

--
Ian Sue Wing                      675 Commonwealth Ave., Boston MA 02215
Associate Professor               Tel: (617) 353-5741
Dept. of Geography & Environment  Fax: (617) 353-5986
Boston University                 Web: http://people.bu.edu/isw

```