Description Usage Arguments Details Value Author(s) Examples
This function generate samples, and functions of those, from an approximated posterior of a fitted model (an inla-object)
1 2 3 4 5 6 | inla.posterior.sample(n = 1L, result, selection = list(),
intern = FALSE, use.improved.mean = TRUE,
add.names = TRUE, seed = 0L, num.threads = NULL,
verbose = FALSE)
inla.posterior.sample.eval(fun, samples, return.matrix = TRUE, ...)
|
n |
Number of samples. |
result |
The inla-object, ie the output from an |
selection |
Select what part of the sample to return. By default, the whole sample
is returned. |
use.improved.mean |
Logical. If |
intern |
Logical. If |
add.names |
Logical. If |
seed |
Control the RNG of |
num.threads |
The number of threads that can be used. |
verbose |
Logical. Run in verbose mode or not. |
fun |
The function to evaluate for each sample. Upon entry, the variable names
defined in the model are defined as the value of the sample.
The list of names are defined in |
samples |
|
return.matrix |
Logical. If |
... |
Additional arguments to |
The hyperparameters are sampled from the configurations used to do the
numerical integration, hence if you want a higher resolution, you need to
to change the int.stratey
variable and friends. The latent field is
sampled from the Gaussian approximation conditioned on the hyperparameters,
but with a correction for the mean (default).
Set sparse-matrix library with inla.setOption(smtp=...)
and
number of threads by inla.setOption(num.threads=...)
.
inla.posterior.sample
returns a list of the samples,
where each sample is a list with
names hyperpar
and latent
, and with their marginal
densities in logdens$hyperpar
and logdens$latent
and the joint density is in logdens$joint
.
inla.posterior.sample.eval
return a list or a matrix of
fun
applied to each sample.
Havard Rue hrue@r-inla.org
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | r = inla(y ~ 1 ,data = data.frame(y=rnorm(1)), control.compute = list(config=TRUE))
samples = inla.posterior.sample(2,r)
## reproducible results:
set.seed(1234)
inla.seed = as.integer(runif(1)*.Machine$integer.max)
x = inla.posterior.sample(100, r, seed = inla.seed)
set.seed(1234)
xx = inla.posterior.sample(100, r, seed = inla.seed)
all.equal(x, xx)
set.seed(1234)
n = 25
xx = rnorm(n)
yy = rev(xx)
z = runif(n)
y = rnorm(n)
r = inla(y ~ 1 + z + f(xx) + f(yy, copy="xx"),
data = data.frame(y, z, xx, yy),
control.compute = list(config=TRUE),
family = "gaussian")
r.samples = inla.posterior.sample(100, r)
fun = function(...) {
mean(xx) - mean(yy)
}
f1 = inla.posterior.sample.eval(fun, r.samples)
fun = function(...) {
c(exp(Intercept), exp(Intercept + z))
}
f2 = inla.posterior.sample.eval(fun, r.samples)
fun = function(...) {
return (theta[1]/(theta[1] + theta[2]))
}
f3 = inla.posterior.sample.eval(fun, r.samples)
## Predicting nz new observations, and
## comparing the estimated one with the true one
set.seed(1234)
n = 100
alpha = beta = s = 1
z = rnorm(n)
y = alpha + beta * z + rnorm(n, sd = s)
r = inla(y ~ 1 + z,
data = data.frame(y, z),
control.compute = list(config=TRUE),
family = "gaussian")
r.samples = inla.posterior.sample(10^3, r)
nz = 3
znew = rnorm(nz)
fun = function(zz = NA) {
## theta[1] is the precision
return (Intercept + z * zz +
rnorm(length(zz), sd = sqrt(1/theta[1])))
}
par(mfrow=c(1, nz))
f1 = inla.posterior.sample.eval(fun, r.samples, zz = znew)
for(i in 1:nz) {
hist(f1[i, ], n = 100, prob = TRUE)
m = alpha + beta * znew[i]
xx = seq(m-4*s, m+4*s, by = s/100)
lines(xx, dnorm(xx, mean=m, sd = s), lwd=2)
}
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.