Sample and evalue from from a joint marginal approximation as returned using argument selection in inla.

inla.rjmarginal(n, jmarginal, constr)
 inla.rjmarginal.eval(fun, samples, ...)

Arguments

n

The number of samples

jmarginal

A marginal object given either by a inla object or result$selection

constr

Optional linear constraints; see ?INLA::f and argument extraconstr

fun

A function which is evaluated for each sample, similar to inla.posterior.sample.eval: please see the documentation for this functions for details.

samples

The samples, as in the form of the output from inla.rjmarginal

Value

inla.rjmarginal returns a list with the samples in samples (matrix) and the corresponding log-densities in log.density (vector). Each column in samples contains one sample.

inla.rjmarginal.eval returns a matrix, where each row is the (vector) function evaluated at each sample.

Author

Havard Rue hrue@r-inla.org

See also

Examples

 n = 10
 x = 1+rnorm(n)
 xx = 3 + rnorm(n)
 y = 1 + x + xx + rnorm(n)
 selection = list(xx=1, Predictor = 3:4, x=1)
 r = inla(y ~ 1 + x + xx,
          data = data.frame(y, x, xx),
          selection = selection)
#> Warning: error in running command
#> Error in inla.inlaprogram.has.crashed(): The inla-program exited with an error. Unless you interupted it yourself, please rerun with verbose=TRUE and check the output carefully.
#>   If this does not help, please contact the developers at <help@r-inla.org>.
 ns = 100
 xx = inla.rjmarginal(ns, r)
#> Error: object 'r' not found
 
 print(cbind(mean = r$selection$mean, sample.mean = rowMeans(xx$samples)))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': object 'r' not found
 print("cov matrix")
#> [1] "cov matrix"
 print(round(r$selection$cov.matrix, dig=3))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': object 'r' not found
 print("sample cov matrix")
#> [1] "sample cov matrix"
 print(round(cov(t(xx$samples)), dig=3))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': error in evaluating the argument 'x' in selecting a method for function 't': $ operator is invalid for atomic vectors
 
 skew = function(z) mean((z-mean(z))^3)/var(z)^1.5
 print(round(cbind(skew = r$selection$skewness,
                   sample.skew = apply(xx$sample, 1, skew)), dig=3))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': object 'r' not found

 ## illustrating the eval function
 n = 10
 x = rnorm(n)
 eta = 1 + x
 y = eta + rnorm(n, sd=0.1)
 selection = list(x = 1, Predictor = c(1, 2, 4, 5),  '(Intercept)' = 1)
 r = inla(y ~ 1 + x,
          data = data.frame(y, x),
          selection = selection)
#> Warning: error in running command
#> Error in inla.inlaprogram.has.crashed(): The inla-program exited with an error. Unless you interupted it yourself, please rerun with verbose=TRUE and check the output carefully.
#>   If this does not help, please contact the developers at <help@r-inla.org>.
 xx = inla.rjmarginal(100,  r)
#> Error: object 'r' not found
 xx.eval = inla.rjmarginal.eval(function() c(x, Predictor, Intercept),  xx)
#> Warning: Function 'inla.rjmarginal.eval()' is experimental.
#> Error in samples$samples: $ operator is invalid for atomic vectors
 print(cbind(xx$samples[, 1]))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': $ operator is invalid for atomic vectors
 print(cbind(xx.eval[, 1]))
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'print': object 'xx.eval' not found