Learn R Programming

aghq (version 0.4.1)

validate_moment: Validate a moment function object

Description

Routine for checking whether a given moment function object is valid.

Usage

validate_moment(...)

# S3 method for aghqmoment validate_moment(moment, checkpositive = FALSE, ...)

# S3 method for list validate_moment(moment, checkpositive = FALSE, ...)

# S3 method for `function` validate_moment(moment, checkpositive = FALSE, ...)

# S3 method for character validate_moment(moment, checkpositive = FALSE, ...)

# S3 method for default validate_moment(moment, ...)

Value

TRUE if the function runs to completion without throwing an error.

Arguments

...

Used to pass arguments to methods.

moment

An object to check if it is a valid moment function or not. Can be an object of class aghqmoment returned by aghq::make_moment_function(), or any object that can be passed to aghq::make_moment_function().

checkpositive

Default FALSE, do not check that gg$fn(theta) > 0. Otherwise, a vector of values for which to perform that check. No default values are provided, since validate_moment has no way of determining the domain and range of gg$fn. This argument is used internally in aghq package functions, with cleverly chosen check values.

Details

This function checks that:

  • The supplied object contains elements fn, gr, and he, and that they are all functions,

  • If checkpositive is a vector of numbers, then it checks that gg$fn(checkpositive) is not -Inf, NA, or NaN. (It actually uses is.infinite for the first.)

In addition, if a list is provided, the function first checks that it contains the right elements, then passes it to make_moment_function, then checks that. If a function or a character is provided, it checks that match.fun works, and returns any errors or warnings from doing so in a clear way.

This function throws an informative error messages when checks don't pass or themselves throw errors.

See Also

Other moments: make_moment_function()

Examples

Run this code

mom1 <- make_moment_function(exp)
mom2 <- make_moment_function('exp')
mom3 <- make_moment_function(list(fn=function(x) x,gr=function(x) 1,he = function(x) 0))
validate_moment(mom1)
validate_moment(mom2)
validate_moment(mom3)
if (FALSE) {
mombad1 <- list(exp,exp,exp) # No names
mombad2 <- list('exp','exp','exp') # List of not functions
mombad3 <- make_moment_function(function(x) -exp(x)) # Not positive
validate_moment(mombad1)
validate_moment(mombad2)
validate_moment(mombad3)
}

Run the code above in your browser using DataLab