Skip to content

Commit 4f85d37

Browse files
committed
begin outfactoring of create_Durbin
1 parent 8c8ec9b commit 4f85d37

File tree

2 files changed

+95
-2
lines changed

2 files changed

+95
-2
lines changed

R/SLX_WX.R

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
4646
if (is.null(weights)) weights <- rep(as.numeric(1), n)
4747
if (any(is.na(weights))) stop("NAs in weights")
4848
if (any(weights < 0)) stop("negative weights")
49-
dvars <- c(NCOL(x), 0L)
49+
# dvars <- c(NCOL(x), 0L)
50+
if (!(isTRUE(Durbin) || is.formula(Durbin))) {
51+
stop("Durbin argument neither TRUE nor formula")
52+
} else {
53+
if (Sys.getenv("SPATIALREG_CREATE_DURBIN") == "") {
5054
prefix <- "lag"
5155
if (isTRUE(Durbin)) {
5256
if (have_factor_preds) warn_factor_preds(have_factor_preds)
@@ -94,7 +98,7 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
9498
wxn <- substring(colnames(WX), nchar(prefix)+2,
9599
nchar(colnames(WX)))
96100
zero_fill <- length(xn) + (which(!(xn %in% wxn)))
97-
} else stop("Durbin argument neither TRUE nor formula")
101+
}
98102
dvars <- c(NCOL(x), NCOL(WX))
99103
if (is.formula(Durbin)) {
100104
attr(dvars, "f") <- Durbin
@@ -104,6 +108,21 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
104108
}
105109
x <- cbind(x, WX)
106110
rm(WX)
111+
} else { # SPATIALREG_CREATE_DURBIN
112+
res <- create_Durbin(Durbin=Durbin,
113+
have_factor_preds=have_factor_preds, x=x, listw=listw,
114+
zero.policy=zero.policy, data=data, na.act=na.act)
115+
x <- res$x
116+
dvars <- res$dvars
117+
inds <-attr(dvars, "inds")
118+
xn <- attr(dvars, "xn")
119+
wxn <- attr(dvars, "wxn")
120+
zero_fill <- attr(dvars, "zero_fill")
121+
formula_durbin_factors <- attr(dvars, "formula_durbin_factors")
122+
attr(dvars, "xn") <- NULL
123+
attr(dvars, "wxn") <- NULL
124+
}
125+
}
107126
# WX <- create_WX(x, listw, zero.policy=zero.policy, prefix="lag")
108127
# x <- cbind(x, WX)
109128
# 180128 Mark L. Burkey summary.lm error for SlX object
@@ -437,4 +456,67 @@ create_WX <- function(x, listw, zero.policy=NULL, prefix="") {
437456
WX
438457
}
439458

