[R] Help with spdep package issue with errorsarlm

Samantha Sifleet Sifleet.Samantha at epamail.epa.gov
Wed Jun 27 03:02:29 CEST 2012


   Hi List,
   I am hoping some of you have experience with the spdep package.

   I have had success with lm.morantest and lm.LMtests.

   I am trying to run a spatial error model and I keep getting the following
   error
   Error in res[i, listw$neighbours[[i]]] <- listw$weights[[i]] :
     NAs are not allowed in subscripted assignments
   I think it has something to do with the fact that some of my polygons have
   no neighbors.  I built my spatial weight matrix using nearest neighbor = 5
   I then re tried building my spatial weight matrix using threshold distance
   and I get the following error
   Error in validObject(.Object) :
     invalid class âdgRMatrixâ object: all column indices must be between 0 and
   ncol-1
   despite using the zero.policy = TRUE and na.omit in the code it seems to be
   NAs mucking up the process
   Any tips are most appreciated.
   Thanks!
   ##my code:
   rm(list=ls())
   library("spdep")
   setwd("E:/weights")
   deer<-read.csv("Deer_Appl_complete.txt", header = TRUE, skip = 1)
   summary(deer)
   deerwt<-read.gwt2nb("Deer_Appl_complete.GWT")
   attach(deer)
   gwt_weights_deer<-nb2listw(deerwt, style="W", zero.policy=TRUE)
   Deer_model_A<-lm(APP_SQMI~SQRT_OAK_D+SQRT_PUBLI+DEER_RANGE+MEDIAN_DIS+ROAD_D
   MU+MEAN_HSI)
   summary(Deer_model_A)
   Deer_model_A.res<-resid(Deer_model_A)
   Deer_model_A_moran<-lm.morantest(Deer_model_A,gwt_weights_deer,
   zero.policy=TRUE, alternative="two.sided")
   Deer_model_A_Lagrange<-lm.LMtests(Deer_model_A,      gwt_weights_deer,
   zero.policy=TRUE, test="all")
   SPER_Deer_Model_A<-errorsarlm(APP_SQMI~SQRT_OAK_D+SQRT_PUBLI+DEER_RANGE+MEDI
   AN_DIS+ROAD_DMU+MEAN_HSI,    data=deer,   gwt_weights_deer,   na.omit,
   zero.policy=TRUE)
   ##This is where it spits out
   Error in res[i, listw$neighbours[[i]]] <- listw$weights[[i]] :
     NAs are not allowed in subscripted assignments
   #Then I tried using a threshold distance spatial weight matrix
   deerwt_dis<-read.gwt2nb("Deer_Appl_distance.GWT")
   gwt_weights_dis<-nb2listw(deerwt_dis, style="W", zero.policy=TRUE)
   SPER_Deer_Model_A<-errorsarlm(APP_SQMI~SQRT_OAK_D+SQRT_PUBLI+DEER_RANGE+MEDI
   AN_DIS+ROAD_DMU+MEAN_HSI,    data=deer,    gwt_weights_dis,   na.omit,
   zero.policy=TRUE)
   ##This kicks out with the following error
   Error in validObject(.Object) :
     invalid class âdgRMatrixâ object: all column indices must be between 0 and
   ncol-1


More information about the R-help mailing list