inlabru-org / inlabru

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

replicate ar1 error #55

Closed ASeatonSpatial closed 3 years ago

ASeatonSpatial commented 5 years ago

Error when attempting to define a replicate of an ar1 model component.

Running the following code with the current backend development branch and INLA version 18.07.12 returns an error:

library(INLA)
library(inlabru)

# Simulate data - 3 groups with same AR1 param
n = 100
rho <- 0.8
x1 = arima.sim(n=n, model=list(ar=c(rho)))
x2 = arima.sim(n=n, model=list(ar=c(rho)))
x3 = arima.sim(n=n, model=list(ar=c(rho)))

# Poisson observations
y1 = rpois(n, lambda = exp(x1))
y2 = rpois(n, lambda = exp(x2))
y3 = rpois(n, lambda = exp(x3))

df_groups <- data.frame(y = c(y1, y2, y3),
                        t = rep(1:n, 3))
r <- rep(1:3, each = n)

fml <- y ~ Intercept + myar1(map = t, model = "ar1", replicate = r, n = n, values = 1:n)
fit <- bru(fml, family = "poisson", data = df_groups)

This returns:

Error in parse.input.list(list(effects[[k]]), input.ncol(A[[k]]), paste("Effect block ", : Effect block 2: Mismatching row sizes: 300, n.A=100

This is attempting to fit a model with 3 replicates of n = 100 each.

Andy

finnlindgren commented 3 years ago

This bug is fixed since at least 2.1.15:

> fml <- y ~ Intercept + myar1(t, model = "ar1", replicate = r)
> fit <- bru(fml, family = "poisson", data = df_groups)
> summary(fit)
inlabru version: 2.1.15.9000
INLA version: 20.12.10
Components:
  Intercept: Model types main='linear', group='exchangeable', replicate='iid'
  myar1: Model types main='ar1', group='exchangeable', replicate='iid'
Likelihoods:
  Family: 'poisson'
    Data class: 'data.frame'
    Predictor: y ~ .
Time used:
    Pre = 0.212, Running = 0.577, Post = 0.168, Total = 0.957 
Fixed effects:
            mean    sd 0.025quant 0.5quant 0.975quant   mode kld
Intercept -0.242 0.236      -0.72   -0.237      0.213 -0.229   0

Random effects:
  Name    Model
    myar1 AR1 model

Model hyperparameters:
                     mean    sd 0.025quant 0.5quant 0.975quant  mode
Precision for myar1 0.431 0.084      0.285    0.425      0.613 0.414
Rho for myar1       0.702 0.050      0.598    0.705      0.792 0.708

Expected number of effective parameters(stdev): 154.04(7.24)
Number of equivalent replicates : 1.95 

Deviance Information Criterion (DIC) ...............: 894.60
Deviance Information Criterion (DIC, saturated) ....: 426.16
Effective number of parameters .....................: 145.77

Watanabe-Akaike information criterion (WAIC) ...: 869.96
Effective number of parameters .................: 91.34

Marginal log-Likelihood:  -551.29 
Posterior marginals for the linear predictor and
 the fitted values are computed