Consider a statistical problem where you have a
response variable that you want to describe conditional on an
explanatory variable and a
nested variable, where the nested variable only arises as a meaningful variable for particular values of the explanatory variable. In cases where the explanatory variable does not admit a meaningful nested variable, the latter is usually coded either as
NA in the data set, or if it is coded with a value, that value is merely a placeholder that does not have any meaningful interpretation.
This situation tends to arise whenever you have an explanatory variable indicating the existence of a thing, and one or more nested variables describing the characteristics of that thing. Some examples of this kind of situation in statistical problems are the following:
The explanatory variable is an indicator of whether a survey participant is
married, and the nested variable is some
characteristic of the spouse(e.g., education, age, etc.);
The explanatory variable is an indicator of the
presence of an itemin a space, and the nested variable is a measure of some
characteristic of the item(e.g., size, distance, etc.);
The explanatory variable is an indicator of the occurrence of an
eventand the nested variable is a description of some
characteristic of the event(e.g., duration, magnitude, etc.).
In these kinds of situations, we often want to build a regression-type model (in the broad sense that includes GLMs, GLMMs, etc.) describing the relationship between the response variable and the other variables. It is not obvious how to deal with the nested variable in this type of model.
Question: How do we deal with the
nested variable in this type of model?
Note: This question is designed to give a generalised answer to a recurring question on CV.SE regarding nested variables in regression (see e.g., here, here, here and here). This question is designed to give a generalised context-independent example of this problem.