[R-sig-Geo] error using predict.sarlm

Roger Bivand Roger.Bivand at nhh.no
Thu Apr 23 10:20:27 CEST 2009


On Wed, 22 Apr 2009, evans324 at umn.edu wrote:

> Hi Roger,
>
> Thanks for the response. Unfortunately, I'm still getting the same error 
> running predict.sarlm ("Error in if (nrow(newdata) != 
> length(listw$neighbours)) stop("mismatch between newdata and spatial 
> weights") : argument is of length zero"). Here's what I've done since.

OK, see below for analysis

>
>> Thanks for providing the verbatim code. If this is the verbatim code, then 
>> newdata = "dak_test_data" is saying that newdata is not the data.frame 
>> object called dak_test_data, but a single element character vector - that 
>> is, nrow("dak_test_data") is NULL. However, this does not trigger the error 
>> message as such, so it could also be that you wrote newdata = 
>> dak_test_data, and nrow(dak_test_data) is not equal to 
>
> I removed quotes, but still get the same error. The dak_test_data file was 
> imported using readShapePoly. I also tried importing the dataset using 
> read.dbf, but still got the same error using the resulting data frame.

Then the dak_test_data is a SpatialPolygonsDataFrame, for which there is 
no nrow() method, leading to problems. Please get around this by saying 
newdata=as(dak_test_data, "data.frame") to coerce it to data frame. I'll 
look at providing dim methods for Spatial*DataFrame objects other than 
SpatialPointsDataFrame, which are in sp already; nrow() just returns 
dim()[1] of the object.

Thanks,

Roger

>
>> However, this does not trigger the error message as such, so it could also 
>> be that you wrote newdata = dak_test_data, and nrow(dak_test_data) is not 
>> equal to length(dak_test.10nn.listw) - or here nrow(coords), which is the 
>> test.
>
> As you suggested, nrow(dak_test_data)is NULL and thus is not equal to 
> length(dak_test.10nn.listw), which equals 3. nrow(coords) = 2255. Here's the 
> sequence of code that went into creating these weights:
>
> coords<-coordinates(dak_test_data)
> rn<-sapply(slot(dak_test_data, "polygons"), function(x) slot(x, "ID"))
> dak_test.10nn <- knearneigh(coords, k=10)
> dak_test.10nn.nb <- knn2nb(dak_test.10nn, row.names=rn, sym=TRUE)
> dak_test.10nn.listw <- nb2listw(dak_test.10nn.nb, zero.policy=TRUE)
>
> When I ran this using the imported dbf, I used the same weights since I'm not 
> aware of a way to create weights without tracing back to a shapefile. The 
> data frame in this case had nrow=2255. I still received the same error 
> message.
>
> I'd appreciate any help you can offer in solving this problem.  Thanks.
>
> Heather Sander 
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list