@@ -46,7 +46,11 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
46
46
if (is.null(weights )) weights <- rep(as.numeric(1 ), n )
47
47
if (any(is.na(weights ))) stop(" NAs in weights" )
48
48
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" ) == " " ) {
50
54
prefix <- " lag"
51
55
if (isTRUE(Durbin )) {
52
56
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
94
98
wxn <- substring(colnames(WX ), nchar(prefix )+ 2 ,
95
99
nchar(colnames(WX )))
96
100
zero_fill <- length(xn ) + (which(! (xn %in% wxn )))
97
- } else stop( " Durbin argument neither TRUE nor formula " )
101
+ }
98
102
dvars <- c(NCOL(x ), NCOL(WX ))
99
103
if (is.formula(Durbin )) {
100
104
attr(dvars , " f" ) <- Durbin
@@ -104,6 +108,21 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
104
108
}
105
109
x <- cbind(x , WX )
106
110
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
+ }
107
126
# WX <- create_WX(x, listw, zero.policy=zero.policy, prefix="lag")
108
127
# x <- cbind(x, WX)
109
128
# 180128 Mark L. Burkey summary.lm error for SlX object
@@ -437,4 +456,67 @@ create_WX <- function(x, listw, zero.policy=NULL, prefix="") {
437
456
WX
438
457
}
439
458
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
+ }
440
522
0 commit comments