# Time spent in an activity as an independent variable

I want to include time spent doing something (weeks breastfeeding, for example) as an independent variable in a linear model. However, some observations do not engage in the behavior at all. Coding them as 0 isn't really right, because 0 is qualitatively different from any value >0 (i.e. women who don't breastfeed may be very different from women who do, even those who don't do it for very long). The best I can come up with is a set of dummies that categorizes the time spent, but this is a waste of precious information. Something like zero-inflated Poisson also seems like a possibility, but I can't exactly figure out what that would look like in this context. Does anyone have any suggestions?

To expand a bit on the answer of @ken-butler. By adding both the continuous variable (hours) and an indicator variable for a special value (hours = 0, or non-breastfeeding), you think that there is a linear effect for the "non-special" value and a discrete jump in the predicted outcome at the special value. It helps (for me at least) to look at a graph. In the example below we model hourly wage as a function of hours per week that the respondents (all females) work, and we think that there is something special about "the standard" 40 hours per week:

The code that produced this graph (in Stata) can be found here: http://www.stata.com/statalist/archive/2013-03/msg00088.html

So in this case we have assigned the continuous variable a value 40 even though we wanted it to be treated differently from the other values. Similarly, you would give your weeks breastfeeding the value 0 even though you think it is qualitatively different from the other values. I interpret your comment below that you think that this is a problem. This is not the case and you do not need to add an interaction term. In fact, that interaction term will be dropped due to perfect collinearity if you tried. This is not a limitation, it just tells you that the interaction terms does not add any new information.

Say your regression equation looks like this:

$$\hat{y} = \beta_1 weeks\_breastfeeding + \beta_2 non\_breastfeeding + \cdots$$

Where $weeks\_breastfeeding$ is the number of weeks breastfeeding (including the value 0 for those that do not breastfeed) and $non\_breastfeeding$ is an indicator variable that is 1 when someone does not breastfeed and 0 otherwise.

Consider what happens when someone is breastfeeding. The regression equation simplifies to:

$$\hat{y} = \beta_1 weeks\_breastfeeding + \beta_2 0 + \cdots \\ = \beta_1 weeks\_breastfeeding + \cdots$$

So $\beta_1$ is just a linear effect of the number of weeks breastfeeding for those that do breastfeed.

Consider what is hapening when someone is not breastfeeding:

$$\hat{y} = \beta_1 0 + \beta_2 1 + \cdots \\ = \beta_2 + \cdots$$

So $\beta_2$ gives you the effect of not breastfeeding and the number of weeks breastfeeding drops from the equation.

You can see that there is no use to add an interaction term, as that interaction term is already (implicitly) in there.

There is however something weird about $\beta_2$ though, as it measures the effect of breastfeeding by comparing the expected outcome of those who do not breastfeed with those that breastfeed but do so only 0 weeks... It kind of makes sense in a "compare like with like" sort of way, but the practical usefulness is not immediatly obvious. It may make more sense to compare the "non-breastfeeders" with those women that were breastfeeding 12 weeks (approx. 3 months). In that case you just give the "non-breastfeeders" the value 12 for $weeks\_breastfeeding$. So the value you assigning to $weeks\_breastfeeding$ for the "non-breastfeeders" does influence the regression coefficient $\beta_2$ in the sense that it determines with whom the "non-breastfeeders" are compared. Instead of a problem, this is actually something that can be quite useful.

• I appreciate the answer (and the others), but I'm having a hard time accepting it. If I include a 1:0, and the continuous time variable, I still have to assign the non-breast-feeders a value for time (or else they drop for a missing co-variate). Even conditional on the 1:0 variable, I don't see how including the non-breast-feeders as time=0 doesn't affect the regression coefficient. Perhaps also adding the product interaction term between the two would make more sense? – D L Dahly Apr 18 '13 at 11:12
• @DLDahly I have edited my answer to deal with these doubts – Maarten Buis Apr 19 '13 at 9:16
• Ok, that's very helpful. Let me ask one more quick follow-up...if I am understanding you correctly, then the estimated value for B1 should be the same regardless of what time-value I give the B2=1 people. Is that right? – D L Dahly Apr 19 '13 at 12:05
• Very nice response Maarten. Here is a similar question/answer on the site that shows a similar situation in including an independent variable that only pertains to a particular subgroup. – Andy W Apr 19 '13 at 12:07
• @GavinM.Jones I never thought of the need to name it or to cite this: it is just a straightforward application of continuous and indicator variables. Consequently I don't have a good reference for you. The closes thing I could quickly dig up is Treiman, D. J. (2009): Quantitative Data Analysis. Doing Social Research to Test Ideas. San Francisco: Jossey-Bass. , chapter 7 discussed something similar. The model contains a constant. – Maarten Buis Jun 3 '19 at 7:32

Something simple: represent your variable by a 1/0 indicator for any/none, and the actual value. Put both into the regression.

If you put a binary indicator for any-time-spent(=1) vs no-time-spent(=0) and then have the amount of time spent as a continuous variable, the different effect of "0" times will be "picked up" by the 0-1 indicator

You can use mixed-effects models with a grouping that is based in 0 time vs nonzero time, and keep your independent variable

• Could you please expand on this a bit? Many thanks. – D L Dahly Apr 18 '13 at 11:13
• a mixed effects model assumes that there a factor that divides the data into different (heterogeneous) buckets, in each of which we might have a different relationship between explanatory and dependent variables (either in terms if intercept or intercept and slope/coefficient). en.wikipedia.org/wiki/Mixed_model – rezakhorshidi Apr 18 '13 at 11:17
• So use individuals, nested in breastfeeding status, and then a random slope on the weeks-breastfeeding? I could do this as an SEM easily enough and test certain constraints. Thanks +1 – D L Dahly Apr 18 '13 at 11:29

If you are using Random Forest or Neural Network putting this number as 0 is OK, because they will be able to figure out that 0 is distinctly different from other values (if it is in fact different). Other way around is adding of a categorical variable yes/no in addition to time variable.

But all in all, in this particular case I don't see a real issue - 0.1 weeks of breastfeeding is close to 0 and effect will be very similar, so it looks like a pretty continuous variable to me with 0 not standing out as something distinct.

• +1 for the first paragraph but dealing with social science or medical data, the effect of 0 vs. 0.1 weeks of something is not the main worry. The point is that women who do not attempt or report breastfeeding at all might be systematically different in a lot of other respects (health problems, income, family situation, ability to stay out of work, access to health services, where they obtained information about parenting, etc.) There is really no reason to believe that these women are very similar to mothers who try breastfeeding and stop it quickly. – Gala Apr 17 '13 at 9:21
• From a statistical standpoint, it would be better to put these other variables explicitly in your model but it makes sense to be careful with assuming there is nothing special going on at 0, I think. – Gala Apr 17 '13 at 9:27

Tobit model is what you want, I think.

• Tobits are used when the outcome is censored above or below some threshold. For example, we don't observe any wages below the minimum wage or incomes above some top coded value. This application is for an independent variable. – Dimitriy V. Masterov Apr 16 '13 at 23:29