marginal {INLA} | R Documentation

## Functions which operates on marginals

### Description

Density, distribution function, quantile function, random generation, hpd-interval, interpolation, expectations, mode and transformations of marginals obtained by `inla` or `inla.hyperpar()`. These functions computes the density (inla.dmarginal), the distribution function (inla.pmarginal), the quantile function (inla.qmarginal), random generation (inla.rmarginal), spline smoothing (inla.smarginal), computes expected values (inla.emarginal), computes the mode (inla.mmarginal), transforms the marginal (inla.tmarginal), and provide summary statistics (inla.zmarginal).

### Usage

     inla.dmarginal(x, marginal, log = FALSE)
     inla.pmarginal(q, marginal, normalize = TRUE, len = 2048L)
     inla.qmarginal(p, marginal, len = 2048L)
     inla.rmarginal(n, marginal)
     inla.hpdmarginal(p, marginal, len = 2048L)
     inla.smarginal(marginal, log = FALSE, extrapolate = 0.0, keep.type = FALSE, factor=15L)
     inla.emarginal(fun, marginal, ...)
     inla.mmarginal(marginal)
     inla.tmarginal(fun, marginal, n=2048L, h.diff = .Machine$double.eps^(1/3),
                    method = c("quantile", "linear"))
     inla.zmarginal(marginal, silent = FALSE)

### Arguments

`marginal` | A marginal object from either `inla` or `inla.hyperpar()`, which is either `list(x=c(), y=c())` with density values `y` at locations `x`, or a `matrix(,n,2)` for which the density values are the second column and the locations in the first column. The`inla.hpdmarginal()`-function assumes a unimodal density.
`fun` | A (vectorised) function like `function(x) exp(x)` to compute the expectation against, or which define the transformation new = fun(old)
`x` | Evaluation points
`q` | Quantiles
`p` | Probabilities
`n` | The number of observations. If `length(n) > 1`, the length is taken to be the number required. For `inla.marginal.transform`, its the number of points to use in the new density.
`h.diff` | The step-length for the numerical differeniation inside `inla.marginal.transform`
`...` | Further arguments to be passed to function which expectation is to be computed.
`log` | Return density or interpolated density in log-scale?
`normalize` | Renormalise the density after interpolation?
`len` | Number of locations used to interpolate the distribution function.
`keep.type` | If `FALSE` then return a `list(x=, y=)`, otherwise if `TRUE`, then return a matrix if the input is a matrix
`extrapolate` | How much to extrapolate on each side when computing the interpolation. In fraction of the range.
`factor` | The number of points after interpolation is `factor` times the original number of points; which is argument `n` in `spline`
`method` | Which method should be used to layout points for where the transformation is computed.
`silent` | Output the result visually (TRUE) or just through the call.

### Value

`inla.smarginal` returns `list=c(x=c(), y=c())` of interpolated values do extrapolation using the factor given, and the remaining function returns what they say they should do.

### Author(s)

Havard Rue hrue@r-inla.org

### See Also

`inla`, `inla.hyperpar`

### Examples

     ## a simple linear regression example
     n = 10
     x = rnorm(n)
     sd = 0.1
     y = 1+x + rnorm(n,sd=sd)
     res = inla(y ~ 1 + x, data = data.frame(x,y),
                control.family=list(initial = log(1/sd^2),fixed=TRUE))

     ## chose a marginal and compare the with the results computed by the
     ## inla-program
     r = res$summary.fixed["x",]
     m = res$marginals.fixed$x

     ## compute the 95% HPD interval
     inla.hpdmarginal(0.95, m)

     x = seq(-6, 6, len = 1000)
     y = dnorm(x)
     inla.hpdmarginal(0.95, list(x=x, y=y))

     ## compute the the density for exp(r), version 1
     r.exp = inla.tmarginal(exp, m)
     ## or version 2
     r.exp = inla.tmarginal(function(x) exp(x), m)

     ## to plot the marginal, we use the inla.smarginal, which interpolates (in
     ## log-scale). Compare with some samples.
     plot(inla.smarginal(m), type="l")
     s = inla.rmarginal(1000, m)
     hist(inla.rmarginal(1000, m), add=TRUE, prob=TRUE)
     lines(density(s), lty=2)

     m1 = inla.emarginal(function(x) x^1, m)
     m2 = inla.emarginal(function(x) x^2, m)
     stdev = sqrt(m2 - m1^2)
     q = inla.qmarginal(c(0.025,0.975), m)

     ## inla-program results
     print(r)

     ## inla.marginal-results (they shouldn't be perfect!)
     print(c(mean=m1, sd=stdev, "0.025quant" = q[1], "0.975quant" = q[2]))
     ## using the buildt-in function
     inla.zmarginal(m)

[Package INLA version 18.07.12 Index]