inlabru-org / inlabru

inlabru
https://inlabru-org.github.io/inlabru/
90 stars 21 forks source link

Problem with `summary.component` when fitting model with weights #251

Closed vpnsctl closed 1 day ago

vpnsctl commented 1 month ago

Hi Finn and inlabru team,

We were fitting a simple model:

library(INLA) 
library(inlabru)

#Simulate 20 x 5 data
maturity <- 1:5
dates <- 1:20

prec_true <- c(0.5, 0.3,0.4)

cor_true <- c(0.5, 0.3, 0.7)

beta1 <- arima.sim(list(order=c(1,0,0), ar=cor_true[1]), n=length(dates), mean=0, sd = 1/sqrt(prec_true[1]))
beta2 <- arima.sim(list(order=c(1,0,0), ar=cor_true[2]), n=length(dates), mean = 0, sd = 1/sqrt(prec_true[2]))
beta3 <- arima.sim(list(order=c(1,0,0), ar=cor_true[3]), n=length(dates), mean = 0, sd = 1/sqrt(prec_true[3]))

lambda <- 0.2

sim <- beta1 + beta2 * (1 - exp(-lambda * maturity[1]))/(lambda * maturity[1]) + beta3 *((1 - exp(-lambda * maturity[1]))/(lambda * maturity[1]) - exp(-lambda * maturity[1])) + 0.1 * rnorm(length(beta1))

for(i in 2:5){
  sim <- c(sim, sim <- beta1 + beta2 * (1 - exp(-lambda * maturity[i]))/(lambda * maturity[i]) + beta3 *((1 - exp(-lambda * maturity[i]))/(lambda * maturity[i]) - exp(-lambda * maturity[i])) + 0.1 * rnorm(length(beta1)))
}

#Parameters
f1 <- seq(1, 1, length = length(maturity))
f2 <- (1 - exp(-lambda * maturity)) / (lambda * maturity)
f3 <- ((1 - exp(-lambda * maturity)) / (lambda * maturity) - exp(-lambda * maturity))

n <- 20
k <- 5
m <- n
nd <- n * k
md <- m * k

F1 <- matrix(f1, k, m)
F2 <- matrix(f2, k, m)
F3 <- matrix(f3, k, m)

F1t=double(md)
F1t[1:md]=as.vector(t(F1)) 
F2t=double(md) 
F2t[1:md]=as.vector(t(F2)) 
F3t=double(nd) 
F3t[1:md]=as.vector(t(F3))

ix=rep(1:m,k) 
Yff <- sim

id <- id1 <- id2 <- ix

mu1=F1t
mu2=F2t 
mu3=F3t

mdata <- data.frame(Y = Yff, dates = rep(1:m, length(maturity)), maturity = rep(maturity, each= m), 
                    idf = ix, id1f = ix, id2f = ix, 
                    mu1f = mu1, mu2f = mu2, mu3f = mu3, 
                    F1tf = F1t, F2tf = F2t, F3tf = F3t)

test_formula <- Y ~ -1 + Intercept(1) + f1(idf, F1tf,   model="ar1",constr=T)+ 
  f2(id1f, F2tf, model="ar1",constr=T) + f3(id2f, F3tf, model="ar1",constr=T) + 
  f4(mu1f,model="linear") + f5(mu2f,model="linear") + f6(mu3f,model="linear")  

test_res <- bru(test_formula,  data = mdata, options = list(verbose=TRUE))

summary(test_res)

and this summary gives an error. By digging a bit, we can see the problem here: in this term (and actually all the ones with weights)

test_res[["bru_info"]][["model"]][["effects"]][["f1"]][["weights"]]

the "input" element only has one "level", meaning that we have (with the commented part being the output):

test_res[["bru_info"]][["model"]][["effects"]][["f1"]][["weights"]][["input"]]
# F1tf

So, when we do

test_res[["bru_info"]][["model"]][["effects"]][["f1"]][["weights"]][["input"]][["input"]]

it is trying to subset an element of class "name", and gives that error.

We can see that it is indeed trying to do the subsetting [["input"]][["input"]] (two "input"), by looking at summary.component, in which we have this snippet:

lapply(c("main", "group", "replicate", "weights"), function(x) {
        if (is.null(object[[x]][["input"]][["input"]])) {
            NULL
        } else {
           paste0( x,  " = ", object[[x]][["type"]], 
                "(",  deparse(object[[x]][["input"]][["input"]]), ")")
                }
      })

So, for weights this does not seem to be a valid subsetting.

Thanks for considering this issue.

Best, Alexandre

finnlindgren commented 1 month ago

Hi, what version of inlabru? I believe this was fixed as part of the general summary&print method expansion in version 2.11.1.9002. Can you please verify that by checking against the current github devel version (2.11.1.9016)? We'll then leave this issue open until the next CRAN release, in case someone else runs into the same issue.

vpnsctl commented 1 month ago

Perfect! Confirmed!! Really solved. Thank you very much!!! Edit: Just adding the version that had the problem. It was, indeed, CRAN version: 2.11.1.

finnlindgren commented 1 day ago

Closing, as inlabru 2.12.0 is on its way onto CRAN.