Learn R Programming

phenology (version 4.2.4)

phenology_MHmcmc: Run the Metropolis-Hastings algorithm for data

Description

Run the Metropolis-Hastings algorithm for data. The number of iterations is n.iter+n.adapt+1 because the initial likelihood is also displayed. I recommend thin=1 because the method to estimate SE uses resampling. As initial point is maximum likelihood, n.adapt = 0 is a good solution. The parameters intermediate and filename are used to save intermediate results every 'intermediate' iterations (for example 1000). Results are saved in a file of name filename. The parameter previous is used to indicate the list that has been save using the parameters intermediate and filename. It permits to continue a mcmc search. These options are used to prevent the consequences of computer crash or if the run is very very long and computer processes at time limited.

Usage

phenology_MHmcmc(result = stop("An output from fit_phenology() must be provided"),
  n.iter = 10000,
  parametersMCMC = stop("A model generated with phenology_MHmcmc_p() must be provided"),
  n.chains = 4, n.adapt = 0, thin = 1, trace = FALSE,
  intermediate = NULL, filename = "intermediate.Rdata", previous = NULL)

Arguments

result
An object obtained after a SearchR fit
n.iter
Number of iterations for each step
parametersMCMC
A set of parameters used as initial point for searching with information on priors
n.chains
Number of replicates
n.adapt
Number of iterations before to store outputs
thin
Number of iterations between each stored output
trace
True or False, shows progress
intermediate
Period for saving intermediate result, NULL for no save
filename
If intermediate is not NULL, save intermediate result in this file
previous
Previous result to be continued. Can be the filename in which intermediate results are saved.

Value

  • A list with resultMCMC being mcmc.list object, resultLnL being likelihoods and parametersMCMC being the parameters used

Details

phenology_MHmcmc runs the Metropolis-Hastings algorithm for data (Bayesian MCMC)

Examples

Run this code
library(phenology)
data(Gratiot)
# Generate a formatted list named data_Gratiot
data_Gratiot <- add_phenology(Gratiot, name="Complete",
    reference=as.Date("2001-01-01"), format="%d/%m/%Y")
# Generate initial points for the optimisation
parg <- par_init(data_Gratiot, parametersfixed=NULL)
# Run the optimisation
result_Gratiot <- fit_phenology(data=data_Gratiot,
		parametersfit=parg, parametersfixed=NULL, trace=1)
# Generate set of priors for Bayesian analysis
pmcmc <- phenology_MHmcmc_p(result_Gratiot, accept = TRUE)
result_Gratiot_mcmc <- phenology_MHmcmc(result = result_Gratiot, n.iter = 10000,
parametersMCMC = pmcmc, n.chains = 1, n.adapt = 0, thin = 1, trace = FALSE)
# Get standard error of parameters
summary(result_Gratiot_mcmc)
# Make diagnostics of the mcmc results using coda package
mcmc <- as.mcmc(result_Gratiot_mcmc)
require(coda)
heidel.diag(mcmc)
raftery.diag(mcmc)
autocorr.diag(mcmc)
acf(mcmc[[1]][,"LengthB"], lag.max=200, bty="n", las=1)
acf(mcmc[[1]][,"Max_Gratiot"], lag.max=50, bty="n", las=1)
batchSE(mcmc, batchSize=100)
# The batch standard error procedure is usually thought to
# be not as accurate as the time series methods used in summary
summary(mcmc)$statistics[,"Time-series SE"]
plot(result_Gratiot_mcmc, parameters=3, las=1, xlim=c(-10, 300))

Run the code above in your browser using DataLab