Skip to content

Commit

Permalink
update to SlX object call and summary/print methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbivand committed Jan 17, 2024
1 parent 8c2ef1e commit 5c12770
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ export(lmSLX, create_WX)

S3method(impacts, SlX)
S3method(predict, SlX)
S3method(print, SlX)
S3method(summary, SlX)
S3method(print, summary.SlX)

# SF/ME
export(SpatialFiltering, ME)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Version 1.3-2 (development)

* pass through SlX formula in call

* re-corrected #19 because the fitted model weights component may be NULL

* suppress warning from `multcomp::glht` as the test which throws the warning is discarded
Expand Down
35 changes: 34 additions & 1 deletion R/SLX_WX.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
subset <- !(1:length(listw$neighbours) %in% na.act)
listw <- subset(listw, subset, zero.policy=zero.policy)
}


d_name <- deparse(substitute(data))
listw_name <- deparse(substitute(listw))

y <- model.response(mf, "numeric")
y_name <- as.character(mt)[2]
if (any(is.na(y))) stop("NAs in dependent variable")
x <- model.matrix(mt, mf)
if (any(is.na(x))) stop("NAs in independent variable")
Expand Down Expand Up @@ -198,11 +202,40 @@ lmSLX <- function(formula, data = list(), listw, na.action, weights=NULL, Durbin
attr(lm.model, "mixedImps") <- mixedImps
attr(lm.model, "dvars") <- dvars
if (is.formula(Durbin)) attr(lm.model, "Durbin") <- deparse(Durbin)
if (is.null(weights)) attr(lm.model, "weights") <- weights
tms <- as.character(lm.model$terms)
attr(lm.model, "SLX_call") <- paste(y_name, " ", tms[1], " ", tms[3],
", data = ", d_name, ", listw = ", listw_name, sep="")
class(lm.model) <- c("SlX", class(lm.model))
lm.model
}


print.SlX <- function(x, digits = max(3L, getOption("digits") - 3L), ...) {
x$ocall <- x$call
if (!is.null(attr(x, "SLX_call")))
x$call <- str2lang(paste("lm(", attr(x, "SLX_call"), ")", sep=""))
class(x) <- "lm"
invisible(print(x, digits=digits, ...))
}

summary.SlX <- function(object, correlation = FALSE, symbolic.cor = FALSE, ...) {
class(object) <- "lm"
sum_obj <- summary(object, correlation=correlation, symbolic.cor=symbolic.cor, ...)
class(sum_obj) <- "summary.SlX"
sum_obj
}


print.summary.SlX <- function(x, digits = max(3L, getOption("digits") - 3L), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...) {
x$ocall <- x$call
if (!is.null(attr(x, "SLX_call")))
x$call <- str2lang(paste("lm(", attr(x, "SLX_call"), ")", sep=""))
class(x) <- "lm"
invisible(print(x, digits=digits, symbolic.cor=symbolic.cor, signif.stars=signif.stars, ...))
}


predict.SlX <- function(object, newdata, listw, zero.policy=NULL, ...) {
if (is.null(zero.policy))
zero.policy <- get("zeroPolicy", envir = .spatialregOptions)
Expand Down
13 changes: 11 additions & 2 deletions man/SLX.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
\alias{print.WXimpact}
\alias{summary.WXimpact}
\alias{print.summary.WXimpact}
\alias{print.SlX}
\alias{summary.SlX}
\alias{print.summary.SlX}
\alias{predict.SlX}

\title{Spatial Durbin linear (SLX, spatially lagged X) model}
Expand All @@ -14,12 +17,14 @@
\usage{
lmSLX(formula, data = list(), listw, na.action, weights=NULL, Durbin=TRUE,
zero.policy=NULL)
create_WX(x, listw, zero.policy=NULL, prefix="")
\method{print}{SlX}(x, digits = max(3L, getOption("digits") - 3L), ...)
\method{summary}{SlX}(object, correlation = FALSE, symbolic.cor = FALSE, ...)
\method{print}{summary.SlX}(x, digits = max(3L, getOption("digits") - 3L), symbolic.cor = x$symbolic.cor, signif.stars = getOption("show.signif.stars"), ...)
\method{impacts}{SlX}(obj, ...)
\method{print}{WXimpact}(x, ...)
\method{summary}{WXimpact}(object, ..., adjust_k=(attr(object, "type") == "SDEM"))
\method{predict}{SlX}(object, newdata, listw, zero.policy=NULL, ...)

create_WX(x, listw, zero.policy=NULL, prefix="")
}

\arguments{
Expand All @@ -33,6 +38,10 @@ is called.}
\item{weights}{an optional vector of weights to be used in the fitting process. Non-NULL weights can be used to indicate that different observations have different variances (with the values in weights being inversely proportional to the variances); or equivalently, when the elements of weights are positive integers w_i, that each response y_i is the mean of w_i unit-weight observations (including the case that there are w_i observations equal to y_i and the data have been summarized) - \code{\link{lm}}}
\item{Durbin}{default TRUE for \code{lmSLX} (Durbin model including WX); if TRUE, full spatial Durbin model; if a formula object, the subset of explanatory variables to lag}
\item{zero.policy}{default NULL, use global option value; if TRUE assign zero to the lagged value of zones without neighbours, if FALSE assign NA}
\item{digits}{the number of significant digits to use when printing}
\item{correlation}{logical; if \code{TRUE}, the correlation matrix of the estimated parameters is returned and printed}
\item{symbolic.cor}{logical. If \code{TRUE}, print the correlations in a symbolic form (see 'symnum') rather than as numbers}
\item{signif.stars}{logical. If \code{TRUE}, 'significance stars' are printed for each coefficient}
\item{obj}{A spatial regression object created by \code{lmSLX}}
\item{\dots}{Arguments passed through}
\item{prefix}{default empty string, may be \dQuote{lag} in some cases}
Expand Down

0 comments on commit 5c12770

Please sign in to comment.