[R-sig-Geo] Fwd: Error is spdep spautolm()

Samar Deen sa462 at cornell.edu
Tue Apr 12 00:22:16 CEST 2016


I was running a CAR with different inputs into the function, but have a few
errors that I am unable to resolve.

CAR1 = spautolm(s2008 ~ 1, data = df4, listw = gab.nhbr.listw,
+ weights = 1, family = "CAR")
Error in model.frame.default(formula = s2008 ~ 1, data = df4, weights = 1,
 :
  variable lengths differ (found for '(weights)')

CAR1 = spautolm(s2008 ~ 1, data = df4, listw = gab.nhbr.listw,
+ weights = tempmean, family = "CAR")
Error in validObject(.Object) :
  invalid class “dgRMatrix” object: slot j is not increasing inside a column

Here in both cases I used a Delaunay Triangulation to determine neighbors
and also made the neighbors symmetric using the command:

nc_nb.tri <- make.sym.nb(nc_nb.tri)

In my code I also have other methods for determining the neighbors. I seem
to be getting different errors for all.

I would really appreciate any guidance on how I may resolve this error.

Any help will be really appreciated.

Thank you & Regards,

My code is pasted below:

library(Matrix)
library(spdep)
library(RColorBrewer)

#Get cents3
setwd("~/Desktop/Flounder")
cents3 <- read.csv("cents3.csv")

#Subset the relevant data
df=data.frame(cents3)
df2 <- subset(df, select = c(8,9,41,49,50))
df3 <- df2[complete.cases(df2), ]
df4 <- na.omit(df3)
coordinates(df4) = c("x","y")

##SET the neighbours
coords = coordinates(df4)
IDs = row.names(data.frame(df4))

# Delaunay Triangulation

nc_nb.tri = tri2nb(coords,row.names=IDs)
nc_nb.tri <- make.sym.nb(nc_nb.tri)
#
plot(df4, border = "grey")
plot(nc_nb.tri, coordinates(df4), add = TRUE, col = "blue")

# Sphere of Influence

nc_nb.soi = graph2nb(soi.graph(nc_nb.tri,coords),row.names=IDs)
nc_nb.soi <- make.sym.nb(nc_nb.soi)

#
plot(df4, border = "grey")
plot(nc_nb.soi, coordinates(df4), add = TRUE, col = "blue")

# Relative Graph
#
nc_nb.relative = graph2nb(relativeneigh(coords),row.names=IDs)
#
plot(df4, border = "grey")
plot(nc_nb.relative, coordinates(df4), add = TRUE, col = "blue")
#
# Neighbors based on distance (Here all neighbors withing 30 miles
# This one is a bit messy
nc_nb.dnn = dnearneigh(cbind(df4$x,df4$y),0,2,row.names=IDs)
#
plot(df4, border = "grey")
plot(nc_nb.dnn, coordinates(df4), add = TRUE, col = "blue")
#
# K nearest neighbors
#
nc_nb.knn = knn2nb(knearneigh(coordinates(df4),k=5),row.names=IDs)
nc_nb.knn <- make.sym.nb(nc_nb.knn)
#
plot(df4, border = "grey")
plot(nc_nb.knn, coordinates(df4), add = TRUE, col = "blue")
#

#7- Making Neighborhood Wieghts
#
# Compute distances between neighbors

gab.dist = nbdists(nc_nb.tri, cbind(df4$x, df4$y))
#
# Create a spatial neighbor object from weights object
# using inverse distance weighting, style B gives equal weights to the
nrihbors
gab.nhbr = listw2sn(nb2listw(nc_nb.tri, glist = gab.dist,
                             style = "U", zero.policy = TRUE))
#
# The distance between neighbor i and j
dij = gab.nhbr[, 3]
#
# SSTobserved
n = df4$tempmean
#
# Scaled inverse distance
el1 = min(dij)/dij
#
# Square root of ratio of central fish to neighbor fish quantities
#not needed
el2 = sqrt(n[gab.nhbr$to]/n[gab.nhbr$from])
#
# Combine distance and birth weightings
gab.nhbr$weights = el1
#
# Create spatial weights object from spatial neighbor object
gab.nhbr.listw = sn2listw(gab.nhbr)

gab.nhbr.listw$style <- "W"

#8- CAR with no predictor
#when wieght = 1 it will be propotional to the variance-
#since it is a standardized survey across the region we can assume it is 1

CAR1 = spautolm(s2008 ~ tempmean, data = df4, listw = gab.nhbr.listw,
               verbose = TRUE, family = "CAR", method = "Matrix_J")

CAR1 = spautolm(s2008 ~ 1, data = df4, listw = gab.nhbr.listw,
                weights = 1, family = "CAR")

summary(CAR1)

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list