【问题标题】:spatial panel regression in R: non conformable spatial weights?R中的空间面板回归:非整合空间权重?
【发布时间】:2018-03-13 12:26:03
【问题描述】:

我正在尝试使用 splm 包在 R 中运行空间面板回归。所以我有多边形随着时间的推移汇总数据,我想看看因变量如何受到其他变量的影响,这些变量也随着时间而变化。

我有 546 个区域,其中包含许多变量,但为了测试它的工作原理,我使用了 3 个多边形的数据子集,包括用于计算权重的 shapefile 和数据。

https://drive.google.com/file/d/0B4SK0f2zZUKxZ0dDU2lnclB2M3c/view?usp=sharing

#load data
file="sector_panel_data_test.csv"
sector_data=read.table(file,sep=",", header=T, quote="")
sector_data[is.na(sector_data)] <- 0
names(sector_data)
attach(sector_data)

#load shape
require (rgdal)
sectors <-readOGR(dsn=".",layer="sectors_test_sample_year1")
nb <- poly2nb(sectors)


#distance based neighbors
coords <- coordinates(sectors)
nb.d125<- dnearneigh(coords,0,125000,row.names=sectors$Code)

#create weights matrix
mat.d125 <-nb2mat(nb.d125,glist=NULL,style="W",zero.policy=TRUE)

#and then a weights list object
listd125 = mat2listw(mat.d125, style="W")

#design model and run, just picked one variable here
fm <- prop_fdeg ~ mean_pop
randommodel <-spml(fm, 
data=sector_data,index=NULL,listw=listFQQ,model="random", lag=FALSE)

我收到以下错误:

spreml 中的错误(公式 = 公式,数据 = 数据,索引 = 索引,w = listw2mat(listw),:不符合的空间权重

有人知道这是什么意思吗?我到处搜索,只发现有同样问题的人在寻找解决方案。

【问题讨论】:

    标签: r regression panel spatial


    【解决方案1】:

    这可能与您的问题无关,但希望可以帮助其他人搜索此错误。

    数据必须采用特定格式:前两列按此顺序包含indextime,其余为剩余变量。切换timeindex 将导致Non conformable spatial weights,因为dim(w) != n,其中$n$ 将是time 的唯一元素的数量。

    【讨论】:

      【解决方案2】:

      我也刚刚收到同样的错误。在使用我的数据逐步浏览源代码后(见下文),面板数据中的缺失似乎导致某些行的列表删除。反过来,这些删除的行将导致面板数据和 listw 对象具有不同数量的观察值。要解决此问题,您需要 (1) 估算丢失的数据,(2) 从 listw 对象中删除已删除的行,或 (3) 从模型中删除缺失的变量。就我而言,估算所有丢失的数据似乎可以阻止错误。您还需要注意保持面板数据平衡,因为 splm 也会破坏大多数不平衡的面板数据(plm 中的make.pbalanced 命令似乎对解决数据不平衡没有太大帮助,因为它将添加带有 splm 将拒绝的 NA 的行。

      检查缺失、估算缺失数据和/或查看数据在源代码中的工作方式的几种方法:

      1. 比较dim(your_data)dim(na.omit(your_data))

      2. 使用 naniar 可视化面板数据中的缺失(另请参阅新的 panelView 包)

        install.packages("naniar") # visualise missing data 
        library(ggplot2)
        library(naniar)
        gg_miss_var(your_data)  
        
      3. 对您的数据运行plm(不是splm)并检查输出中数据的维度(与原始数据相比)。

        p_out <- plm(formula = your_formula, data = your_data, model = "within") 
        summary(p_out)
        dim(model.matrix(p_out)) 
        
      4. 一种直接估算缺失数据的方法是使用 simputation 包。更多信息请见https://cran.r-project.org/web/packages/simputation/vignettes/intro.html

      5. Amelia 包为使用时间序列、横截面数据的多重插补提供了更好的选择:https://gking.harvard.edu/amelia

      6. 直接在R-Forge 运行spreml 的底层代码。首先,查看下面的代码表明错误是由最后一行生成的。就在这一行之上,我们可以看到 n 是如何定义的,这至少暗示了一些可能的调试途径(通过直接运行底层代码来查看 dim(w) 与 n 不同的地方(w &lt;- your_listw_object)。

        ## data management through plm functions
        pmod <- plm(formula, data, index=index, model="pooling")
        X <- model.matrix(pmod)
        y <- pmodel.response(pmod)
        
        #names(index) <- row.names(data)
        #ind <- index[which(names(index) %in% row.names(X))]
        #tind <- tindex[which(names(index) %in% row.names(X))]
        
        ind <- attr(pmod$model, "index")[, 1]
        tind <- attr(pmod$model, "index")[, 2]
        oo <- order(tind, ind)
        X <- X[oo, , drop=FALSE]
        y <- y[oo]
        ind <- ind[oo]
        tind <- tind[oo]
        n <- length(unique(ind))
        k <- dim(X)[[2]]
        t <- max(tapply(X[, 1], ind, length))
        nT <- length(ind)
        
        ## check compatibility of weights matrix
        if (dim(w)[[1]] != n) stop("Non conformable spatial weights")
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 1970-01-01
        • 1970-01-01
        • 2014-05-24
        • 2021-01-03
        • 2018-05-30
        • 1970-01-01
        相关资源
        最近更新 更多