Skip to content

Commit c2a3899

Browse files
committed
Durbin factors code factor
1 parent 7f3276d commit c2a3899

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

R/ML_models.R

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,7 @@ errorsarlm <- function(formula, data = list(), listw, na.action, weights=NULL,
4646
warning("intercept-only model, Durbin invalid and set FALSE")
4747
Durbin <- FALSE
4848
}
49-
dcfact <- which(attr(attr(mf, "terms"), "dataClasses") == "factor")
50-
have_factor_preds <- FALSE
51-
if (length(dcfact) > 0) {
52-
have_factor_preds <- TRUE
53-
factnames <- names(dcfact)
54-
xlevels <- lapply(factnames, function(xnms) levels(mf[[xnms]]))
55-
names(xlevels) <- factnames
56-
}
49+
have_factor_preds <- have_factor_preds_mf(mf)
5750
#
5851
na.act <- attr(mf, "na.action")
5952
if (!is.null(na.act)) {
@@ -76,7 +69,8 @@ errorsarlm <- function(formula, data = list(), listw, na.action, weights=NULL,
7669
etype <- "emixed"
7770
if (have_factor_preds)
7871
warning("use of spatially lagged factors (categorical variables)\n",
79-
paste(factnames, collapse=", "), "\nis not well-understood")
72+
paste(attr(have_factor_preds, "factnames"), collapse=", "),
73+
"\nis not well-understood")
8074
}
8175
if (is.formula(Durbin)) etype <- "emixed"
8276
if (is.logical(Durbin) && !isTRUE(Durbin)) etype <- "error"
@@ -130,11 +124,12 @@ errorsarlm <- function(formula, data = list(), listw, na.action, weights=NULL,
130124
}
131125
dmf <- lm(Durbin, data1, na.action=na.fail,
132126
method="model.frame")
133-
Ddcfact <- which(attr(attr(dmf, "terms"), "dataClasses") == "factor")
134-
if (length(Ddcfact) > 0) {
135-
warning("use of spatially lagged factors (categorical variables)\n",
136-
paste(names(Ddcfact), collapse=", "), "\nis not well-understood")
137-
}
127+
formula_durbin_factors <- have_factor_preds_mf(dmf)
128+
if (formula_durbin_factors) {
129+
warning("use of spatially lagged factors (categorical variables)\n",
130+
paste(attr(formula_durbin_factors, "factnames"), collapse=", "),
131+
"\nis not well-understood")
132+
}
138133
# dmf <- lm(Durbin, data, na.action=na.action,
139134
# method="model.frame")
140135
fx <- try(model.matrix(Durbin, dmf), silent=TRUE)

R/SLX_WX.R

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,7 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
1818
if (attr(mt, "intercept") == 1 && !any(attr(mt, "factors") == 1)) {
1919
stop("intercept-only model, Durbin invalid")
2020
}
21-
dcfact <- which(attr(attr(mf, "terms"), "dataClasses") == "factor")
22-
have_factor_preds <- FALSE
23-
if (length(dcfact) > 0) {
24-
have_factor_preds <- TRUE
25-
factnames <- names(dcfact)
26-
xlevels <- lapply(factnames, function(xnms) levels(mf[[xnms]]))
27-
names(xlevels) <- factnames
28-
}
21+
have_factor_preds <- have_factor_preds_mf(mf)
2922
na.act <- attr(mf, "na.action")
3023
if (!inherits(listw, "listw")) stop("No neighbourhood list")
3124
if (listw$style == "M") warning("missing spatial weights style")
@@ -58,7 +51,8 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
5851
if (isTRUE(Durbin)) {
5952
if (have_factor_preds)
6053
warning("use of spatially lagged factors (categorical variables)\n",
61-
paste(factnames, collapse=", "), "\nis not well-understood")
54+
paste(attr(have_factor_preds, "factnames"), collapse=", "),
55+
"\nis not well-understood")
6256
WX <- create_WX(x, listw, zero.policy=zero.policy,
6357
prefix=prefix)
6458
} else if (is.formula(Durbin)) {
@@ -69,10 +63,11 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
6963
}
7064
dmf <- lm(Durbin, data1, na.action=na.fail,
7165
method="model.frame")
72-
Ddcfact <- which(attr(attr(dmf, "terms"), "dataClasses") == "factor")
73-
if (length(Ddcfact) > 0) {
66+
formula_durbin_factors <- have_factor_preds_mf(dmf)
67+
if (formula_durbin_factors) {
7468
warning("use of spatially lagged factors (categorical variables)\n",
75-
paste(names(Ddcfact), collapse=", "), "\nis not well-understood")
69+
paste(attr(formula_durbin_factors, "factnames"), collapse=", "),
70+
"\nis not well-understood")
7671
}
7772
# dmf <- lm(Durbin, data, na.action=na.action,
7873
# method="model.frame")

R/cat_durbin.R

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Copyright 2025 by Roger Bivand
2+
#
3+
4+
have_factor_preds_mf <- function(mf) {
5+
if (!inherits(mf, "data.frame") || is.null(attr(mf, "terms")))
6+
stop("mf not a model.frame")
7+
dcfact <- which(attr(attr(mf, "terms"), "dataClasses") == "factor")
8+
have_factor_preds <- FALSE
9+
if (length(dcfact) > 0) {
10+
have_factor_preds <- TRUE
11+
factnames <- names(dcfact)
12+
xlevels <- lapply(factnames, function(xnms) levels(mf[[xnms]]))
13+
names(xlevels) <- factnames
14+
attr(have_factor_preds, "xlevels") <- xlevels
15+
attr(have_factor_preds, "factnames") <- factnames
16+
}
17+
have_factor_preds
18+
}

0 commit comments

Comments
 (0)