How can I calculate the standard deviation of the linear.predictor from the standard deviation of the model components?

33 views
Skip to first unread message

JJ Hubbard

unread,
Mar 15, 2023, 6:23:13 PM3/15/23
to R-inla discussion group
If I have a model like so

m <- inla(target ~ 1 +
    fixed_effect1 +
    fixed_effect2 +

    f(random_effect1, model="iid") +
    f(random_effect2, model="iid"),

    data=df,
    control.compute=list(return.marginals=FALSE, config=FALSE),
    control.inla=list(strategy="adaptive", int.strategy=auto),
    verbose=F
)

I know that m$summary.linear.predictor[i,]$mean is equivalent to taking the fixed effect coefficients from m$summary.fixed$mean, multiplying them with the value of their covariate, and adding these with the coefficients from m$summary.random$mean.

How do the standard deviations of the fixed and random effects combine to create m$summary.linear.predictor$sd?

Helpdesk (Haavard Rue)

unread,
Mar 16, 2023, 3:44:30 AM3/16/23
to JJ Hubbard, R-inla discussion group
the mean is as you described but the variance/sd needs to account for
dependence between variables in the sum.

PS: with a recent version (and R-4.2) then arguments
control.inla=list(strategy="adaptive", int.strategy=auto)
are not needed
> --
> 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/0c328017-f362-47b5-b2a9-af2555f9fd9en%40googlegroups.com
> .

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

JJ Hubbard

unread,
Mar 16, 2023, 11:28:26 AM3/16/23
to R-inla discussion group
I take it this is not possible then, given it is likely very difficult to know dependence relations?

Helpdesk (Haavard Rue)

unread,
Mar 17, 2023, 3:44:28 PM3/17/23
to JJ Hubbard, R-inla discussion group
but the the sd for the linear predictor is already computed, or better
still, the marginals as well, I do not understand you question. sorry.

JJ Hubbard

unread,
Mar 17, 2023, 5:16:40 PM3/17/23
to R-inla discussion group
I mistakenly responded only to you so I am copying your response back here for others:

JJ Hubbard: "My use case is I would like to be able to calculate the mean and standard deviation of the posterior of an example not in the data inla was fit to. For example, a new observation with different values of the covariates than in the training data."

Haavard: " Add those to the model with NA data"

Adding the additional data as NA is what I am currently doing, but I am reaching the point where fitting my model takes a very long time as I add more and more data. So I am hoping to refit the model periodically when a large amount of new data is available, and just predict on values before then by reconstructing the posterior this way.

Havard Rue

unread,
Mar 17, 2023, 5:38:29 PM3/17/23
to R-inla discussion group, JJ Hubbard
Ok.   I guess you’re using a recent testing version.  It’s still to slow?

Otherwise you can use posterior sampling.   There is a vignettte about it 

-- 
Håvard Rue 

JJ Hubbard

unread,
Mar 18, 2023, 1:35:45 AM3/18/23
to R-inla discussion group
Do I need to add noise in some form to the samples from inla.posterior.sample()? The samples are fairly tightly clustered compared to when I sample using rnorm() with model$summary.linear.predictor$mean and sqrt(model$summary.linear.predictor$sd^2 + (1/sqrt(model$summary.hyperpar$mean[1]))^2) as the means and standard deviations of the posteriors.

Similar to how I add the noise from 1/sqrt(precision) in my second method, do I need to do something similar for inla.posterior.sample? Given that it samples from the joint distribution, I kind of expected its samples to be more dispersed. Perhaps this expectation is wrong.

Finn Lindgren

unread,
Mar 18, 2023, 2:10:38 AM3/18/23
to R-inla discussion group
Hi,

Inla.posterior.sample samples from the joint distribution of the latent variables and parameter, but not the data level, so yes, you need to add the data level simulation yourself, with rnorm in your case, based on the simulated precision values (look in the sample output to find them/their names.

Finn

On 18 Mar 2023, at 06:35, JJ Hubbard <jordanj...@gmail.com> wrote:



JJ Hubbard

unread,
Mar 18, 2023, 11:51:13 PM3/18/23
to R-inla discussion group
Does that mean taking each predictor from samples[[i]]$latent and adding rnorm(1, mean = 1/sqrt(samples[[i]]$hyperpar[[1]])) to it?

Elias T. Krainski

unread,
Mar 19, 2023, 12:30:50 AM3/19/23
to R-inla discussion group

Finn Lindgren

unread,
Mar 19, 2023, 12:47:11 AM3/19/23
to JJ Hubbard, R-inla discussion group
sd=, not mean=. The mean should be 0.

Finn

On 19 Mar 2023, at 04:51, JJ Hubbard <jordanj...@gmail.com> wrote:

Does that mean taking each predictor from samples[[i]]$latent and adding rnorm(1, mean = 1/sqrt(samples[[i]]$hyperpar[[1]])) to it?
Reply all
Reply to author
Forward
0 new messages