Learn R Programming

ProFit (version 1.3.3)

profitMakePriors: Make a Priors Function

Description

A utility function to construct a legal ProFit prior function that can be input to profitSetupData

Usage

profitMakePriors(modellist, sigmas, tolog, means=NULL, tofit=NULL, allowflat=FALSE)

Arguments

modellist

List; required. A valid ProFit modellist. Used to verify input arguments and check that the constructed prior function returns a finite value. The values of all parameters must be finite.

sigmas

Numeric list; required. The standard deviation of the prior distribution for each parameter. Must have the same length as modellist. All must be >0 or >=0 if allowflat is TRUE.

tolog

Logical list; required. Logicals indicating whether the parameter is fit in log space, in which case it must be logged in the prior function since it is always passed linear parameters. Must have the same length as modellist.

means

Numeric list; optional. The mean of the prior distribution for each parameter; these must be logged if tolog is TRUE for this parameter. Must have the same length as modellist. If the means are not specified, the prior function will follow default behaviour, which is to assume that the values in Data$modellist specify the prior means.

tofit

Logical list; optional. Logicals indicating whether the parameter is to be fit. If specified, only the parameters to be fit will have priors computed; otherwise, the default is for all priors to be evaluated (including for fixed parameters). Must have the same length as modellist.

allowflat

Logical; optional. Allows for flat priors by setting sigmas to Inf; in this case, the log-likelihood is computed as zero rather than -Inf.

Value

Function; a legal ProFit prior function that can be input to profitSetupData

Details

This function returns a valid ProFit prior function that can be input to profitSetupData. The function illustrates the use of R's formals function to set the default values of function arguments after a function is defined. This is necessary to store the values of prior distributions rather than a reference to the name of the variable storing those values in the current workspace, which is R's default behaviour. This behaviour is undesirable when saving a fit along with the corresponding profitSetupData object, as the parameters of the prior function can be changed or lost if the workspace is not saved.

See Also

profitLikeModel, profitSetupData

Examples

Run this code
# NOT RUN {
params = c(50,50,0,5,1,0,0.5,0)

modellist=list(
  sersic=list(
    xcen= params[1], ycen=params[2],
    mag= params[3], re=params[4],
    nser=params[5], ang=params[6],
    axrat=params[7], box=params[8]
  )
)

tolog=list(
  sersic=list(
    xcen=FALSE, ycen=FALSE,
    mag=FALSE, re=TRUE,
    nser=TRUE, ang=FALSE,
    axrat=TRUE, box=FALSE
  )
)

# Setup s.d. = 1 for linear and 0.1 dex for logged parameters
linear = unlist(tolog)
sigmas = unlist(modellist)
sigmas[which(linear)] = 0.1
sigmas[which(!linear)] = 1
sigmas = relist(sigmas, modellist)

#Make the list structure of prior function:
priors=profitMakePriors(modellist, sigmas, tolog)

#Check that the priors return the expected likelihood:
stopifnot(abs(priors(modellist,modellist) - sum(dnorm(0,0,unlist(sigmas),log=TRUE)))
< 10*.Machine$double.eps)
# }

Run the code above in your browser using DataLab