| Title: | Flexible Maximum Likelihood Regression with Gradient-Based Optimisation |
|---|---|
| Description: | Fits flexible maximum likelihood regression models supporting censored, interval, and hybrid continuous/dichotomous data. Provides explicit analytic and numerical gradient computation, random intercept models via Gauss-Hermite quadrature, and multiple distribution families. |
| Authors: | Kim Rand [aut, cre] |
| Maintainer: | Kim Rand <[email protected]> |
| License: | GPL-2 |
| Version: | 0.1.4 |
| Built: | 2026-06-03 08:16:58 UTC |
| Source: | https://github.com/mathsinhealth/xreg2 |
Computes the Akaike Information Criterion for a fitted
xreg2 model. Scale parameters (SIGMA, LN_SIGMA, OMEGA, LN_OMEGA)
are excluded from the parameter count.
## S3 method for class 'xreg2' AIC(object, ..., k = 2, just_total = TRUE)## S3 method for class 'xreg2' AIC(object, ..., k = 2, just_total = TRUE)
object |
A fitted |
... |
Currently unused. |
k |
Numeric; penalty per parameter. Default |
just_total |
Logical; return only the total AIC across all components.
Default |
AIC method for xreg2 objects
A numeric scalar (AIC value).
Average Relative Error
ARE(est, obs, absolute = FALSE, ...) ## S3 method for class 'data.frame' ARE(est, obs, absolute = FALSE, ...) ## S3 method for class 'matrix' ARE(est, obs, absolute = FALSE, ...) ## S3 method for class 'numeric' ARE(est, obs, absolute = FALSE, ...) ARE_conform(dfys, dfobs, absolute = FALSE)ARE(est, obs, absolute = FALSE, ...) ## S3 method for class 'data.frame' ARE(est, obs, absolute = FALSE, ...) ## S3 method for class 'matrix' ARE(est, obs, absolute = FALSE, ...) ## S3 method for class 'numeric' ARE(est, obs, absolute = FALSE, ...) ARE_conform(dfys, dfobs, absolute = FALSE)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
absolute |
Logical; return absolute version. Default |
... |
Currently unused. |
dfys |
Numeric matrix or data frame of estimates. |
dfobs |
Numeric matrix or data frame of observations. |
Numeric ARE value.
Computes the Bayesian Information Criterion for a fitted
xreg2 model.
## S3 method for class 'xreg2' BIC(object, ..., just_total = TRUE)## S3 method for class 'xreg2' BIC(object, ..., just_total = TRUE)
object |
A fitted |
... |
Currently unused. |
just_total |
Logical; return only the total BIC. Default |
BIC method for xreg2 objects
A numeric scalar (BIC value).
Combines one or more xreg2Control objects into an
xreg2ControlList suitable for passing to xreg2_fit.
## S3 method for class 'xreg2Control' c(...) ## S3 method for class 'xreg2ControlList' c(...)## S3 method for class 'xreg2Control' c(...) ## S3 method for class 'xreg2ControlList' c(...)
... |
One or more |
Combine xreg2Control objects into a list
An object of class c("xreg2ControlList", "list").
ctrl1 <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), name = "component1") cl <- c(ctrl1)ctrl1 <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), name = "component1") cl <- c(ctrl1)
Combined mean of two groups
combmean(m1, m2, n1 = NULL, n2 = NULL, na.rm = FALSE)combmean(m1, m2, n1 = NULL, n2 = NULL, na.rm = FALSE)
m1 |
Mean of group 1 (scalar or vector). |
m2 |
Mean of group 2 (scalar or vector). |
n1 |
Size of group 1. |
n2 |
Size of group 2. |
na.rm |
Logical. |
Combined mean.
Combined mean of multiple groups
combmeans(ms, ns, na.rm = FALSE)combmeans(ms, ns, na.rm = FALSE)
ms |
Numeric vector of group means. |
ns |
Numeric vector of group sizes. |
na.rm |
Logical. |
Combined mean.
Combined mean, SD and n for multiple groups
combmssds(ms, ss, ns, na.rm = FALSE)combmssds(ms, ss, ns, na.rm = FALSE)
ms |
Numeric vector of group means. |
ss |
Numeric vector of group SDs. |
ns |
Numeric vector of group sizes. |
na.rm |
Logical. |
Matrix with columns n, mean, sd.
Combined standard deviation of two groups
combsd(m1, m2, s1, s2, n1, n2, na.rm = FALSE)combsd(m1, m2, s1, s2, n1, n2, na.rm = FALSE)
m1 |
Mean of group 1. |
m2 |
Mean of group 2. |
s1 |
SD of group 1. |
s2 |
SD of group 2. |
n1 |
Size of group 1. |
n2 |
Size of group 2. |
na.rm |
Logical. |
Combined SD.
Combined SD of multiple groups
combsds(ms, ss, ns, na.rm = FALSE)combsds(ms, ss, ns, na.rm = FALSE)
ms |
Numeric vector of group means. |
ss |
Numeric vector of group SDs. |
ns |
Numeric vector of group sizes. |
na.rm |
Logical. |
Combined SD.
Computes multiple error metrics and returns them as a matrix.
comp_table(est, obs, ...)comp_table(est, obs, ...)
est |
Numeric vector or matrix of estimates. |
obs |
Numeric vector of observations. |
... |
Passed to metric functions. |
Comparison table of error metrics
A numeric matrix with one row per metric.
Maximum Absolute Relative Error
erMAX(est, obs, ...) ## S3 method for class 'data.frame' erMAX(est, obs, ...) ## S3 method for class 'matrix' erMAX(est, obs, ...) ## S3 method for class 'numeric' erMAX(est, obs, ...)erMAX(est, obs, ...) ## S3 method for class 'data.frame' erMAX(est, obs, ...) ## S3 method for class 'matrix' erMAX(est, obs, ...) ## S3 method for class 'numeric' erMAX(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Currently unused. |
Numeric erMAX value.
Computes Fisher's intraclass correlation coefficient between two numeric vectors or matrices.
fisher_icc(y, x, ...) ## S3 method for class 'data.frame' fisher_icc(y, x, ...) ## S3 method for class 'matrix' fisher_icc(y, x, ...) ## S3 method for class 'numeric' fisher_icc(y, x, ...)fisher_icc(y, x, ...) ## S3 method for class 'data.frame' fisher_icc(y, x, ...) ## S3 method for class 'matrix' fisher_icc(y, x, ...) ## S3 method for class 'numeric' fisher_icc(y, x, ...)
y |
Numeric vector, matrix, or data frame. |
x |
Numeric vector, matrix, or data frame. |
... |
Currently unused. |
Fisher's intraclass correlation coefficient
Numeric ICC value.
Maps common distribution name variants to their canonical R
short form (e.g., "normal" -> "norm").
fixdist(dist)fixdist(dist)
dist |
Character distribution name. |
Standardise distribution name
Canonical short-form character name.
Returns the named R distribution function (density, CDF,
quantile, or random) for a given distribution, with an extra ...
argument appended to its formals.
getf(dist, type) getp(dist) getq(dist) getd(dist) getr(dist)getf(dist, type) getp(dist) getq(dist) getd(dist) getr(dist)
dist |
Character distribution name (passed to |
type |
Character; one of |
Get a distribution function by type
A function.
Mean of a distribution
getMean(qfun, pars)getMean(qfun, pars)
qfun |
Quantile function. |
pars |
Named list of distribution parameters. |
Numeric mean.
Standard deviation of a distribution
getSD(qfun, pars)getSD(qfun, pars)
qfun |
Quantile function or character distribution name. |
pars |
Named list of distribution parameters. |
Numeric standard deviation.
Variance of a distribution
getVar(qfun, pars)getVar(qfun, pars)
qfun |
Quantile function or character distribution name. |
pars |
Named list of distribution parameters. |
Numeric variance.
Tests whether numeric values are whole numbers within a numeric tolerance.
is.wholenumber(x, tol = .Machine$double.eps^0.5)is.wholenumber(x, tol = .Machine$double.eps^0.5)
x |
Numeric vector. |
tol |
Numeric tolerance. Default |
Test for whole number
Logical vector.
Lin's concordance correlation coefficient (simplified)
just_CCC(y, x, ...) ## S3 method for class 'data.frame' just_CCC(y, x, ...) ## S3 method for class 'matrix' just_CCC(y, x, ...) ## S3 method for class 'numeric' just_CCC(y, x, ...)just_CCC(y, x, ...) ## S3 method for class 'data.frame' just_CCC(y, x, ...) ## S3 method for class 'matrix' just_CCC(y, x, ...) ## S3 method for class 'numeric' just_CCC(y, x, ...)
y |
Numeric vector, matrix, or data frame. |
x |
Numeric vector, matrix, or data frame. |
... |
Passed to |
Numeric CCC value.
Calculates Lin's concordance correlation coefficient (CCC) for a pair of variables, measuring agreement along the 45-degree line.
LCCC( x, y, ci = "z-transform", conf.level = 0.95, na.rm = FALSE, justp = FALSE, ... )LCCC( x, y, ci = "z-transform", conf.level = 0.95, na.rm = FALSE, justp = FALSE, ... )
x |
Numeric vector; first variable. |
y |
Numeric vector; second variable. |
ci |
Character; method for confidence interval. Default
|
conf.level |
Numeric; confidence level. Default |
na.rm |
Logical; remove |
justp |
Logical; if |
... |
Currently unused. |
Lin's concordance correlation coefficient
A list with components rho.c, s.shift,
l.shift, C.b, and blalt.
Mean Absolute Error
MAE(est, obs, ...) ## S3 method for class 'data.frame' MAE(est, obs, ...) ## S3 method for class 'matrix' MAE(est, obs, ...) ## S3 method for class 'numeric' MAE(est, obs, ...)MAE(est, obs, ...) ## S3 method for class 'data.frame' MAE(est, obs, ...) ## S3 method for class 'matrix' MAE(est, obs, ...) ## S3 method for class 'numeric' MAE(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Passed to |
Numeric MAE value.
Mean Absolute Relative Error / Mean Absolute Percentage Error
MAPE(est, obs, ...) MARE(est, obs, ...) ## S3 method for class 'data.frame' MARE(est, obs, ...) ## S3 method for class 'matrix' MARE(est, obs, ...) ## S3 method for class 'numeric' MARE(est, obs, ...)MAPE(est, obs, ...) MARE(est, obs, ...) ## S3 method for class 'data.frame' MARE(est, obs, ...) ## S3 method for class 'matrix' MARE(est, obs, ...) ## S3 method for class 'numeric' MARE(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Currently unused. |
Numeric MARE value.
Mean Bias Error
MBE(est, obs, ...) ## S3 method for class 'data.frame' MBE(est, obs, ...) ## S3 method for class 'matrix' MBE(est, obs, ...) ## S3 method for class 'numeric' MBE(est, obs, ...)MBE(est, obs, ...) ## S3 method for class 'data.frame' MBE(est, obs, ...) ## S3 method for class 'matrix' MBE(est, obs, ...) ## S3 method for class 'numeric' MBE(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Passed to |
Numeric MBE value.
Computes the density of the -th order statistic in a
random sample of from a given distribution.
ordD(x, r, n, par = c(mean = 0, sd = 1), dist = "norm", log = FALSE)ordD(x, r, n, par = c(mean = 0, sd = 1), dist = "norm", log = FALSE)
x |
Numeric vector of values. |
r |
Integer rank. |
n |
Sample size. |
par |
Named vector of distribution parameters. |
dist |
Character distribution name. |
log |
Logical; return log-density. Default |
Density for an order statistic
Numeric density (or log-density).
Computes the expected value of the -th order statistic
in a random sample of from a given distribution.
ordE(r, n, par = c(mean = 0, sd = 1), dist = "norm", log = FALSE)ordE(r, n, par = c(mean = 0, sd = 1), dist = "norm", log = FALSE)
r |
Integer rank (or vector of ranks). |
n |
Sample size. |
par |
Named vector of distribution parameters. |
dist |
Character distribution name. |
log |
Currently unused. |
Expected value of an order statistic
Numeric expected value.
Expected probability of order statistics
ordEp(probs, n, par = c(mean = 0, sd = 1), dist = "normal")ordEp(probs, n, par = c(mean = 0, sd = 1), dist = "normal")
probs |
Numeric vector of rank proportions. |
n |
Sample size. |
par |
Named vector of distribution parameters. |
dist |
Character distribution name. |
Numeric vector of probabilities.
Generates predictions (linear predictors and intermediate
computed variables) from a fitted xreg2 model applied to new data.
## S3 method for class 'xreg2' predict(object, newdata = NULL, return_vector = FALSE, ...)## S3 method for class 'xreg2' predict(object, newdata = NULL, return_vector = FALSE, ...)
object |
A fitted |
newdata |
A named list of data frames (one per model component), or a single data frame (automatically wrapped and named after the first component). |
return_vector |
Logical; if |
... |
Currently unused. |
Predict method for xreg2 objects
A named list of data frames (one per component), each augmented
with computed variables including Xb.
set.seed(1) df <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200)) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), name = "main" ) fit <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df)) newdf <- data.frame(x = 1:5) preds <- predict(fit, newdata = list(main = newdf))set.seed(1) df <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200)) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), name = "main" ) fit <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df)) newdf <- data.frame(x = 1:5) preds <- predict(fit, newdata = list(main = newdf))
Prints a concise summary of a fitted xreg2 model,
including coefficients, standard errors, and minimised negative
log-likelihoods.
## S3 method for class 'xreg2' print(x, ...)## S3 method for class 'xreg2' print(x, ...)
x |
A fitted |
... |
Currently unused. |
Print method for xreg2 objects
x invisibly.
Computes summary statistics and quantiles for a numeric vector.
quant_fun( x, statfuns = function(y) c(MEAN = mean(y, na.rm = TRUE), SE = sd(y, na.rm = TRUE)), probs = c(min = 0, `2.5%` = 0.025, `25%` = 0.25, median = 0.5, `75%` = 0.75, `97.5%` = 0.975, max = 1), names = TRUE )quant_fun( x, statfuns = function(y) c(MEAN = mean(y, na.rm = TRUE), SE = sd(y, na.rm = TRUE)), probs = c(min = 0, `2.5%` = 0.025, `25%` = 0.25, median = 0.5, `75%` = 0.75, `97.5%` = 0.975, max = 1), names = TRUE )
x |
Numeric vector. |
statfuns |
Function returning named summary statistics. |
probs |
Named numeric vector of quantile probabilities. |
names |
Logical; include names in quantile output. |
Quantile summary statistics
Named numeric vector of statistics and quantiles.
Computes quantile summaries within each level of a categorical variable.
quant_fun_groups(x, catvar, pooled = TRUE, n = TRUE) quant_funs_groups(x, catvar, pooled = TRUE, n = TRUE)quant_fun_groups(x, catvar, pooled = TRUE, n = TRUE) quant_funs_groups(x, catvar, pooled = TRUE, n = TRUE)
x |
A list of numeric vectors. |
catvar |
Factor or character vector of group labels. |
pooled |
Logical; include a pooled row. Default |
n |
Logical; include count and missing count columns. Default |
Quantile summaries by group
Data frame with one row per group.
Applies quant_fun over rows or columns of a
matrix or data frame.
quant_funs( x, MARGIN = 1, statfuns = function(y) c(MEAN = mean(y, na.rm = TRUE), SE = sd(y, na.rm = TRUE)), probs = c(min = 0, `2.5%` = 0.025, `25%` = 0.25, median = 0.5, `75%` = 0.75, `97.5%` = 0.975, max = 1) )quant_funs( x, MARGIN = 1, statfuns = function(y) c(MEAN = mean(y, na.rm = TRUE), SE = sd(y, na.rm = TRUE)), probs = c(min = 0, `2.5%` = 0.025, `25%` = 0.25, median = 0.5, `75%` = 0.75, `97.5%` = 0.975, max = 1) )
x |
Matrix or data frame. |
MARGIN |
Integer; |
statfuns |
Function returning named summary statistics. |
probs |
Named numeric vector of quantile probabilities. |
Quantile summaries over matrix margins
Data frame of statistics.
Like quant_funs but returns transposed result.
quant_funst( x, MARGIN = 1, statfuns = function(y) c(MEAN = mean(y, na.rm = TRUE), SE = sd(y, na.rm = TRUE)), probs = c(min = 0, `2.5%` = 0.025, `25%` = 0.25, median = 0.5, `75%` = 0.75, `97.5%` = 0.975, max = 1) )quant_funst( x, MARGIN = 1, statfuns = function(y) c(MEAN = mean(y, na.rm = TRUE), SE = sd(y, na.rm = TRUE)), probs = c(min = 0, `2.5%` = 0.025, `25%` = 0.25, median = 0.5, `75%` = 0.75, `97.5%` = 0.975, max = 1) )
x |
Matrix or data frame. |
MARGIN |
Integer; |
statfuns |
Function returning named summary statistics. |
probs |
Named numeric vector of quantile probabilities. |
Transposed quantile summaries over matrix margins
Transposed data frame of statistics.
Relative Mean Squared Error
ReMSE(est, obs, trueobs, ...) ## S3 method for class 'data.frame' ReMSE(est, obs, trueobs, ...) ## S3 method for class 'matrix' ReMSE(est, obs, trueobs, ...) ## S3 method for class 'numeric' ReMSE(est, obs, trueobs, ...) ReMSE_conform(dfys, dfobs, trueobs)ReMSE(est, obs, trueobs, ...) ## S3 method for class 'data.frame' ReMSE(est, obs, trueobs, ...) ## S3 method for class 'matrix' ReMSE(est, obs, trueobs, ...) ## S3 method for class 'numeric' ReMSE(est, obs, trueobs, ...) ReMSE_conform(dfys, dfobs, trueobs)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
trueobs |
Numeric; true values. |
... |
Currently unused. |
dfys |
Numeric matrix or data frame of estimates. |
dfobs |
Numeric matrix or data frame of observations. |
Numeric ReMSE value.
Root Mean Square Error
RMSE(est, obs, percentage = FALSE, ...) ## S3 method for class 'data.frame' RMSE(est, obs, percentage = FALSE, ...) ## S3 method for class 'matrix' RMSE(est, obs, percentage = FALSE, ...) ## S3 method for class 'numeric' RMSE(est, obs, percentage = FALSE, ...) pRMSE(est, obs, ...) rRMSE(est, obs, ...)RMSE(est, obs, percentage = FALSE, ...) ## S3 method for class 'data.frame' RMSE(est, obs, percentage = FALSE, ...) ## S3 method for class 'matrix' RMSE(est, obs, percentage = FALSE, ...) ## S3 method for class 'numeric' RMSE(est, obs, percentage = FALSE, ...) pRMSE(est, obs, ...) rRMSE(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
percentage |
Logical; return as percentage of mean. Default |
... |
Passed to |
Numeric RMSE value.
Root Mean Square Relative Error
RMSRE(est, obs, ...) ## S3 method for class 'data.frame' RMSRE(est, obs, ...) ## S3 method for class 'matrix' RMSRE(est, obs, ...) ## S3 method for class 'numeric' RMSRE(est, obs, ...)RMSRE(est, obs, ...) ## S3 method for class 'data.frame' RMSRE(est, obs, ...) ## S3 method for class 'matrix' RMSRE(est, obs, ...) ## S3 method for class 'numeric' RMSRE(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Currently unused. |
Numeric RMSRE value.
Prints a detailed summary of a fitted xreg2 model.
Currently equivalent to print.xreg2.
## S3 method for class 'xreg2' summary(object, ...)## S3 method for class 'xreg2' summary(object, ...)
object |
A fitted |
... |
Currently unused. |
Summary method for xreg2 objects
object invisibly.
Two-sample t-test from summary statistics
t_test2(m1, m2, s1, s2, n1, n2, m0 = 0, equal.variance = FALSE)t_test2(m1, m2, s1, s2, n1, n2, m0 = 0, equal.variance = FALSE)
m1 |
Numeric; mean of sample 1. |
m2 |
Numeric; mean of sample 2. |
s1 |
Numeric; SD of sample 1. |
s2 |
Numeric; SD of sample 2. |
n1 |
Numeric; size of sample 1. |
n2 |
Numeric; size of sample 2. |
m0 |
Numeric; null difference. Default |
equal.variance |
Logical; assume equal variances. Default |
Named numeric vector with test statistics and confidence interval.
t-statistic for model comparison
tstat(est, obs, ...) ## S3 method for class 'data.frame' tstat(est, obs, ...) ## S3 method for class 'matrix' tstat(est, obs, ...) ## S3 method for class 'numeric' tstat(est, obs, ...)tstat(est, obs, ...) ## S3 method for class 'data.frame' tstat(est, obs, ...) ## S3 method for class 'matrix' tstat(est, obs, ...) ## S3 method for class 'numeric' tstat(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Currently unused. |
Numeric t-statistic.
Uncertainty at 95 Percent
U95(est, obs, ...) ## S3 method for class 'data.frame' U95(est, obs, ...) ## S3 method for class 'matrix' U95(est, obs, ...) ## S3 method for class 'numeric' U95(est, obs, ...)U95(est, obs, ...) ## S3 method for class 'data.frame' U95(est, obs, ...) ## S3 method for class 'matrix' U95(est, obs, ...) ## S3 method for class 'numeric' U95(est, obs, ...)
est |
Numeric; estimates. |
obs |
Numeric; observations. |
... |
Passed to |
Numeric U95 value.
Computes the negative log-likelihood for a censored/interval
regression model supporting multiple distributions (normal, lognormal,
exponential, beta, gamma, weibull). Designed as a p_fun argument
for xreg2_control.
xreg2_cont( par, dtalist, log.p = TRUE, aggregate.p = TRUE, print_pars = FALSE, dist = "normal" )xreg2_cont( par, dtalist, log.p = TRUE, aggregate.p = TRUE, print_pars = FALSE, dist = "normal" )
par |
Named numeric vector of current parameter values. |
dtalist |
Internal data list produced by |
log.p |
Logical; use log-scale probabilities. Default |
aggregate.p |
Logical; return scalar negloglik. Default |
print_pars |
Logical; print parameters and value. Default |
dist |
Character; distribution family. One of |
General continuous log-likelihood for xreg2
Scalar negative log-likelihood (when aggregate.p = TRUE).
# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont, # start_values = c(INTERCEPT = 0, SLOPE = 0, LN_SIGMA = 0))# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont, # start_values = c(INTERCEPT = 0, SLOPE = 0, LN_SIGMA = 0))
Computes the negative log-likelihood for a censored/interval
normal regression model. Designed as a p_fun argument for
xreg2_control.
xreg2_cont_normal( par, dtalist, log.p = TRUE, aggregate.p = TRUE, print_pars = FALSE )xreg2_cont_normal( par, dtalist, log.p = TRUE, aggregate.p = TRUE, print_pars = FALSE )
par |
Named numeric vector of current parameter values. |
dtalist |
Internal data list produced by |
log.p |
Logical; use log-scale probabilities. Default |
aggregate.p |
Logical; return scalar negloglik. Default |
print_pars |
Logical; print parameters and value. Default |
Continuous normal log-likelihood for xreg2
Scalar negative log-likelihood (when aggregate.p = TRUE).
# Typically used as p_fun in xreg2_control(): # ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont_normal)# Typically used as p_fun in xreg2_control(): # ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont_normal)
Computes the negative log-likelihood for a mixed-effects
log-normal regression model with a random intercept, integrated via
Gauss-Hermite quadrature. Designed as a p_fun argument for
xreg2_control.
xreg2_cont_r_lnorm(par, dtalist)xreg2_cont_r_lnorm(par, dtalist)
par |
Named numeric vector of current parameter values. Must include
either |
dtalist |
Internal data list produced by |
Continuous log-normal log-likelihood with random intercept
Scalar negative log-likelihood.
# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont_r_lnorm)# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont_r_lnorm)
Computes the negative log-likelihood for a mixed-effects normal
regression model with a random intercept, integrated via Gauss-Hermite
quadrature. Designed as a p_fun argument for
xreg2_control.
xreg2_cont_r_normal(par, dtalist, print_pars = FALSE)xreg2_cont_r_normal(par, dtalist, print_pars = FALSE)
par |
Named numeric vector of current parameter values. Must include
either |
dtalist |
Internal data list produced by |
print_pars |
Logical; print parameters and value. Default |
Continuous normal log-likelihood with random intercept
Scalar negative log-likelihood.
# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont_r_normal)# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_cont_r_normal)
Creates a control object specifying the model formula(s),
likelihood function, gradient function, and starting values for one
component of a joint xreg2 model.
xreg2_control( formulas, start_values = numeric(), fixed_values = numeric(), p_fun = xreg2_cont_normal, grad_fun = function(par, ...) numDeriv::grad(func = p_fun, x = par, method = "simple", ...), p_aggregation_fun = function(d_df) return(-d_df$p * d_df$internal_count), weights_var = NA, name = NA, censor_bounds = c(-Inf, Inf), lower = NA, upper = NA )xreg2_control( formulas, start_values = numeric(), fixed_values = numeric(), p_fun = xreg2_cont_normal, grad_fun = function(par, ...) numDeriv::grad(func = p_fun, x = par, method = "simple", ...), p_aggregation_fun = function(d_df) return(-d_df$p * d_df$internal_count), weights_var = NA, name = NA, censor_bounds = c(-Inf, Inf), lower = NA, upper = NA )
formulas |
A formula or list of formulas. The left-hand side names the dependent (or intermediate) variable; the right-hand side is evaluated in the context of parameter values and data columns. |
start_values |
Named numeric vector of starting values. An existing
|
fixed_values |
Named numeric vector of parameter values held fixed during optimisation. |
p_fun |
Log-likelihood function. Default |
grad_fun |
Gradient function. Default uses |
p_aggregation_fun |
Aggregation function applied to the likelihood data-frame. Rarely changed by users. |
weights_var |
Character name of a column in the data used as
observation weights. |
name |
Optional character label for this component. |
censor_bounds |
Numeric vector of length 2 giving lower and upper
censoring bounds. Default |
lower |
Named numeric vector of lower parameter bounds (for bounded optimisers). |
upper |
Named numeric vector of upper parameter bounds. |
Create an xreg2 control object
An object of class c("xreg2Control", "list").
ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), p_fun = xreg2_cont_normal, name = "continuous" )ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), p_fun = xreg2_cont_normal, name = "continuous" )
Computes the negative log-likelihood for a binary logistic
regression model. Designed as a p_fun argument for
xreg2_control.
xreg2_dich_logistic(par, dtalist)xreg2_dich_logistic(par, dtalist)
par |
Named numeric vector of current parameter values. |
dtalist |
Internal data list produced by |
Dichotomous logistic log-likelihood for xreg2
Scalar negative log-likelihood.
# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_dich_logistic, # start_values = c(INTERCEPT = 0, SLOPE = 0))# ctrl <- xreg2_control(formulas = list(y ~ INTERCEPT + x * SLOPE), # p_fun = xreg2_dich_logistic, # start_values = c(INTERCEPT = 0, SLOPE = 0))
Fits a flexible maximum-likelihood regression model supporting
censored, interval, and hybrid data. Accepts a list of
xreg2_control objects and a matching named list of
data frames.
xreg2_fit( controlList, dataList = NULL, start_values = numeric(), fixed_values = numeric(), latent_classes = 0, latent_class_parameters = character(), latent_id_colname = character(), return_type = "fit", print_sum = FALSE, method = "ucminf", hessian = TRUE, run_from = NULL, ... )xreg2_fit( controlList, dataList = NULL, start_values = numeric(), fixed_values = numeric(), latent_classes = 0, latent_class_parameters = character(), latent_id_colname = character(), return_type = "fit", print_sum = FALSE, method = "ucminf", hessian = TRUE, run_from = NULL, ... )
controlList |
An |
dataList |
Named list of data frames, one per component. Names must
match those of |
start_values |
Optional named numeric vector of starting values,
overriding those in |
fixed_values |
Named numeric vector of parameter values held fixed. |
latent_classes |
Integer; number of latent classes. |
latent_class_parameters |
Character vector of parameter names that vary by latent class. |
latent_id_colname |
Character name of the grouping column for latent classes. |
return_type |
Character; |
print_sum |
Logical; print summary during optimisation. Default
|
method |
Character; optimisation method. Default |
hessian |
Logical; compute Hessian for standard errors. Default
|
run_from |
Internal; character string identifying the calling wrapper
(e.g. |
... |
Additional arguments passed to the optimiser. |
Fit a flexible maximum-likelihood regression model
An object of class "xreg2" with fitted coefficients,
standard errors, log-likelihoods, and counts.
xreg2_control, xreg2_optim,
predict.xreg2
set.seed(1) df <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200)) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), name = "main" ) fit <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df)) coef(fit)set.seed(1) df <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200)) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), name = "main" ) fit <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df)) coef(fit)
Simplified version of xreg2_fit that accepts
data already containing ub (upper bound) and lb (lower
bound) columns instead of requiring data pre-processing.
xreg2_fit_s( controlList, dataList = NULL, start_values = numeric(), fixed_values = numeric(), latent_classes = 0, latent_class_parameters = character(), latent_id_colname = character(), return_type = "fit", print_sum = FALSE, method = "ucminf", hessian = TRUE, ... )xreg2_fit_s( controlList, dataList = NULL, start_values = numeric(), fixed_values = numeric(), latent_classes = 0, latent_class_parameters = character(), latent_id_colname = character(), return_type = "fit", print_sum = FALSE, method = "ucminf", hessian = TRUE, ... )
controlList |
An |
dataList |
Named list of data frames, one per component. Names must
match those of |
start_values |
Optional named numeric vector of starting values,
overriding those in |
fixed_values |
Named numeric vector of parameter values held fixed. |
latent_classes |
Integer; number of latent classes. |
latent_class_parameters |
Character vector of parameter names that vary by latent class. |
latent_id_colname |
Character name of the grouping column for latent classes. |
return_type |
Character; |
print_sum |
Logical; print summary during optimisation. Default
|
method |
Character; optimisation method. Default |
hessian |
Logical; compute Hessian for standard errors. Default
|
... |
Additional arguments passed to the optimiser. |
Fit an xreg2 model from pre-structured data
An object of class "xreg2".
set.seed(1) # xreg2_fit_s expects pre-structured data with lb and ub columns. # Use lb == ub for exact (uncensored) observations. y <- rnorm(100, 2, 0.5) df <- data.frame(y = y, x = rnorm(100), lb = y, ub = y) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0, LN_SIGMA = 0), name = "main" ) fit <- xreg2_fit_s(controlList = c(ctrl), dataList = list(main = df))set.seed(1) # xreg2_fit_s expects pre-structured data with lb and ub columns. # Use lb == ub for exact (uncensored) observations. y <- rnorm(100, 2, 0.5) df <- data.frame(y = y, x = rnorm(100), lb = y, ub = y) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0, LN_SIGMA = 0), name = "main" ) fit <- xreg2_fit_s(controlList = c(ctrl), dataList = list(main = df))
High-level wrapper around xreg2_fit replicating
the interface of the STATA hyreg command and
xreg::hyreg(). Simultaneously fits a censored continuous model
(normal or logistic) and a dichotomous model (logistic or normal) to a
single data frame that contains both observation types.
xreg2_hyreg( formula, df, datatype = "method", init = numeric(), contdist = "normal", dichdist = "logistic", hetcont = NULL, hetdich = NULL, ul = Inf, ll = -Inf, lntheta = TRUE, lnsigma = TRUE, dichformula = NULL, ... )xreg2_hyreg( formula, df, datatype = "method", init = numeric(), contdist = "normal", dichdist = "logistic", hetcont = NULL, hetdich = NULL, ul = Inf, ll = -Inf, lntheta = TRUE, lnsigma = TRUE, dichformula = NULL, ... )
formula |
A formula or list of formulas for the continuous component.
The dependent variable (left-hand side) must match a column in |
df |
A |
datatype |
Character; name of the column that distinguishes continuous
( |
init |
Optional named numeric vector (or |
contdist |
Character; distribution for the continuous component.
|
dichdist |
Character; distribution for the dichotomous component.
|
hetcont |
Optional formula for heteroscedastic standard deviations in
the continuous component. If |
hetdich |
Optional formula for heteroscedastic standard deviations in
the dichotomous component. If |
ul |
Numeric; upper censoring bound. Default |
ll |
Numeric; lower censoring bound. Default |
lntheta |
Logical; exponentiate theta prior to fitting (matches STATA
default). Default |
lnsigma |
Logical; exponentiate sigma prior to fitting (matches STATA
default). Default |
dichformula |
Optional formula for the dichotomous component. Defaults
to |
... |
Additional arguments forwarded to |
Hybrid regression wrapper for xreg2
An object of class "xreg2".
set.seed(1) n <- 300 df <- data.frame( value = c(rnorm(200, 0.6, 0.3), rep(NA, 100)), age = rnorm(n), method = c(rep(TRUE, 200), rep(FALSE, 100)) ) df$value[df$method == FALSE] <- as.integer(df$age[df$method == FALSE] > 0) fit <- xreg2_hyreg( formula = value ~ INTERCEPT + age * AGE, df = df, datatype = "method" ) print(fit)set.seed(1) n <- 300 df <- data.frame( value = c(rnorm(200, 0.6, 0.3), rep(NA, 100)), age = rnorm(n), method = c(rep(TRUE, 200), rep(FALSE, 100)) ) df$value[df$method == FALSE] <- as.integer(df$age[df$method == FALSE] > 0) fit <- xreg2_hyreg( formula = value ~ INTERCEPT + age * AGE, df = df, datatype = "method" ) print(fit)
Low-level optimisation routine called by xreg2_fit
and xreg2_fit_s. Can also be called directly on an
xreg2_obj to re-optimise with different settings.
Optimizer routing:
"ucminf" (default) — uses ucminf with central-difference
gradients and high-precision tolerances.
"L-BFGS-B", "BFGS", "CG", "Nelder-Mead",
"SANN", "Brent" — uses stats::optim().
Box constraints (lower/upper set in
xreg2_control) are enforced for "L-BFGS-B".
Any other method string — requires the optimx package (listed
in Suggests) and dispatches via optimx::optimr().
Method-specific control defaults (high-precision) are set automatically and
can be overridden by passing optim_control = list(...) to
xreg2_fit.
xreg2_optim( xreg2obj, method = NULL, hessian = TRUE, fixed_values = NULL, optim_control = NULL, ... )xreg2_optim( xreg2obj, method = NULL, hessian = TRUE, fixed_values = NULL, optim_control = NULL, ... )
xreg2obj |
An |
method |
Character; optimisation method. |
hessian |
Logical; compute Hessian. Default |
fixed_values |
Optional named numeric vector of additional fixed values. |
optim_control |
Optional named list of control parameters that override
the method-specific defaults set by |
... |
Additional arguments passed to the optimiser objective function. |
Optimise an xreg2 model object
An object of class "xreg2".
set.seed(1) df <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200)) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), name = "main" ) obj <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df), return_type = "control") fit <- xreg2_optim(obj, method = "ucminf") fit_lbfgsb <- xreg2_optim(obj, method = "L-BFGS-B")set.seed(1) df <- data.frame(y = rnorm(200, 2, 0.5), x = rnorm(200)) ctrl <- xreg2_control( formulas = list(y ~ INTERCEPT + x * SLOPE), start_values = c(INTERCEPT = 0, SLOPE = 0), name = "main" ) obj <- xreg2_fit(controlList = c(ctrl), dataList = list(main = df), return_type = "control") fit <- xreg2_optim(obj, method = "ucminf") fit_lbfgsb <- xreg2_optim(obj, method = "L-BFGS-B")