Finds the maximum likelihood estimates of parameters and returns a fitted model object with standard errors, confidence intervals, and other inference quantities computed automatically via autodiff.
Usage
fit(
loglik,
params,
method = c("bfgs", "lbfgs", "newton", "gradient"),
predict_fn = NULL,
...
)Arguments
- loglik
A log-likelihood function. Can be specified in two ways:
Named arguments:
function(mu, sigma) loglik_normal(mu, sigma, x)Single parameter argument:
function(p) loglik_normal(p$mu, p$sigma, x)
The function should return a scalar value object.
- params
Named numeric vector of initial parameter values, e.g.,
c(mu = 0, sigma = 1).- method
Optimization method: "bfgs" (default), "lbfgs", "newton", or "gradient".
- predict_fn
Optional prediction function. Should take arguments
(params, newdata)whereparamsis a named list of parameter values andnewdatais the new data for prediction. If provided, enables use ofpredict()on the fitted model.- ...
Additional arguments passed to the optimizer.
Examples
if (FALSE) { # \dontrun{
# Generate data
set.seed(42)
x <- rnorm(100, mean = 5, sd = 2)
# Fit using named arguments (recommended)
result <- fit(
function(mu, sigma) loglik_normal(mu, sigma, x),
params = c(mu = 0, sigma = 1)
)
# Or using single parameter argument
result <- fit(
function(p) loglik_normal(p$mu, p$sigma, x),
params = c(mu = 0, sigma = 1)
)
# Standard R generics work
coef(result) # Parameter estimates
vcov(result) # Variance-covariance matrix
confint(result) # 95% confidence intervals
logLik(result) # Log-likelihood (works with AIC/BIC)
AIC(result) # Akaike information criterion
summary(result) # Full summary with p-values
} # }