通过重复测量使不正确的邻居适应空间回归

时间:2017-08-03 18:41:55

标签: r model spatial neighbours distance-matrix

在构建邻居距离矩阵之后,我试图拟合空间滞后模型(spdep::lagsarlm)。我有两个问题,因为每次我读到它时,模型总是适合每个空间位置只有一个观察(一行)的数据。

我的数据集每个空间点的观察数量可变(但它不是时间数据)我想知道这样做是否有效,特别是在创建时距离矩阵,因为我收到警告:

Warning message:
In spdep::knearneigh(., k = 3, longlat = F) :
  knearneigh: identical points found

事实上,当我绘制邻居关系时,我得到了一个错误的图形(我猜这个算法认为重复的点是自己的邻居,所以它们被隔离);当我只过滤第一个小节时,情节就可以了。

library(sp); library(spdep);set.seed(12345678)
df = data.frame('id'=rep(1:10, 3),
  'x'=rep(rnorm(10, 48, 0.1), 3),
  'y'=rep(rnorm(10, 2.3, 0.05),3),
  'response'=c(rnorm(5), rnorm(20, 1), rnorm(5)),
  'type.sensor'=rep(c(rep("a", 6), rep("b", 4)), 3))
coordinates(df)<-c("x", "y")
w <-  df %>% spdep::knearneigh(k=3, longlat=F) %>% knn2nb 
plot(w, coordinates(df))
df2 = head(df, 10) # I keep only the first measure for each location
w2 <-  df2 %>% spdep::knearneigh(k=3, longlat=F) %>% knn2nb 
plot(w2, coordinates(df2))

enter image description here

所以我对第一个案例lagsarlm模型的结果不太自信..

lagsarlm(response ~ type.sensor, data=df, listw=nb2listw(w), type = "lag" )
lagsarlm(response ~ type.sensor, data=df, listw=nb2listw(w2), type = "lag" )

但是,如果我尝试使用较大的数据集来拟合我的模型,但是使用正确的邻居矩阵,则会抱怨

Error in lagsarlm(response ~ type.sensor, data = df, listw = nb2listw(w2),  : 
  Input data and weights have different dimensions

我怎样才能处理这些数据呢?感谢。

0 个答案:

没有答案