[R-sig-Geo] including factors in the formula for spml?
Ariel Ortiz-Bobea
aortizbobea at arec.umd.edu
Tue May 29 02:00:37 CEST 2012
Hello,
I'm trying to include a "state-level time trend" in a panel model with
spatially correlated errors. I'm getting the following error when I run the
spml() function with a "factor" as part of the formula:
Error in solve.default(crossprod(xco), crossprod(xco, yco)) :
Lapack routine dgesv: system is exactly singular
It seems spml doesnt like factors in the formula (they are ok in the pml()
function). Do you have any suggestions on how to do this without having to
de-trend my data before the analysis or creating a bunch of
dummies+interactions "manually" (using the numeric class and not factors)?
Thanks in advance.
Ariel
--- Reproductible code below -----
# Load packages
library(spdep)
library(plm)
library(splm)
# Create simple spatial structure
data <- expand.grid(lon=1:10, lat=1:10 )
data$fips <- 1:dim(data)[1]
rownames(data)<- 1:dim(data)[1]
n<-dim(data)[1]
mylist <- vector('list', n)
for(i in 1:n) {
mylist[[i]] = Polygons(list(Polygon(
cbind( c(data[i,"lon"]-0.5, data[i,"lon"]-0.5, data[i,"lon"]+0.5,
data[i,"lon"]+0.5, data[i,"lon"]-0.5),
c(data[i,"lat"]-0.5,data[i,"lat"]+0.5,data[i,"lat"]+0.5,data[i,"lat"]-0.5,data[i,"lat"]-0.5)))),i)
}
polys <- SpatialPolygons(mylist, 1:n)
polys <- SpatialPolygonsDataFrame(polys, data)
# Create neighbor structure and plot it
coords <- coordinates(polys)
nb <- poly2nb(polys)
dlist <- nbdists(nb, coords)
idlist <- lapply(dlist, function(x) 1/x) # inverse distance
nbw <- nb2listw(nb, glist=idlist, style="W")
plot(polys)
plot(nb, coordinates(polys), add=T, col="red")
# Create artificial dataset for regression
years <- 2000:2010 # time periods
t <- length(years) # number of time periods
dataset <- data.frame( fips = polys at data$fips, year=rep(years,
each=n) )
dataset$x <- rnorm(n*t , rep(0,times=n) ) # covariate
dataset$a <- rep(rnorm(n,mean=2,sd=sqrt(3)), times=t) # fixed effect
dataset$state<- as.factor(floor(dataset$fips/10)) # create a "state" dummy
head(dataset)
# This is the unknown and true regression parameter to be estimated
beta = matrix(1, nrow=1, ncol=1)
# Generate error and implied y variable
e <- rnorm(t*n, rep(0,times=n))
dataset$y <- as.matrix(dataset[,c("x")]) %*% beta + dataset$a + e
# Run FE regression with plm: this works fine...
fit_plm <- plm(formula = y ~ x + state*(as.numeric(year) +
I(as.numeric(year)^2)), data = dataset, model="within", index =
c("fips","year"))
summary(fit_plm)
# Run FE regression with spatial error: crashes (apack routine dgesv: system
is exactly singular)
fit_spml <- spml(formula = y ~ x + state*(as.numeric(year) +
I(as.numeric(year)^2)), data = dataset, model="within",
effect=c("individual"), index = c("fips","year"), listw = nbw, lag = FALSE,
spatial.error = "b")
-----
Ariel Ortiz-Bobea
PhD Candidate in Agricultural & Resource Economics
University of Maryland - College Park
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/including-factors-in-the-formula-for-spml-tp7579094.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
More information about the R-sig-Geo
mailing list