Feature that is numeric if true, or single value if false In regards to feature engineering for machine learning models.  I would like to engineer a feature that encodes the following:


*

*value can be true and if so it will measure a numeric (maybe continuous) e.g.  'weeks since transaction', 'time since marriage'

*however, it is possible that this event never occurred or this value doesn't apply. e.g. transaction never occurred, person never married


How might I design this feature.  I can see a couple of options:


*

*Have a continuous positive variable, but a negative value for the 'false' values

*Have a continuous variable with NA for the 'false' values

*Use two separate features in some way.


My guess is 1. runs afoul of the mechanics of most algorithms, 2. doesn't work well with logistic regression without imputing something for the NA, and I don't know about 3. (Is there a name for this type of variable?)
 A: This sort of thing happens a lot in survey data. Question 1: Are you married. Question 2: If you answered yes to question 1, how long have you been married? I feel your pain.
I usually go with option 3: I code a binary for yes/no bit and then a continuous variable for the "how long have you been married" bit. Note that the continuous part of option 3 is the same as option 2. Option 1 involves "making the data up", which I try not to do.
First, I analyse the full data set with just the binary variable and hope that this gives me something useful. At a minimum, it's a good filter. If marital status does not affect the outcome, then length of marriage may not affect it either. Problem solved.
If I'm interested in "length of marriage", I'm going to select out those cases that are married and limit my analysis to them. If I believe that "length of marriage" affects my response, and I'm interested in studying that issue, unmarried people really have no information to give me on that point. So I exclude them for that piece.
There is an option 4 that sometimes works, and that is to build an ordinal variable that combines both portions. Recode 1 for unmarried, 2 for married for less than five years; 3 for married for five years or more. Obviously, this only works when the "no" answer can be viewed as a lower bound to the "yes" case. This won't work, for example, if the questions were Q1, did you take Prof. Smith's machine learning course? Q2. If "Yes", rate it on a scale of 1 through 10.
There is a reason why machine learning algorithms collapse under the kind of data you describe. Basically, you are dealing with two different sample spaces -- one space for the unmarried people; one space for the married people, about whom we have additional information. So the structure of the problem is different for the two population groups. Statistical models assume one sample space, one sigma algebra, one probability measure per customer. You actually have two models going here, and I am not aware of any clean way to bridge that gap.
