5
$\begingroup$

Imagine I am trying to fit a multilevel model on products, and want to group by product type.

In cases where product types have all the same predictors this is straight-forward. E.g. you might estimate the effect of color on sales or something similar.

But what if some predictors only make sense for some of the product types? Like a "leg length" feature might make sense for shorts if people have preference for how far above or below the knee they like their shorts, but not for pants which are always full length. And it may make yet less sense for shirts, since the do not at all have a leg length.

In that case, is there a way to handle that or is it best to have different models per group? For the features that are shared and are expected to be drawn from the same distribution, I guess we lose some benefit there, so that's why I'm wondering if the models can be done as a single model.

I've thought about a number of things (e.g. for products that don't have the feature, setting it to a constant value, or to a random value drawn from a distribution of feature values from products where the feature does make sense, etc) but all seem to have very obvious problems.

$\endgroup$
10
  • $\begingroup$ What question are you trying to answer with this model? $\endgroup$
    – Noah
    Commented Aug 22, 2020 at 8:27
  • $\begingroup$ I'm trying to get coefficients for different features as described in the question, their impact on sales. $\endgroup$
    – CHP
    Commented Aug 22, 2020 at 8:42
  • $\begingroup$ It's not possible to include variables in your model for which there are missing observations - what do you expect the fitting algorithm to do at these values? So I'd say (1) either you choose an imputation that you can justify given the data and question, (2) or you drop the variables for which not all products have values from your global model, (3) or you fit separate models for product types. Note that after doing (3) you can still combine posterior distributions from different models for those features all products have in common $\endgroup$
    – stefgehrig
    Commented Aug 23, 2020 at 9:27
  • $\begingroup$ They're not 'missing' though so they can't be imputed, shirts just don't have legs, hence no leg length. Conceptually, I could build a different model for each product, with different predictors. So it seems like in a hierarchical model I should be ale to mathematically define a model so that predictors for some groups differ from other groups IF those predictors are treated as non-nested, since in that case they don't impact other groups. $\endgroup$
    – CHP
    Commented Aug 23, 2020 at 10:08
  • $\begingroup$ Something similar to what you say in 3, except fitted together since the non-common predictors will still eat up variance away from other predictors in the groups that DO have them, thereby affecting the distribution of values of the nested predictors. $\endgroup$
    – CHP
    Commented Aug 23, 2020 at 10:16

2 Answers 2

2
+50
$\begingroup$

There's a pretty sensible way of doing this if you centre all your predictors by subtracting the mean value.

Using your example, say you have athletic shorts, shorts, and jeans, and your numeric predictors are a) brightness (defined for all categories), and b) short length (defined for shorts only, NA for trousers). Now, if you centre your both your numeric predictors, you get measures of a) whether the items are darker or lighter than average, and b) whether the shorts are shorter or longer than average. You can safely say that the trousers are $\pm0$ cm shorter or longer than average, and so impute a value of 0 for this predictor for all items that aren't shorts (they're neither shorter nor longer than average). You can then fit a multilevel as you normally would, allowing all predictors to vary across categories:

lmer(sales ~ 1 + centred_colour + centred_leg_length + 
             (1 + centred_colour + centred_leg_length | category),
     data=sales_data)

Since this predictor only varies for the different kinds of shorts, only sales of those items will have an effect on this parameter. Since it's set to 0 for all other products, this predictor won't affect inferences or predictions about them.

If it happens that you end up with only one category of shorts in your data, if won't be possible to include it as a random effect, and you'll have to change your model accordingly:

lmer(sales ~ 1 + centred_colour + centred_leg_length + 
             (1 + centred_colour | category),
     data=sales_data)
$\endgroup$
4
  • $\begingroup$ This sounds like a better version of my method (e.g. using the mean), so it's promising that we're thinking of similar things. Are you concerned at all about what this might do to the SEs and other statistics reported for the coefficients since you're adding a bunch of data points? I don't have good intuition about this. $\endgroup$
    – CHP
    Commented Aug 24, 2020 at 18:36
  • $\begingroup$ I think the standard errors should be unaffected, since data points where the predictor is set to 0 drop out of the computation. It could inflate the calculated degrees of freedom, but a) you're unlikely to need these for anything, b) if you have lots of data, this doesn't really matter, and c) if necessary, you can work out the correct DFs by subtracting the number of zeros. $\endgroup$
    – Eoin
    Commented Aug 25, 2020 at 9:32
  • 1
    $\begingroup$ Alright, i won't have time to fully test this before the bounty expires but it sounds like an equivalent or better method than mine, which I already tested somewhat and returns reasonable outputs. So I'll just assume this will perform even better and accept it so it will get the bounty if a better (e.g. w/ some mathematical proofs of why it works) answer doesn't come along by the time the bounty expires :) $\endgroup$
    – CHP
    Commented Aug 26, 2020 at 20:57
  • $\begingroup$ Thanks. It's also worth noting that imputing the mean value can lead to all sorts of complicated side-effects if you end up including interaction terms, all of which are avoided by centering your variables and imputing 0 instead. $\endgroup$
    – Eoin
    Commented Aug 28, 2020 at 11:47
1
$\begingroup$

Try to create a dummy variable that is 0 when a product does not have the feature (i.e. does not have leg), and 1 when it has this feature. Turn then this dummy variable into a factor.

When a dummy does not have a feature, replace the missing value with zero.

Then in your model, interact your continuous variable of interest with the dummy factor variable. Add also the dummy factor variables.


Note: I am happy to read from a more knowable person why this works (or does not work in the general case).

$\endgroup$
2
  • $\begingroup$ I think the regression term for the interaction here won't be identifiable, since you could double the main effect of the continuous variable and halve the interaction term, and end up with the same model. $\endgroup$
    – Eoin
    Commented Aug 24, 2020 at 18:20
  • $\begingroup$ Let F1 et F2 be the factors of the dummy variables, and C the continuous variable. I suggest to havee the following variable: F1 ; F2 ; F1:C ; F2:C. That is: four coefficients. I do not see how one could "double the main effect of the continuous variable and halve the interaction term". Sorrry if I was not clear previously. $\endgroup$ Commented Aug 24, 2020 at 20:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.