"slm" model using rgeneric

76 views
Skip to first unread message

lyh...@gmail.com

unread,
Feb 29, 2024, 9:27:16 AM2/29/24
to R-inla discussion group
Hi INLA group,

Can I use “rgeneric” to code “slm” model?

Considering the following spatial lag model, taking the form
y=\rhoy+x\beta+e and e is a tiny noise term.
E(y)=(I-rhoW)^-1 x\beta
and 
Var(y) = \sigma^2[(I-\rhoW)^T(I-\rhoW)] ^-1, so the precision is written
Prec(y)=\tau[(I-\rhoW)^T(I-\rhoW)]

I checked the "generic" vignette and found it can be used for a model with a mean structure.
However, I can not figure out how to code this term (I-rhoW)^-1 x\beta  in generic, furthermore, theta contains four hyperparameters, how to code them?
And what about the Graph part in the code?
I copy my code below, but it does not run well.
Could you give me some hints to improve it?

Thanks in advance
Charles

'inla.rgeneric.SLM.model' <- function(
    cmd = c("graph", "Q", "mu", "initial", "log.norm.const",
            "log.prior", "quit"),
    theta = NULL) {
 
  prec.high = exp(15)
 
  #Internal function
  interpret.theta <- function() {
    return(
      list(prec = exp(theta[1L]),
           rho = 1 / (1 + exp(-theta[2L]))),
           a = theta[3L],
           b = theta[4L]
    )
  }
 
  graph = function() {
    G = Diagonal(n = length(x), x=1)
    return(G)
  }
 
  Q <- function() {
    require(Matrix)
   
    param <- interpret.theta()
   
    return(param$prec * (Diagonal(nrow(W), x = 1) - param$rho * W) %*% t(Diagonal(nrow(W), x = 1) - param$rho * W))
  }
 
  mu <- function()
  {
    par = interpret.theta()
    return( solve(Diagonal(nrow(W), x = 1) - param$rho * W) * (par$a + par$b * x))
    }
 
  log.norm.const <- function() {
    return(numeric(0))
   
  }
 
  log.prior <- function() {
    param = interpret.theta()
    val =
      (dgamma(param$prec, 1, .1, log = T) + log(param$prec) +
      log(1) + log(param$rho) + log(1 - param$rho) +
      dnorm(par$a, mean=0, sd=1, log=TRUE) +
      dnorm(par$b, mean=0, sd=1, log=TRUE))
   
   
    return(val)
  }
 
  initial <- function() {
    return(rep(0, 2))
  }
 
  quit <- function() {
    return(invisible())
  }
 
  if (is.null(theta))
    theta <- initial()
 
  res <- do.call(match.arg(cmd), args = list())
  return(res)
}


















Virgilio Gómez-Rubio

unread,
Mar 1, 2024, 3:12:47 AM3/1/24
to lyh...@gmail.com, R-inla discussion group
Hi,

The slm model has already been implemented in INLA:



In the first paper you will find details about the implementation. In your implementation (I checked it very quickly) I believe that the graph definition is wrong as it should have non-zero entries in the places where the precision matrix is non-zero. Also, some of these models have been implemented using the generic0 latent effect:


Finally, please check the documentation of the slmlaten effect in INLA: inla.doc(“slm”)

Best,

Virgilio

lyh...@gmail.com

unread,
Mar 1, 2024, 8:38:46 AM3/1/24
to R-inla discussion group
Hi Prof. Gómez-Rubio,

Thank you for your reply.

I have read these papers and well understand that a spatial lag model can be rewritten as a latent Gaussian model with all covariates being random effects.

But I have no idea how to implement it by myself, especially how to code both covariates coupled with the spatial strength parameter(\rho) being random effects?
The fact is that I want to extend these spatial lag models.

I have a presumptuous request I wonder if you can give a similar implementation of "slm" using rgeneric?

Best wishes
Charles

Håvard Rue

unread,
Mar 1, 2024, 9:05:51 AM3/1/24
to lyh...@gmail.com, R-inla discussion group
using 'rgeneric' would be the easy way out.

make sure to test first on simpler models and go through the vignette
which discuss how to use it (and also use 'inla.rgeneric.q`)



On Fri, 2024-03-01 at 05:38 -0800, lyh...@gmail.com wrote:
> Hi Prof.Gómez-Rubio,
> --
> You received this message because you are subscribed to the Google
> Groups "R-inla discussion group" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to r-inla-discussion...@googlegroups.com.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/r-inla-discussion-group/2101aca9-06de-49e3-9ea1-83ddc38a0108n%40googlegroups.com
> .

--
Håvard Rue
hr...@r-inla.org

Reply all
Reply to author
Forward
0 new messages