frml: like tda_fml, but for episode (survival)
data rather than plain cross-sectional rows – fn is the
log-likelihood contribution of one episode, and can refer to
time (the position within it) the way tda_rate's
own define can. This is not fml under another name:
the same definitions that work with fml on plain rows fail
here with “no episode data defined” until an actual episode
structure (survival::Surv()) is given.
Arguments
- formula
a
Surv(...)formula, the same astda_rate's.- data
a data frame.
- definitions
a character vector of assignments, evaluated in order inside TDA, the last of which must define
fn– seetda_fmlfor the same convention on plain data, and the description above for the episode-specific variables available here. Or an unevaluated{ }block of plain R assignments instead, translated automatically the same waytda_fml'sdefinitionsis. Names on the right that are neither a covariate fromformulanor one of TDA's built-ins, and are not defined earlier, are parameters to be estimated.formula'sSurv(...)arguments can also be referenced directly by their real column name when they are a simple column reference (Surv(TFP, DES)'sTFP/DES, say, work the same as TDA's built-instf - ts/des) – matching how TDA's manual writes this (examples/exam/frml1.cf, referencing its ownDES/DURcolumns directly, not through any built-in alias). A computedSurv()argument (Surv(TFin - TStart + 1, DES)) has no one column to translate to, so only TDA's built-ins reach it.- start
optional named list of starting values, in the order the definitions introduce the parameters.
- id, spell
columns identifying the case and numbering its spells, for multi-episode data – the same pair
tda_ratetakes; a case's spells then enter the likelihood together.strata()in the formula is refused rather than accepted:fmlhas no stratification mechanism and would silently ignore the term (fit per stratum instead).- control
optional
tda_control.- constraints
optional linear constraints on the parameters –
frml'scon=, the identical mechanism andbN-by-position convention astda_fml'sconstraints(see there for the full explanation and a worked example of the naming trap); it works the same way here.- residuals
ask TDA to also compute, per episode, its contribution to
fnat the converged parameters –frml's ownpres=, the identical mechanism astda_fml's ownresiduals(see there for what it actually is: not a classical observed-minus-fitted residual), computed per episode here.- residual_vars
with
residuals = TRUE, extra columns to write alongsidefn's value, one per episode –frml's ownv=, the same mechanism astda_fml'sresidual_vars. Only reliably supports a covariate fromformula; TDA's episode built-ins (ts,tf,org,des,time) cannot be named here, despite being available insidefn: they are expression-only aliases, not real data-matrix variables –v = ts,tfis a TDA syntax error (their real, differently-named underlying variables, e.g.Tstartforts, do work, but are internal, auto-generated byedef()and not something to build an interface around safely).- protocol
ask TDA to also write its iteration-by-iteration diagnostic log –
frml'sprot=, the same mechanism astda_fml'sprotocol.- options
a named list of further TDA options, passed through.
- dir
working directory.
- ...
passed to
tda_run.
Details
fn's expression cannot reference the raw duration/status columns
from data directly – Surv()'s columns are consumed
by edef() into TDA's internal episode representation, and are
not separately visible by name afterward. An
fn written against the original column names fails with those
names listed as parameters to estimate (TDA's convention for any
identifier it does not otherwise recognise), not as a lookup error.
Use TDA's built-in accessors for an episode instead: ts and
tf (start and finish time), org and des (origin
and destination state), time (tf - ts, and the same
built-in tda_rate's define can use). A
covariate, by contrast, is referenced by its own name from
formula, translated automatically the same way
tda_fml does it.
See also
Other sequence analysis:
tda_evalfi(),
tda_fml(),
tda_seq_info(),
tda_seqgc(),
tda_seqm(),
tda_seqmd(),
tda_seqpe(),
tda_seqpm()
Examples
set.seed(1)
n <- 200
d <- data.frame(x = rnorm(n))
haz <- exp(0.5 + 0.8 * d$x)
d$dur <- rexp(n, haz)
d$status <- 1
# the exponential hazard model, written out by hand -- matches
# tda_rate(Surv(dur, status) ~ x, d, model = "exponential"). des and
# (tf - ts) are TDA's built-ins, not d's dur/status columns -- see
# Details.
fit <- tda_frml(Surv(dur, status) ~ x, d,
c("xb = b0 + x * b1", "gamma = exp(xb)",
"fn = des * xb - gamma * (tf - ts)"))
coef(fit)
#> b0 b1
#> 0.5387063 0.7930654
coef(tda_rate(Surv(dur, status) ~ x, d, model = "exponential"))
#> Constant x
#> 0.5387064 0.7930655
# the same model, written in R syntax instead
fit2 <- tda_frml(Surv(dur, status) ~ x, d, {
xb = b0 + x * b1
gamma = exp(xb)
fn = des * xb - gamma * (tf - ts)
})
coef(fit2)
#> b0 b1
#> 0.5387063 0.7930654