Model specification for DEFM
td_ones(m, covar = "")
td_generic(m, mat, covar = "")
td_formula(m, formula, new_name = "")
td_logit_intercept(m, y_indices = as.integer(c()), covar = "")
rule_not_one_to_zero(m, term_indices)
rule_constrain_support(m, term_index, lb, ub)
# S3 method for class 'DEFM'
e1 + e2
An object of class DEFM.
String. Name of a covariate to use as an interaction
for the effect. If equal to ""
, then no interaction effect.
is used.
used to weight the term.
Integer matrix. The matrix specifies the type of motif to capture (see details.)
Character scalar (see details).
Character scalar. Name to be assigned for the new term. if empty, then it builds a name based on the formula.
Integer vector with the coordinates to include in the term.
Non-negative vector of indices. Indicates which outcomes this rule will apply.
Non-negative scalar. Which term this rule will apply.
Numeric scalars. Lower and upper bounds.
e1 An object of class DEFM (e1) and a character (e2).
Invisible 0.
In td_generic
, users can specify a particular motif to model. Motifs
are represented by cells with values equal to 1, for example, the matrix:
represents a transition y0 -> (y1, y2)
. If 0 is a motif of interest, then
the matrix should include 0 to mark zero values.
The function td_formula
,
will take the formula and generate the corresponding
input for defm::counter_transition(). Formulas can be specified in the
following ways:
Intercept effect: {...}
No transition, only including the current state.
Transition effect: {...} > {...}
Includes current and previous states.
The general notation is [0]y[column id]_[row id]
. A preceeding zero
means that the value of the cell is considered to be zero. The column
id goes between 0 and the number of columns in the array - 1 (so it
is indexed from 0,) and the row id goes from 0 to m_order.
Both Intercepts and Transition can interact with covariates. Using
either the covar
argument or, in the case of formulas, x [Covar name]
,
for example:
Intercept effect: {...} x Hispanic
interacts with the Hispanic covar.
Transition effect: {...} > {...} x Hispanic
Same.
Intercept effects only involve a single set of curly brackets. Using the
'greater-than' symbol (i.e., <
) is only for transition effects. When
specifying intercept effects, users can skip the row_id
, e.g.,
y0_0
is equivalent to y0
. If the passed row id
is different from
the Markov order, i.e., row_id != m_order
, then the function returns
with an error.
Examples:
"{y0, 0y1}"
is equivalent to set a motif with the first element equal
to one and the second to zero.
Transition effects can be specified using two sets of curly brackets and
an greater-than symbol, i.e., {...} > {...}
. The first set of brackets,
which we call LHS, can only hold row id
that are less than m_order
.
The term td_logit_intercept
will add what is equivalent to an
intercept in a logistic regression. When y_indices
is specified, then the
function will add one intercept per outcome. These can be weighted by
a covariate.
The function rule_not_one_to_zero
will avoid the transition one to zero in a Markov process.
The function rule_constrain_support
will constrain the support of the model
by specifying a lower and upper bound for a given statistic.
The +
method is a shortcut for term_formula
# Loading Valtente's SNS data
data(valentesnsList)
mymodel <- new_defm(
id = valentesnsList$id,
Y = valentesnsList$Y,
X = valentesnsList$X,
order = 1
)
# Conventional regression intercept
td_logit_intercept(mymodel)
# Interaction effect with Hispanic
td_logit_intercept(mymodel, covar = "Hispanic")
# Transition effect from only y1 to both equal to 1.
td_formula(mymodel, "{y1, 0y2} > {y1, y2}")
# Same but interaction with Female
td_formula(mymodel, "{y1, 0y2} > {y1, y2} x Female")
# Inspecting the model
mymodel
#> Num. of Arrays : 0
#> Support size : -
#> Support size range : -
#> Arrays in powerset : 0
#> Transform. Fun. : no
#> Model terms (8) :
#> - Logit intercept alcohol
#> - Logit intercept tobacco
#> - Logit intercept mj
#> - Logit intercept alcohol x Hispanic
#> - Logit intercept tobacco x Hispanic
#> - Logit intercept mj x Hispanic
#> - Motif {tobacco+, mj-}⇨{tobacco+, mj+}
#> - Motif {tobacco+, mj-}⇨{tobacco+, mj+} x Female
#> Model Y variables (3):
#> 0) alcohol
#> 1) tobacco
#> 2) mj
# Initializing and fitting
init_defm(mymodel)
defm_mle(mymodel)
#>
#> Call:
#> stats4::mle(minuslogl = minuslog, start = start, method = "L-BFGS-B",
#> nobs = nrow_defm(object) + ifelse(morder_defm(object) > 0,
#> -nobs_defm(object), 0L), lower = lower, upper = upper)
#>
#> Coefficients:
#> Logit intercept alcohol
#> -1.4021661
#> Logit intercept tobacco
#> -2.8318789
#> Logit intercept mj
#> -3.0995487
#> Logit intercept alcohol x Hispanic
#> 1.5999458
#> Logit intercept tobacco x Hispanic
#> 0.9043077
#> Logit intercept mj x Hispanic
#> 1.7451446
#> Motif {tobacco+, mj-}⇨{tobacco+, mj+}
#> 3.6516810
#> Motif {tobacco+, mj-}⇨{tobacco+, mj+} x Female
#> 0.1025614