smle: Simple maximum likelihood estimation

View source: R/mle-factory.R

smleR Documentation

Simple maximum likelihood estimation

Description

By default, this function performs a maximum likelihood estimation for one or several parameters, but it can be used for any other optimization problems. The interface is intented to be rather simple while allowing more advanced parametrizations.

Usage

smle(data, ...)

## Default S3 method:
smle(data, f, param_init, max = TRUE, ...)

## S3 method for class 'intensity'
smle(data, f, param_init, max = TRUE, ...)

Arguments

data

The data set to work with. It can be a vector (if there is only one variable), a data frame (if there is one or more variables) or an intensity object.

...

Additional arguments to be passed to optim.

f

A function to be maximized, typically a log-likelihood function. This function must have only two arguments: data and param, which must correspond to the data argument of smle and a named vector of the parameter(s) to be estimated.

param_init

Either a named vector with proposed initial values of the parameter(s), or a function that returns such a vector. This parameter is not needed if the parameter param of f is already provided with such a named vector.

max

Does f need to be maximized? Set to FALSE to require a minimization of f.

Details

The optim tool does the hard work under the hood. Extra arguments (e.g. method of optimization to be used) can be passed to optim through the ... argument. Note that contrary to the default optim arguments, smle tries to solve a maximization problem using the method "L-BFGS-B" by default (see optim documentation for more information).

Value

An object of class smle, which is a list containing the following components:

call The call.
coef The estimated coefficients.
coef_se The standard errors of the estimated coefficients.
vcov The variance-covariance matrix of the estimated coefficients.
data The data parameter.
f The f parameter.
nobs The number of observations.
full_input, full_output The full input and output of the optim function.

Examples

set.seed(12345)
data <- rlogis(100, location = 5, scale = 2)
ll_logis <- function(data, param = c(location = 0, scale = 1)) {
    sum(dlogis(x = data, location = param[["location"]],
               scale = param[["scale"]], log = TRUE))
}
res <- smle(data, ll_logis)
res
summary(res)

# Using the magrittr syntax:
require(magrittr)
data %>% smle(ll_logis)

# Comparision with the output of fitdistr (MASS package), which works for a
# limited number of predefined distributions:
require(MASS)
fitdistr(data, "logistic")

# Example with an intensity object:
require(magrittr)
require(dplyr)
data <- tomato_tswv$field_1929 %>%
    filter(t == 1) %>%
    incidence() %>%
    clump(unit_size = c(x = 3, y = 3))
ll_betabinom <- function(data, param) {
    sum(dbetabinom(x = data[["i"]], size = data[["n"]],
                   prob = param[["prob"]], theta = param[["theta"]],
                   log = TRUE))
}
epsilon <- 1e-7
res <- smle(data, ll_betabinom, param_init = c(prob = 0.5, theta = 0.5),
            lower = c(prob  = 0 + epsilon,
                      theta = 0 + epsilon),
            upper = c(prob = 1 - epsilon,
                      theta = Inf))
res
summary(res)

param_init <- data.frame(lower = c(0 + epsilon, 0 + epsilon),
                         start = c(0.5, 0.5),
                         upper = c(1 - epsilon, Inf))
rownames(param_init) <- c("prob", "theta")
res <- smle(data, ll_betabinom, param_init)
res
summary(res)


chgigot/epiphy documentation built on Nov. 20, 2023, 1:13 p.m.