[R-sig-Geo] Error in Spatial lag model
Miluji Sb
milujisb at gmail.com
Sun Nov 20 20:14:34 CET 2016
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)
E2_mod_pdssat_org_lon_lat <- E2_mod_pdssat_org[, c(2:3)] # The lon and lat
will be required
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
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.
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]]
More information about the R-sig-Geo
mailing list