459+
create_Durbin <- function(Durbin, have_factor_preds, x, listw, zero.policy,
460+
data, na.act) {
461+
prefix <- "lag"
462+
if (isTRUE(Durbin)) {
463+
if (have_factor_preds) warn_factor_preds(have_factor_preds)
464+
WX <- create_WX(x, listw, zero.policy=zero.policy,
465+
prefix=prefix)
466+
} else if (is.formula(Durbin)) {
467+
data1 <- data
468+
if (!is.null(na.act) && (inherits(na.act, "omit") ||
469+
inherits(na.act, "exclude"))) {
470+
data1 <- data1[-c(na.act),]
471+
}
472+
dmf <- lm(Durbin, data1, na.action=na.fail,
473+
method="model.frame")
474+
formula_durbin_factors <- have_factor_preds_mf(dmf)
475+
if (formula_durbin_factors)
476+
warn_factor_preds(formula_durbin_factors)
477+
# dmf <- lm(Durbin, data, na.action=na.action,
478+
# method="model.frame")
479+
fx <- try(model.matrix(Durbin, dmf), silent=TRUE)
480+
if (inherits(fx, "try-error"))
481+
stop("Durbin variable mis-match")
482+
WX <- create_WX(fx, listw, zero.policy=zero.policy,
483+
prefix=prefix)
484+
inds <- match(substring(colnames(WX), 5,
485+
nchar(colnames(WX))), colnames(x))
486+
if (anyNA(inds)) {
487+
wna <- which(is.na(inds)) #TR: continue if Durbin has intercept, but formula has not
488+
if (length(wna) == 1 && grepl("Intercept", colnames(WX)[wna])
489+
&& attr(terms(formula), "intercept") == 0
490+
&& attr(terms(Durbin), "intercept") == 1) {
491+
inds <- inds[-wna]
492+
} else{
493+
stop("WX variables not in X: ",
494+
paste(substring(colnames(WX), 5,
495+
nchar(colnames(WX)))[is.na(inds)], collapse=" "))
496+
}
497+
}
498+
icept <- grep("(Intercept)", colnames(x))
499+
iicept <- length(icept) > 0L
500+
if (iicept) {
501+
xn <- colnames(x)[-1]
502+
} else {
503+
xn <- colnames(x)
504+
}
505+
wxn <- substring(colnames(WX), nchar(prefix)+2,
506+
nchar(colnames(WX)))
507+
zero_fill <- length(xn) + (which(!(xn %in% wxn)))
508+
}
509+
dvars <- c(NCOL(x), NCOL(WX))
510+
if (is.formula(Durbin)) {
511+
attr(dvars, "f") <- Durbin
512+
attr(dvars, "inds") <- inds
513+
attr(dvars, "xn") <- xn
514+
attr(dvars, "wxn") <- wxn
515+
attr(dvars, "zero_fill") <- zero_fill
516+
attr(dvars, "formula_durbin_factors") <- formula_durbin_factors
517+
}
518+
x <- cbind(x, WX)
519+
rm(WX)
520+
list(x=x, dvars=dvars)
521+
}
440522

inst/tinytest/test_Durbin_factor.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@ lw <- spdep::nb2listw(COL.nb)
44
COL.OLD$fEW <- factor(COL.OLD$EW)
55
COL.OLD$fDISCBD <- ordered(cut(COL.OLD$DISCBD, c(0, 1.5, 3, 4.5, 6)))
66
f <- formula(CRIME ~ INC + HOVAL + fDISCBD*fEW)
7+
Sys.setenv("SPATIALREG_CREATE_DURBIN"="")
78
expect_warning(COL.SLX0 <- lmSLX(f, data=COL.OLD, lw, Durbin=TRUE))
89
expect_warning(COL.SLX1 <- lmSLX(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL + fDISCBD*fEW))
910
expect_warning(COL.SLX2 <- lmSLX(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL + fEW))
1011
expect_silent(COL.SLX3 <- lmSLX(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL))
12+
Sys.setenv("SPATIALREG_CREATE_DURBIN"="0")
13+
expect_warning(COL.SLX0a <- lmSLX(f, data=COL.OLD, lw, Durbin=TRUE))
14+
expect_warning(COL.SLX1a <- lmSLX(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL + fDISCBD*fEW))
15+
expect_warning(COL.SLX2a <- lmSLX(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL + fEW))
16+
expect_silent(COL.SLX3a <- lmSLX(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL))
17+
Sys.setenv("SPATIALREG_CREATE_DURBIN"="")
18+
expect_true(isTRUE(all.equal(COL.SLX0, COL.SLX0a)))
19+
expect_true(isTRUE(all.equal(COL.SLX1, COL.SLX1a)))
20+
expect_true(isTRUE(all.equal(COL.SLX2, COL.SLX2a)))
21+
expect_true(isTRUE(all.equal(COL.SLX3, COL.SLX3a)))
1122
expect_warning(COL.err0 <- errorsarlm(f, data=COL.OLD, lw, Durbin=TRUE))
1223
expect_warning(COL.err1 <- errorsarlm(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL + fDISCBD*fEW))
1324
expect_warning(COL.err2 <- errorsarlm(f, data=COL.OLD, lw, Durbin=~ INC + HOVAL + fDISCBD))

0 commit comments

Comments
 (0)