Error in "lag.listw(listw, xt, ...) : object lengths differ" to estimate panel SLX model

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Error in "lag.listw(listw, xt, ...) : object lengths differ" to estimate panel SLX model

Asmiro
Dear Members,

I am trying to estimate spatial lagged explanatory (SLX) model with R.
I did the following codes.
teffdata <- read_dta("pdata.dta.dta")  # Loaded the dataset
coordinates(teffdata) <- ~Latitude + Longitude
mycoords <- coordinates(teffdata)
dis_rd <- rdist.earth(mycoords, miles = FALSE)
# relabel rows/columns
temp <- teffdata$id
str(temp)
row.names(dis_rd) <- temp
colnames(dis_rd) <- temp
#Create neighbor list based on inverse distance
for(i in 1:dim(dis_rd)[1]) {dis_rd[i,i] = 0} # renders exactly zero all
diagonal elements, where dim is "dimension"
dis_rd[dis_rd > 160] <- 0                   # all distances > 80km are set
to zero ==> Why I choose 20 miles??
dis_rd <- ifelse(dis_rd!=0, 1/dis_rd, dis_rd) # calculating the inverse
distances
# Create the spatial weights based on inverse distance
nbl <- mat2listw(dis_rd, style="W")  # create a (normalized) listw object
or number of neighbors list
# Then, I converted the data.frame into pdata.frame
pd  <- pdata.frame(as.data.frame(teffdata), index = c("ID","year"))
# then,
# Creating specific pdata.frame spatial vectors, for instance:
speduc  <- pd$Educ
spage <- pd$Age
spext  <- pd$Ext
spcredit < pd$Credit # could not create this variable, look the label of it
spfert <- pd$FertilUsage
spswC <- pd$SWC
spsow <- pd$SowingMethod
spdmkt <- pd$dist_market
sprain <- pd$MRainfall
sphhsize <- pd$HHSIZE
spslope <- pd$Slope
spfertil <- pd$SoilFertilStatus
spgender <- pd$Gender
spseedtype <- pd$SeedType
spaez <- pd$AEZ
sptlu <- pd$TLU

# Specifying a formula for these models:
model <- ineff_translog_time ~ Educ + Age + Ext + Credit + FertilUsage +
SWC + SowingMethod +
  dist_market + MRainfall + HHSIZE + Slope + SoilFertilStatus + Gender +
SeedType + AEZ + TLU +
  slag(speduc, listw = nbl) + slag(spage, listw = nbl) + slag(spext, listw
= nbl) +
  slag(spfert, listw = nbl) + slag(spswC, listw = nbl) + slag(spsow, listw
= nbl) +
  slag(spdmkt, listw = nbl) + slag(sprain, listw = nbl) + slag(spfertil,
listw = nbl) +
  slag(spseedtype)

# Applying plm and splm functions on the original data (data.table
data.frame object):
regslx<-plm(model,data= as.data.frame(pd),listw = nbl,index=c("ID","year"),
            model="within", effect="twoways",spatial.error="none",lag=F)
summary(regslx)
# Finally, I got an error Error in "lag.listw(listw, xt, ...) : object
lengths differ"
Note: number of observations = 858
          number of time periods = 2
          I applied inverse distance based spatial weight matrix

So, could you please somone help me where I am getting wrong?




*Kind Regards,*
*Asmiro Abeje Fikadu*
*MSc student in Uni-Bonn, Germany*
*Address: Hainstrasse 52 /Zi. 229*
*                   P.O.BOX:  53121, Bonn*
*                   Cell phone: 015214173450*

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Error in "lag.listw(listw, xt, ...) : object lengths differ" to estimate panel SLX model

Roger Bivand
Administrator
On Thu, 13 Jun 2019, asmiro Abeje Fikadu wrote:

