Skip to content

Commit

Permalink
condition on spdep version for zero.policy
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbivand committed Nov 10, 2023
1 parent a64311a commit ea905de
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ importFrom(splines, interpSpline)
importFrom(LearnBayes, rwmetrop)
importFrom(nlme, fdHess)
importFrom(multcomp, glht)
importFrom("utils", "packageVersion")

##################
# ML SE
Expand Down
36 changes: 30 additions & 6 deletions R/predict.sarlm.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ predict.Sarlm <- function(object, newdata=NULL, listw=NULL, pred.type="TS", all.
W <- as(listw, "CsparseMatrix")
W <- W[region.id, region.id]
style <- listw$style
listw <- mat2listw(W, row.names = region.id, style = style, zero.policy=zero.policy) # re-normalize to keep the style
if (packageVersion("spdep") >= "1.3.1") {
listw <- mat2listw(W, row.names = region.id, style = style, zero.policy=zero.policy) # re-normalize to keep the style
} else {
listw <- mat2listw(W, row.names = region.id, style = style) # re-normalize to keep the style
}
rm(W) # avoid the use of a wrong W
}
}
Expand Down Expand Up @@ -247,7 +251,11 @@ predict.Sarlm <- function(object, newdata=NULL, listw=NULL, pred.type="TS", all.
W <- as(listw.mixed, "CsparseMatrix")
W <- W[region.id.mixed, region.id.mixed]
style <- listw.mixed$style
listw.mixed <- mat2listw(W, row.names = region.id.mixed, style = style, zero.policy=zero.policy) # re-normalize to keep the style
if (packageVersion("spdep") >= "1.3.1") {
listw.mixed <- mat2listw(W, row.names = region.id.mixed, style = style, zero.policy=zero.policy) # re-normalize to keep the style
} else {
listw.mixed <- mat2listw(W, row.names = region.id.mixed, style = style) # re-normalize to keep the style
}
rm(W) # avoid the use of a wrong W
}
}
Expand Down Expand Up @@ -540,7 +548,11 @@ predict.Sarlm <- function(object, newdata=NULL, listw=NULL, pred.type="TS", all.
for (i in 1:nrow(newdata)) {
region.id.temp <- c(region.id.data, region.id.newdata[i])
Wi <- W[region.id.temp, region.id.temp]
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
if (packageVersion("spdep") >= "1.3.1") {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
} else {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style) # re-normalize to keep the style
}
if (power)
Wi <- as(listwi, "CsparseMatrix")
Xi <- rbind(Xs, Xo[i,])
Expand All @@ -564,7 +576,11 @@ predict.Sarlm <- function(object, newdata=NULL, listw=NULL, pred.type="TS", all.
for (i in 1:nrow(newdata)) {
region.id.temp <- c(region.id.data, region.id.newdata[i])
Wi <- W[region.id.temp, region.id.temp]
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
if (packageVersion("spdep") >= "1.3.1") {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
} else {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style) # re-normalize to keep the style
}
Wi <- as(listwi, "CsparseMatrix")
Xi <- rbind(Xs, Xo[i,])
# compute TC1 for S and o units
Expand Down Expand Up @@ -599,7 +615,11 @@ predict.Sarlm <- function(object, newdata=NULL, listw=NULL, pred.type="TS", all.
for (i in 1:nrow(newdata)) {
region.id.temp <- c(region.id.data, region.id.newdata[i])
Wi <- W[region.id.temp, region.id.temp]
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
if (packageVersion("spdep") >= "1.3.1") {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
} else {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style) # re-normalize to keep the style
}
Wi <- as(listwi, "CsparseMatrix")
Xi <- rbind(Xs, Xo[i,])
is.data <- 1:length(ys)
Expand Down Expand Up @@ -633,7 +653,11 @@ predict.Sarlm <- function(object, newdata=NULL, listw=NULL, pred.type="TS", all.
for (i in 1:nrow(newdata)) {
region.id.temp <- c(region.id.data, region.id.newdata[i])
Wi <- W[region.id.temp, region.id.temp]
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
if (packageVersion("spdep") >= "1.3.1") {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style, zero.policy=zero.policy) # re-normalize
} else {
listwi <- mat2listw(Wi, row.names = region.id.temp, style = style) # re-normalize to keep the style
}
Wi <- as(listwi, "CsparseMatrix")
Xi <- rbind(Xs, Xo[i,])
# compute TC1 for S and o units
Expand Down
6 changes: 5 additions & 1 deletion vignettes/sids_models.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ n <- nc$BIR74
el1 <- min(dij)/dij
el2 <- sqrt(n[sids.nhbr$to]/n[sids.nhbr$from])
sids.nhbr$weights <- el1*el2
sids.nhbr.listw <- sn2listw(sids.nhbr, style="B", zero.policy=TRUE)
if (packageVersion("spdep") >= "1.3.1") {
sids.nhbr.listw <- sn2listw(sids.nhbr, style="B", zero.policy=TRUE)
} else {
sids.nhbr.listw <- sn2listw(sids.nhbr)
}
```

The first model (I) is a null model with just an intercept, the second
Expand Down

0 comments on commit ea905de

Please sign in to comment.