首页 文章

R中的空间面板回归:不一致的空间权重?

提问于
浏览
3

我试图用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),:不一致的空间权重

有谁知道这意味着什么?我到处搜索,只发现有同样问题的人在寻找解决方案 .

1 回答

  • 1

    我需要(1)插入缺失的数据,(2)从listw对象中删除丢弃的行,或者(3)从模型中删除缺失的变量 . 在我的情况下,输入所有丢失的数据似乎停止了错误 . 您还需要注意保持面板数据 balancer ,因为splm也会破坏大多数不 balancer 的面板数据( plm 中的 make.pbalanced 命令似乎没有帮助解决数据中的不 balancer 问题,因为它会添加行 splm 将拒绝的NA .

    一些方法可以检查缺失,估算丢失的数据和/或查看数据在源代码中的工作方式:

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

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

    install.packages("naniar") # visualise missing data 
    library(ggplot2)
    library(naniar)
    gg_miss_var(your_data)
    
    • 对数据运行 plm (而不是 splm )并检查输出中数据的大小(与原始数据相比) .
    p_out <- plm(formula = your_formula, data = your_data, model = "within") 
    summary(p_out)
    dim(model.matrix(p_out))
    
    • 使用simputation包来直接计算缺失数据的方法 . 欲了解更多,请参阅https://cran.r-project.org/web/packages/simputation/vignettes/intro.html

    • Amelia软件包提供了更好的多重插补选项,包括时间序列,横截面数据:https://gking.harvard.edu/amelia

    • 直接在R-Forge运行spreml的底层代码 . 首先,查看下面的代码表明错误是由最后一行生成的 . 在这一行之上,我们可以看到n是如何定义的,这至少表明了一些可能的调试途径(通过直接运行底层代码来查看dim(w)与n(其中 w <- 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")
    

相关问题