> Dear Members,
>
> I am trying to estimate spatial lagged explanatory (SLX) model with R.
> I did the following codes.
> teffdata <- read_dta("pdata.dta.dta")  # Loaded the dataset
> coordinates(teffdata) <- ~Latitude + Longitude
> mycoords <- coordinates(teffdata)
> dis_rd <- rdist.earth(mycoords, miles = FALSE)
> # relabel rows/columns
> temp <- teffdata$id
> str(temp)
> row.names(dis_rd) <- temp
> colnames(dis_rd) <- temp
> #Create neighbor list based on inverse distance
> for(i in 1:dim(dis_rd)[1]) {dis_rd[i,i] = 0} # renders exactly zero all
> diagonal elements, where dim is "dimension"
> dis_rd[dis_rd > 160] <- 0                   # all distances > 80km are set
> to zero ==> Why I choose 20 miles??
> dis_rd <- ifelse(dis_rd!=0, 1/dis_rd, dis_rd) # calculating the inverse
> distances
> # Create the spatial weights based on inverse distance
> nbl <- mat2listw(dis_rd, style="W")  # create a (normalized) listw object
> or number of neighbors list

None of the above tells us much (IDW are usually a bad idea anyway) - you
need to re-cast this using a built-in data set for anyone to reproduce
your problem.

> # Then, I converted the data.frame into pdata.frame
> pd  <- pdata.frame(as.data.frame(teffdata), index = c("ID","year"))
> # then,
> # Creating specific pdata.frame spatial vectors, for instance:
> speduc  <- pd$Educ
> spage <- pd$Age
> spext  <- pd$Ext
> spcredit < pd$Credit # could not create this variable, look the label of it
> spfert <- pd$FertilUsage
> spswC <- pd$SWC
> spsow <- pd$SowingMethod
> spdmkt <- pd$dist_market
> sprain <- pd$MRainfall
> sphhsize <- pd$HHSIZE
> spslope <- pd$Slope
> spfertil <- pd$SoilFertilStatus
> spgender <- pd$Gender
> spseedtype <- pd$SeedType
> spaez <- pd$AEZ
> sptlu <- pd$TLU
>
> # Specifying a formula for these models:
> model <- ineff_translog_time ~ Educ + Age + Ext + Credit + FertilUsage +
> SWC + SowingMethod +
>  dist_market + MRainfall + HHSIZE + Slope + SoilFertilStatus + Gender +
> SeedType + AEZ + TLU +
>  slag(speduc, listw = nbl) + slag(spage, listw = nbl) + slag(spext, listw
> = nbl) +
>  slag(spfert, listw = nbl) + slag(spswC, listw = nbl) + slag(spsow, listw
> = nbl) +
>  slag(spdmkt, listw = nbl) + slag(sprain, listw = nbl) + slag(spfertil,
> listw = nbl) +
>  slag(spseedtype)
>

What is length(spseedtype) - is it nxT? What is length(nbl$neighbours) -
is it n? Did you forget to generate the nxT x nxT space-time weights by
hand, would that work better than splm::slag()? Without a reproducible
example (just use Produc), it is hard to tell.

Roger

> # Applying plm and splm functions on the original data (data.table
> data.frame object):
> regslx<-plm(model,data= as.data.frame(pd),listw = nbl,index=c("ID","year"),
>            model="within", effect="twoways",spatial.error="none",lag=F)
> summary(regslx)
> # Finally, I got an error Error in "lag.listw(listw, xt, ...) : object
> lengths differ"
> Note: number of observations = 858
>          number of time periods = 2
>          I applied inverse distance based spatial weight matrix
>
> So, could you please somone help me where I am getting wrong?
>
>
>
>
> *Kind Regards,*
> *Asmiro Abeje Fikadu*
> *MSc student in Uni-Bonn, Germany*
> *Address: Hainstrasse 52 /Zi. 229*
> *                   P.O.BOX:  53121, Bonn*
> *                   Cell phone: 015214173450*
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; e-mail: [hidden email]
https://orcid.org/0000-0003-2392-6140
https://scholar.google.no/citations?user=AWeghB0AAAAJ&hl=en

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway