Skip to content

Commit d49c76d

Browse files
committed
add warnings to SD tests, including ordered factors
1 parent 9afa7f5 commit d49c76d

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

NAMESPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ importFrom(stats, influence.measures, lag, punif, lm, var, integrate,
1212
"model.frame", "model.matrix", "model.response", "na.fail",
1313
"na.omit", "naresid", "optimise", "printCoefmat", "resid",
1414
"terms", "uniroot", "weighted.residuals", "weights", "median",
15-
"pbinom")
15+
"pbinom", "C")
1616

1717
importFrom(deldir, deldir)
1818
importFrom(boot, boot)

R/SD.RStests.R

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ is.formula <- function(x){
55
inherits(x,"formula")
66
}
77

8-
create_X0 <- function(X, listw, Durbin=TRUE, data=NULL, na.act=NULL) {
8+
create_X0 <- function(X, listw, Durbin=TRUE, data=NULL, na.act=NULL, have_factor_preds=FALSE) {
99
if (isTRUE(Durbin)) {
10+
if (have_factor_preds) warn_factor_preds(have_factor_preds)
1011
n <- NROW(X)
1112
m <- NCOL(X)
1213
# check if there are enough regressors
@@ -43,6 +44,9 @@ create_X0 <- function(X, listw, Durbin=TRUE, data=NULL, na.act=NULL) {
4344
}
4445
dmf <- lm(Durbin, data1, na.action=na.fail,
4546
method="model.frame")
47+
formula_durbin_factors <- have_factor_preds_mf(dmf)
48+
if (formula_durbin_factors)
49+
warn_factor_preds(formula_durbin_factors)
4650
# dmf <- lm(Durbin, data, na.action=na.action,
4751
# method="model.frame")
4852
X0 <- try(model.matrix(Durbin, dmf), silent=TRUE)
@@ -106,14 +110,19 @@ SD.RStests <- function(model, listw, zero.policy=attr(listw, "zero.policy"), tes
106110
warning("Spatial weights matrix not row standardized")
107111

108112
if (is.formula(Durbin)) {
109-
dt <- try(eval(model$call[["data"]]), silent=TRUE)
113+
dt <- try(as.data.frame(model$model), silent=TRUE)
114+
# dt <- try(eval(model$call[["data"]]), silent=TRUE)
110115
if (inherits(dt, "try-error") || !is.data.frame(dt))
111-
stop("data object used to fit linear model not available for formula Durbin")
116+
stop("model.frame object used to fit linear model not available for formula Durbin")
117+
112118
}
113119

114-
y <- model.response(model.frame(model))
115-
X <- model.matrix(terms(model), model.frame(model))
116-
X0 <- create_X0(X=X, listw=listw, Durbin=Durbin, data=dt, na.act=na.act)
120+
mf <- model.frame(model)
121+
y <- model.response(mf)
122+
X <- model.matrix(terms(model), mf)
123+
have_factor_preds <- have_factor_preds_mf(mf)
124+
X0 <- create_X0(X=X, listw=listw, Durbin=Durbin, data=dt, na.act=na.act,
125+
have_factor_preds=have_factor_preds)
117126
yhat <- as.vector(fitted(model))
118127
p <- model$rank
119128
p1 <- 1:p

R/cat_durbin.R

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,35 @@ have_factor_preds_mf <- function(mf) {
1414
names(xlevels) <- factnames
1515
attr(have_factor_preds, "xlevels") <- xlevels
1616
attr(have_factor_preds, "factnames") <- factnames
17+
pred_contrasts <- character(length(factnames))
18+
pred_ordered <- logical(length(factnames))
19+
for (pred in seq(along=factnames)) {
20+
contr <- C(mf[[factnames[pred]]])
21+
pred_contrasts[pred] <- attr(contr, "contrasts")
22+
pred_ordered[pred] <- names(attr(contr, "contrasts")) == "ordered"
23+
}
24+
names(pred_contrasts) <- names(pred_ordered) <- factnames
25+
attr(have_factor_preds, "pred_contrasts") <- pred_contrasts
26+
attr(have_factor_preds, "pred_ordered") <- pred_ordered
1727
}
1828
have_factor_preds
1929
}
2030

2131
warn_factor_preds <- function(x) {
22-
warning("use of spatially lagged factors (categorical variables)\n",
32+
plural <- length(attr(x, "factnames")) > 1L
33+
warning("use of spatially lagged ", ifelse(plural, "factors", "factor"),
34+
" (categorical ", ifelse(plural, "variables", "variable"), "\n",
2335
paste(attr(x, "factnames"), collapse=", "),
2436
"\nis not well-understood")
37+
pred_ordered <- attr(x, "pred_ordered")
38+
if (any(pred_ordered)) {
39+
pred_contrasts <- attr(x, "pred_contrasts")
40+
ordered <- which(pred_ordered)
41+
plural <- length(ordered) > 1L
42+
warning("In addition ", ifelse(plural, "variables", "variable"), ":\n",
43+
paste(names(pred_ordered)[ordered], collapse=", "),
44+
"\n", ifelse(plural, "are", "is"),
45+
" ordered (ordinal) with contrasts:\n",
46+
paste(pred_contrasts[ordered], collapse=", "))
47+
}
2548
}

inst/tinytest/test_Durbin_factor.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library(spdep)
2+
data(oldcol)
3+
lw <- nb2listw(COL.nb)
4+
COL.OLD$fEW <- factor(COL.OLD$EW)
5+
COL.OLD$fDISCBD <- ordered(cut(COL.OLD$DISCBD, c(0, 1.5, 3, 4.5, 6)))
6+
f <- formula(CRIME ~ INC + HOVAL + fDISCBD*fEW)
7+
lm_obj <- lm(f, data=COL.OLD)
8+
expect_warning(COL.SD0 <- SD.RStests(lm_obj, lw, test="SDM", Durbin=TRUE))
9+
expect_warning(COL.SD1 <- SD.RStests(lm_obj, lw, test="SDM", Durbin=~ INC + HOVAL + fDISCBD*fEW))
10+
expect_warning(COL.SD2 <- SD.RStests(lm_obj, lw, test="SDM", Durbin=~ INC + HOVAL + fEW))
11+
expect_silent(COL.SD3 <- SD.RStests(lm_obj, lw, test="SDM", Durbin=~ INC + HOVAL))
12+
expect_warning(COL.SDE0 <- SD.RStests(lm_obj, lw, test="SDEM", Durbin=TRUE))
13+
expect_warning(COL.SDE1 <- SD.RStests(lm_obj, lw, test="SDEM", Durbin=~ INC + HOVAL + fDISCBD*fEW))
14+
expect_warning(COL.SDE2 <- SD.RStests(lm_obj, lw, test="SDEM", Durbin=~ INC + HOVAL + fEW))
15+
expect_silent(COL.SDE3 <- SD.RStests(lm_obj, lw, test="SDEM", Durbin=~ INC + HOVAL))
16+

0 commit comments

Comments
 (0)