Modify SPDEP package - insert new function
<p>I am trying to modify the stsls function of the R package spdep.
The function compute a spatial autoregressive function using a two stage least square. For both stages, the function uses the same spatial matrix.
What I want is to create a new function, say stslsm, which uses two different spatial matrices, one for the first stage (inlistw), one for the second stage (listw).</p>

<p>For this reason, I took the stsls function, I added a new entry, inlistw, and modified accordingly the script. For those who are interested, the code is at the bottom of this post, but please consider that this is just a first trial.</p>

<p>Now the problem is that I don't know how to insert this new function in the spdep package.
I read different posts on this issue. The most recurrent suggestion is:</p>

<pre><code>unlockBinding("spdep", loadNamespace("spdep"));
assignInNamespace("stslsm", stslsm, ns=asNamespace("spdep"), envir=loadNamespace("spdep"));
assign("stslsm", stslsm, envir=env);
lockBinding(stslsm, loadNamespace("spdep"));
</code></pre>

<p>But after the second line of code I get the following </p>

<pre><code>Error in bindingIsLocked(x, ns) : no binding for "stslsm"
</code></pre>

<p>I got stuck here. Do you have any suggestion? </p>

<pre><code>function (formula, data = list(), listw, inlistw,zero.policy = NULL, 
      na.action = na.fail, robust = FALSE, HC = NULL, legacy = FALSE, 
      W2X = TRUE) {
if (!inherits(listw, "listw")) 
stop("No neighbourhood list")
if (is.null(zero.policy)) 
zero.policy &lt;- get("zeroPolicy", envir = .spdepOptions)
stopifnot(is.logical(zero.policy))
if (class(formula) != "formula") 
formula &lt;- as.formula(formula)
mt &lt;- terms(formula, data = data)
mf &lt;- lm(formula, data, na.action = na.action, method = "model.frame")
na.act &lt;- attr(mf, "na.action")
if (!is.null(na.act)) {
subset &lt;- !(1:length(listw$neighbours) %in% na.act)
listw &lt;- subset(listw, subset, zero.policy = zero.policy)
}
y &lt;- model.extract(mf, "response")
if (any(is.na(y))) 
stop("NAs in dependent variable")
X &lt;- model.matrix(mt, mf)
if (any(is.na(X))) 
stop("NAs in independent variable")
if (robust) {
if (is.null(HC)) 
  HC &lt;- "HC0"
if (!any(HC %in% c("HC0", "HC1"))) 
  stop("HC must be one of HC0, HC1")
}
Wy &lt;- lag.listw(listw, y, zero.policy = zero.policy)
dim(Wy) &lt;- c(nrow(X), 1)
colnames(Wy) &lt;- c("Rho")
n &lt;- NROW(X)
m &lt;- NCOL(X)
xcolnames &lt;- colnames(X)
K &lt;- ifelse(xcolnames[1] == "(Intercept)", 2, 1)
 if (m &gt; 1) {
 WX &lt;- matrix(nrow = n, ncol = (m - (K - 1)))
if (W2X) 
  WWX &lt;- matrix(nrow = n, ncol = ncol(WX))
for (k in K:m) {
  wx &lt;- lag.listw(inlistw, X[, k], zero.policy = zero.policy)
  if (W2X) 
    wwx &lt;- lag.listw(inlistw, wx, zero.policy = zero.policy)
  if (any(is.na(wx))) 
    stop("NAs in lagged independent variable")
  WX[, (k - (K - 1))] &lt;- wx
  if (W2X) 
    WWX[, (k - (K - 1))] &lt;- wwx
}
if (W2X) 
  inst &lt;- cbind(WX, WWX)
 else inst &lt;- WX
}
if (K == 2 &amp;&amp; listw$style != "W") {
wx1 &lt;- as.double(rep(1, n))
wx &lt;- lag.listw(inlistw, wx1, zero.policy = zero.policy)
if (W2X) 
  wwx &lt;- lag.listw(inlistw, wx, zero.policy = zero.policy)
if (m &gt; 1) {
  inst &lt;- cbind(wx, inst)
  if (W2X) 
    inst &lt;- cbind(wwx, inst)
}
else {
  inst &lt;- matrix(wx, nrow = n, ncol = 1)
  if (W2X) 
    inst &lt;- cbind(inst, wwx)
}
}
result &lt;- tsls(y = y, yend = Wy, X = X, Zinst = inst, robust = robust, 
             HC = HC, legacy = legacy)
result$zero.policy &lt;- zero.policy
result$robust &lt;- robust
if (robust) 
result$HC &lt;- HC
result$legacy &lt;- legacy
result$listw_style &lt;- listw$style
result$call &lt;- match.call()
class(result) &lt;- "stsls"
result
}
</code></pre>