[R] Help with spdep package issue with errorsarlm
Roger Bivand
Roger.bivand at nhh.no
Thu Jun 28 13:19:10 CEST 2012
Samantha Sifleet <Sifleet.Samantha <at> epamail.epa.gov> writes:
>
>
> 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.
Please consider using R-sig-geo, which is a more focussed list for this kind of
question.
Please also always include the output of traceback() run following the error,
and the output of sessionInfo(), in case your system or packages are not updated
and that the problem has already been solved.
>From your code and the k-nearest neighbours scheme, it appears that nb2mat() on
your imported GWT file is finding problems that were not found by lm.morantest()
or lm.LMtests(), that is that some listw$neighbours[[i]] are NAs. It isn't
obvious how the imported distance neighbours scheme is failing, unless your
reported code is given omitting the methods= argument.
Since the problems are coming from your imported weights, why not simply
generate the same objects in R, using knn2nb(knearneigh()), or dnearneigh()?
Roger
> 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