[R-sig-Geo] cluster maps with NAs

Alessandra Carioli alessandracarioli at gmail.com
Thu Mar 27 12:34:21 CET 2014

```Dear All,

I am doing cluster maps (spatial polygons) for some variables which contain NAs. Everything goes smoothly until I try to derive the 4 sectors High-High/Low—Low. At that point I get this error message:

Error in if (varofint[[i]] >= m.varofint & varlag[[i]] >= m.varlag) sec[i] <- 1 : missing value where TRUE/FALSE needed

I have tried a couple of things to get around the problem but nothing seems to work so far. Any help on the issue is greatly appreciated!

Ale

Here is my code:

library(spdep)
listw <- nb2listw(nb) #nb is my neighbor matrix
varofint #variable to be plotted
varofint.name <- "Princeton Index 1997 Clusters”
varlag <-lag.listw(listw, varofint, NAOK=T)
# Calculate Mean of Variable of interest and lagged value of variable of interest
m.varofint <- mean(varofint,na.rm=T)
m.varlag <- mean(varlag,na.rm=T)
# Calculate Lisa Test
lisa.FOQ <- localmoran(varofint,listw, alternative="two.sided",na.action=na.exclude)
# Get significance level
vec <- c(1:n)
significance <- 0.05
vec <- ifelse(lisa.FOQ[,5] < significance, 1,0)
plot.only.significant <- TRUE # to plot only significant areas below 0.05
# Derive sector HERE IS THE PROBLEM
sec <- c(1:n)
for (i in 1:n) {
if (varofint[[i]]>=m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 1
if (varofint[[i]]<m.varofint & varlag[[i]]<m.varlag) sec[i] <- 2
if (varofint[[i]]<m.varofint & varlag[[i]]>=m.varlag) sec[i] <- 3
if (varofint[[i]]>=m.varofint & varlag[[i]]<m.varlag) sec[i] <- 4
}

# Define colors for sectors: this works fine
sec.all <- sec
colors1 <- c(1:n)
for (i in 1:n) {
if (sec.all[i]==1) colors1[i] <- “red"
if (sec.all[i]==2) colors1[i] <- “blue"
if (sec.all[i]==3) colors1[i] <- “lightblue"
if (sec.all[i]==4) colors1[i] <- “pink"
if (sec.all[i]==0) colors1[i] <- “white"
}
loc.m.data <- sec*vec
colors2 <- colors1
for (i in 1:n) {
if (loc.m.data[i]==0) colors2[i] <- “white” # ERROR MESSAGE COMES AGAIN
}

```