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

Roger Bivand Roger.Bivand at nhh.no
Tue Apr 12 10:53:45 CEST 2016


On Tue, 12 Apr 2016, Samar Deen wrote:

> 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)')

This is trivial, input weights must be of the same length as the number of 
observations.

> weights <- 1
> length(weights)
[1] 1

... more below

>
> 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.

(I have access to the data from an earlier offline exchange involving 
issues with missing data)

The points are (most likely) in geographical coordinates, so almost all 
of your attempts to create neighbour objects are wrong - all graph methods 
are only on the plane. Distances can be used when longlat=TRUE only.

Your use of scaled inverted spatial weights is contorted and the declared 
listw object style wrong, this ought to be sufficient:

gab.dist.sinv <- lapply(gab.dist, function(x) min(unlist(gab.dist))/x)

Much seems to have been copied from a script detailing analysis of the NC 
SIDS data set (the 30 mile threshold was used there).

It isn't obvious what you are trying to do.

Roger

>
> 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]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo

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


More information about the R-sig-Geo mailing list