[R-sig-Geo] Error in Spatial lag model

Roger Bivand Roger.Bivand at nhh.no
Sun Nov 20 21:05:26 CET 2016


On Sun, 20 Nov 2016, Miluji Sb wrote:

> Dear Roger,
>
> Thank you for your quick reply.
>
> #
> print(spatial_knn2listw_obj)
>
> Characteristics of weights list object:
> Neighbour list object:
> Number of regions: 10927
> Number of nonzero links: 21854
> Percentage nonzero weights: 0.01830329
> Average number of links: 2
> Non-symmetric neighbours list
>
> Weights style: W
> Weights constants summary:
>      n        nn    S0      S1    S2
> W 10927 119399329 10927 10376.5 44660
> #
>
> The data is actually cross-sectional, is my mistake converting it
> to pdata.frame? I have not tried the splm package, which is a mistake on my
> part. I will try that now. Thanks.
>

If the data are not a spatial panel, then do not use splm, it is not 
relevant. If you have NAs in the response, you will need to address that 
once you can fit a model with complete cases. You may not have NAs in the 
covariates. I guess that the mistake was slag() and pdata.frame(), but 
your example is no help at all. Are you copying poor code from the 
internet? Why use k=2 (seems very ill-judged) - knn are intrinsically 
asymmetric, why 2?

Roger

