Description Usage Arguments Details Value Author(s) See Also Examples
Find Starting Values for the Metropolis Algorithm
1 2 3 | findStartingValues(support, logposterior, lidat, multiple = list(),
nrand = 10, method = c("onebest", "dispersed", "all"), ndispersed = 3,
info = TRUE)
|
support |
named list with one element per parameter (the names of the list correspond to the name of the parameters). Each element should be a vector of length 2 indicating the limits of the support of the parameters (i.e. the min and max values in which the starting values will be searched). |
logposterior |
a function for the calculation of the log
posterior for the current model. This function must rely only on
the data provided in |
lidat |
a named list containing the data (the names of the list correspond to the name of the variables used in logposterior, combined with the parameters), required for the calculation of the posterior |
multiple |
a named list describing which parameters are
vectors of parameters. For example, if a model relies on a single
parameter |
nrand |
the number of starting values to generate before a result is returned (see details). |
method |
the method to be used by the function (see details). |
ndispersed |
integer. When |
info |
whether information on the generation process should be displayed to the user. |
findStartingValues
allows to find a list of starting
values for the Metropolis algorithm for the MCMC fitting of Bayesian models.
If method=="onebest"
, a named list of starting
values for the parameters, that can be used directly as an argument
to GeneralSingleMetropolis
. For other methods, a list of
lists of starting values.
Clement Calenge, clement.calenge@oncfs.gouv.fr
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 | ## We generate data
x1 <- rnorm(100)
x2 <- rnorm(100)
x3 <- rnorm(100)
y <- 1+x1+0.5*x2+1.5*x3+rnorm(100, sd=0.5)
## Our data
lid <- list(x=cbind(x1,x2,x3), y=y)
## 5 parameters: intercept + 3 slopes + residual variance
## For the sake of demonstration, we put the three slopes in
## a vector theta for the estimation.
## The list of starting values should therefore contain: (i) theta,
## (ii) intercept, (iii) residprec
##
## The function for the posterior is:
lpost <- function(par, lidat, ctrl)
{
## prior:
parb <- par
parb$residprec <- NULL
lprior <- sum(dnorm(unlist(parb), mean=0, sd=100)) ## vague prior
## (uniform prior for the residual precision, we do not add this constant)
mu <- lidat$x%*%par$theta+par$intercept
## log-posterior
return(sum(lprior + dnorm(lidat$y, mean=mu, sd = 1/sqrt(par$residprec), log=TRUE)))
}
## We define large support for all parameters
support <- list(theta=c(-10,10), intercept=c(-10,10), residprec=c(0.0001, 10))
## only theta is multiple (3 elements), so:
multiple <- list(theta=3)
## Default method: finds the best starting value among 1000 sampled values
## The plot shows the distribution of the sampled posterior (up to a constant)
## the best value is returned in sv
(sv <- findStartingValues(support, lpost, lid, multiple, nrand=1000))
## Same, but keeps 4 most dispersed starting values among 100 generated values:
(sv2 <- findStartingValues(support, lpost, lid, multiple, nrand=100,
method="dispersed", ndispersed=4))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.