[R] different dimensions in W and my data in moran and spatial model

maria jesus herrerias mjherrer|@@t @end|ng |rom gm@||@com
Wed Apr 14 23:57:59 CEST 2021


Hello everyone,

    I am writing my code in R to run the spatial models. I am learning this
at the moment.
    I have my data for 116 countries over the period 1990-2014,
    and I got the shapefile from the website below.
    The objective is to get the matrix to run the spatial models.
    In the code below, I can get the matrix for the world and also for the
subsample.
    But when I try to run the moran test or sar model I get an error.
    The error is related to the dimension of W (listw) and my dataset. So
any help,
    I would be grateful.

    Below my code just in case someone can help  here.

    # Load the required Packages.

    `library(spdep)          # Spatial Dependence: Weighting Schemes,
Statistics
    `library(rgdal)          # to read shapefiles

    #shapefiles (world map from diva-gis)

       # Read the panel data from stata format for the regression analysis
below.

    `library(foreign)

    `mydata <- read.dta("C:/Users/Usuario/Desktop/instituciones/revision
energy economics/
     spatial  panel in stata/countries_shp/neworiginal.dta")

    `fix(mydata)
    `attach(mydata)

    my data contains 116 countries from 1990 to 2014 in panel.

    # Prepare the dataset for panel data analysis in R.

    `library(plm)

    `mydata <- pdata.frame(mydata, index = c("id", "year"))


    # Read the shapefile and set up the working directory.

    `setwd("C:/Users/Usuario/Desktop/instituciones/revision energy
economics/spatial panel in stata/countries_shp")

    `worlddata <- readOGR("countries.shp")
    `names(worlddata)
    `summary(worlddata)

    # Get the centroids

    `coords <-coordinates(worlddata)

    A. Contiguity based relations

     1. First Order Queen Contiguity

    `queen.nb = poly2nb(worlddata, queen=TRUE)
    `summary(queen.nb)
    `plot(queen.nb,coords)


    # the numbers of neighbours of regions in the neighbours list.

    `cards <- card(queen.nb)

    # convert into a listw type based on contiguity row normalized

    `queen.listw <- nb2listw(queen.nb,style="W", zero.policy = TRUE)
    `summary(queen.listw,zero.policy=TRUE)

    #marginal effects
    `impacts(reg2, listw = listw1,zero.policy=TRUE)

    #marginal effects with p-values
    `summary(impacts(reg2, listw = listw1), R= 500, zstats =
TRUE,zero.policy=TRUE)

    `attributes(queen.listw)

     #Selection of a subset of countries to fit with mydataset (116
countries, the previous ones I did for all countries in the shapefile)


    `setwd("C:/Users/Usuario/Desktop/instituciones/revision energy
economics/spatial panel in stata/countries_shp")

    `worlddata <- readOGR("countries.shp")

    `queen.nb = poly2nb(worlddata, queen=TRUE)

    `coords <-coordinates(worlddata)

    `plot(queen.nb, coords)


    Following Roger Bivand example:

    `to.be.dropped <- c(1, 2, 5, 6, 8, 9, 10, 13, 14, 18, 20, 21, 23, 26,
28, 29, 31, 34, 36, 37, 39, 40, 41, 45, 46, 47, 48, 51, 52, 53, 55, 56, 58,
59, 62, 63, 64, 66, 68, 69, 74, 75, 77, 78, 79, 80, 81, 82, 85, 86, 87, 89,
90, 94, 95, 97, 98, 99, 100, 102, 103, 104, 105, 107, 109, 110, 118, 123,
124, 125, 127, 130, 131, 133, 134, 137, 138, 139, 140, 143, 145, 146, 148,
149, 151, 152, 153, 155, 157, 159, 161, 162, 166, 167, 170, 173, 175, 176,
177, 178, 181, 182, 183, 184, 186, 190, 193, 194, 195, 198, 199, 200, 201,
202, 203, 204, 205, 206, 207, 208, 211, 212, 213, 215, 216, 218, 219, 221,
222, 224, 226, 227, 228, 229, 232, 233, 237, 239, 240, 242, 246, 247, 248,
255, 258, 259, 260, 261, 265)
    sub.queen.nb <- subset(queen.nb,
      !(1:length(queen.nb) %in% to.be.dropped))
    which(!(attr(queen.nb, "region.id") %in%
      attr(sub.queen.nb, "region.id")))

    `sub.queen.listw <- nb2listw(sub.queen.nb,style="W", zero.policy = TRUE)

    so now, I have my 116 countries in the queen matrix.

    # My data and variables
    `y <- cbind(lei)
    `x <- cbind(lgdppcnew, lgdppcnew2, industryg, importsg, fdig)
    `xy <- cbind(mydata$x, mydata$y)
    `listw1 <- sub.queen.listw
    `coords <- coords

    #Define formula

    `reg.ols <- y ~ x


    # Autocorrelation test
    `lm.morantest(ols.eq1,listw1,zero.policy=TRUE)
    `plot.moran(ols.eq1,listw1)

    (here I get the error where it is said the objects have different
dimensions)

    `reg2 <- lagsarlm(ols.eq1, data = mydata, listw1, zero.policy=TRUE)
    `summary(reg2,correlation=TRUE, Nagelkerke=TRUE, Hausman=TRUE,
zero.policy=TRUE)

    (here I get the error where it is said the objects have different
dimensions)

     #marginal effects

    `impacts(reg2, listw = listw1,zero.policy=TRUE)

     #marginal effects with p-values

     `summary(impacts(reg2, listw = listw1), R= 500, zstats =
TRUE,zero.policy=TRUE)

     I don't know if it is for my panel data, the missing values or
something else.
     I tried also in Stata having similar issues.

     thanks in advance and my apologies if it is very naive question, but I
am stuck.

all the best
Maria Jesus

	[[alternative HTML version deleted]]



More information about the R-help mailing list