> Sincerely,
>
> Milu
>
> On Sun, Nov 20, 2016 at 8:29 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote:
>
>> On Sun, 20 Nov 2016, Miluji Sb wrote:
>>
>> I have a dataset as below (with NAs). I am trying to run a spatial lagged
>>> model
>>>
>>> ##
>>> E2_mod_pdssat <- as.data.frame(gcp_t_p[,c(2,3)])
>>> names(E2_mod_pdssat) <- c("Lon", "Lat")
>>> row.names(E2_mod_pdssat) <- NULL
>>>
>>> coords_matrix  <- as.matrix(E2_mod_pdssat) # create coordinate matrix
>>> IDs <- row.names(as(E2_mod_pdssat, "data.frame"))
>>>
>>> spatial_knn2nb_obj <- knn2nb(knearneigh(coords_matrix, k=2,
>>> longlat=TRUE),
>>> row.names=IDs)
>>>
>>> # Now convert the nb to listw for use in spatial regression call.
>>> # Spatial weights, illustrated with coding style "W" (row standardized)
>>>
>>> spatial_knn2listw_obj <- nb2listw(spatial_knn2nb_obj, glist=NULL,
>>> style="W",  zero.policy=TRUE)
>>>
>>
>> Did you examine this object? What did the print method tell you?
>>
>>
>>> E2_mod_pdssat_org_lon_lat <- E2_mod_pdssat_org[, c(2:3)] # The lon and lat
>>> will be required
>>>
>>
>> This object "_org" does not yet exist ...
>>
>> E2_mod_pdssat_org <- E2_mod_pdssat_org[, -c(2,3)]  # Remove the Lon, Lat
>>> columns that are not required
>>>
>>> E2_mod_pdssat_pdata_frame <- pdata.frame(E2_mod_pdssat_org) # Convert data
>>> frame to pdata.frame
>>>
>>>
>> So you omitted to say that the data are (unbalanced) panel data ...
>>
>> All goes well until here but when I try to convert the explanatory
>>> variables in the pdata.frame to spatial lagged variables
>>>
>>> E2_mod_pdssat_X_lagged <-
>>> as.data.frame(cbind(slag(E2_mod_pdssat_pdata_frame$tmean, listw =
>>> spatial_knn2listw_obj),
>>> slag(E2_mod_pdssat_pdata_frame$tmeansq, listw = spatial_knn2listw_obj),
>>> slag(E2_mod_pdssat_pdata_frame$pmean, listw = spatial_knn2listw_obj),
>>> slag(E2_mod_pdssat_pdata_frame$pmeansq, listw = spatial_knn2listw_obj)))
>>>
>>> I get the following error "Error in lag.listw(listw, xt) : object lengths
>>> differ". What am I doing wrong? I have also tried removing the NAs with
>>> complete.cases.
>>>
>>
>> You have defined weights for one object (possibly a cross section), and
>> then try to use them on a different object with a different number of rows.
>> If you really want to do this, you need a Kronecker product of the spatial
>> weights to take the panel structure of your data into account. Further, I
>> doubt strongly that you need to create WX manually, because spatial lags of
>> all the required variables are created by model fitting functions in the
>> splm package.
>>
>> Roger
>>
>>
>>> Any help will be greatly appreciated. Thank you.
>>>
>>> Sincerely,
>>>
>>> Milu
>>>
>>> ## Data
>>>
>>> E2_mod_pdssat_org <- dput(head(temp,5))
>>> structure(list(iso3 = structure(c(30L, 30L, 30L, 1L, 30L), .Label = c(".",
>>> ".M", "AFG", "AGO", "ALB", "ARE", "ARG", "ARM", "AUS", "AUT",
>>> "AZE", "BDI", "BEL", "BEN", "BFA", "BGD", "BGR", "BHS", "BIH",
>>> "BLR", "BLZ", "BOL", "BRA", "BRN", "BTN", "BWA", "CAF", "CAN",
>>> "CHE", "CHL", "CHN", "CIV", "CMR", "COD", "COG", "COL", "CRI",
>>> "CUB", "CYP", "CZE", "DEU", "DJI", "DNK", "DOM", "DZA", "ECU",
>>> "EGY", "ERI", "ESH", "ESP", "EST", "ETH", "FIN", "FJI", "FLK",
>>> "FRA", "GAB", "GBR", "GEO", "GHA", "GIN", "GNB", "GNQ", "GRC",
>>> "GRL", "GTM", "GUF", "GUY", "HND", "HRV", "HTI", "HUN", "IDN",
>>> "IND", "IRL", "IRN", "IRQ", "ISL", "ISR", "ITA", "JAM", "JOR",
>>> "JPN", "KAZ", "KEN", "KGZ", "KHM", "KIR", "KOR", "KWT", "LAO",
>>> "LBN", "LBR", "LBY", "LCA", "LKA", "LSO", "LTU", "LUX", "LVA",
>>> "MAR", "MDA", "MDG", "MEX", "MKD", "MLI", "MMR", "MNE", "MNG",
>>> "MOZ", "MRT", "MWI", "MYS", "NCL", "NER", "NGA", "NIC", "NLD",
>>> "NOR", "NPL", "NZL", "OMN", "PAK", "PAN", "PER", "PHL", "PNG",
>>> "POL", "PRI", "PRK", "PRT", "PRY", "QAT", "ROU", "RUS", "RWA",
>>> "SAU", "SDN", "SEN", "SJM", "SLB", "SLE", "SLV", "SOM", "SRB",
>>> "SUR", "SVK", "SVN", "SWE", "SWZ", "SYR", "TCD", "TGO", "THA",
>>> "TJK", "TKM", "TLS", "TUN", "TUR", "TWN", "TZA", "UGA", "UKR",
>>> "URY", "USA", "UZB", "VEN", "VNM", "VUT", "YEM", "ZAF", "ZMB",
>>> "ZWE"), class = "factor"), lon = c(-69L, -68L, -72L, -71L, -70L
>>> ), lat = c(-55L, -55L, -54L, -54L, -54L), tmean = c(NA, NA,
>>> 4.55847677595631,
>>> 4.06015710382516, 3.71010245901642), pmean = c(NA, NA, 2.45569896697998,
>>> 1.6384220123291, 1.43925595283508), lngdp = c(-0.102380074560642,
>>> -2.3195972442627, 0.0122363297268748, -0.483756244182587,
>>> -6.29744625091553
>>> ), tmeansq = c(NA, NA, 20.7797107696533, 16.4848766326904,
>>> 13.7648601531982
>>> ), pmeansq = c(NA, NA, 6.03045749664307, 2.68442678451538,
>>> 2.07145762443542
>>> )), .Names = c("iso3", "lon", "lat", "tmean", "pmean", "lngdp",
>>> "tmeansq", "pmeansq"), row.names = c("1", "2", "3", "4", "5"), class =
>>> "data.frame")
>>>
>>>         [[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
>>
>

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