Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Forcibly Including Main Effects in Elastic Net Model but Include Optional Interactions

    Dear Statalist,

    By way of background to this question, I am attempting to build a model predicting individuals' labor income throughout their working careers using the Current Population Survey. Among other estimation methods, I would like to use the Elastic Net and force the inclusion of indicator variables for age in my sample and optionally include interactions of age with other demographic characteristics (e.g. education, marital status, etc.).

    However, when I attempt to do this, I run into two errors. The first is a "base conflict error" relating to my age variable (i.e. age: factor variable base category conflict). In my attempt to fix this error message, I generate an identical copy of my age variable via clonevar and include that variable as the optional interaction term. Upon doing this, I get a rather inscrutable conformability error.

    The following code is a minimum working example (code plus resulting error messages) using the automobile dataset replicating the issue:


    Code:
    sysuse auto, clear
    global other_indepvars "c.mpg c.headroom c.trunk c.weight c.length c.turn c.displacement c.gear_ratio i.foreign"
    cap noisily elasticnet linear price (i.rep78) $other_indepvars i.rep78#($other_indepvars), rseed(8973) nolog
    rep78: factor variable base category conflict
    
    clonevar rep78_for_lasso = rep78
    cap noisily elasticnet linear price (i.rep78) $other_indepvars i.rep78_for_lasso#($other_indepvars), rseed(8973) nolog
    1o.rep78_for_lasso#1o.foreign omitted
    2o.rep78_for_lasso#1o.foreign omitted
    1o.rep78_for_lasso#1o.foreign omitted
    2o.rep78_for_lasso#1o.foreign omitted
    1o.rep78_for_lasso#1o.foreign omitted
    2o.rep78_for_lasso#1o.foreign omitted
    _LASSO_RESULT_APPEND_OBJ::compact_B():  3200  conformability error
    _LASSO_RESULT_APPEND_OBJ::compact():     -  function returned error
    _LASSO_RESULT_APPEND_OBJ::merge():     -  function returned error
              _LASYS_merge():     -  function returned error
                     <istmt>:     -  function returned error
    This brings me to two questions.
    1. Does anyone know of a way to force inclusion of a series of indicators in an elastic model and optionally allow for interactions between those indicators and other variables?
    2. If cloning the indicator variable and including it in the optional variables is the way to go, any suggestions on fixing the conformability issue?
    It is worth noting that this issue seems to be driven by treating rep78 (or age, in my actual application) as a categorical variable. Treating rep78 as continuous fixes the issue in the automobile dataset.
    Last edited by Michael Gropper; 21 Jul 2025, 15:40.

  • #2
    Code:
    sysuse auto, clear
    local other_indepvars "c.mpg c.headroom c.trunk c.weight c.length c.turn c.displacement c.gear_ratio i.foreign"
    cap noisily elasticnet linear price (ibn.rep78) `other_indepvars' ibn.rep78#(`other_indepvars'), rseed(8973) nolog
    di e(allvars_sel)
    Below, "5.rep78" defaults to the base level.

    Res.:

    Code:
    . di e(allvars_sel)
    1bn.rep78 2.rep78 3.rep78 4.rep78 5o.rep78 mpg headroom trunk weight length turn displacement gear_ratio 0bn.foreign 1bn.foreign 2bn.rep78#
    > c.mpg 3bn.rep78#c.mpg 4bn.rep78#c.mpg 5bn.rep78#c.mpg 1bn.rep78#c.headroom 2bn.rep78#c.headroom 4bn.rep78#c.headroom 5bn.rep78#c.headroom
    >  1bn.rep78#c.trunk 2bn.rep78#c.trunk 3bn.rep78#c.trunk 4bn.rep78#c.trunk 5bn.rep78#c.trunk 1bn.rep78#c.weight 2bn.rep78#c.weight 3bn.rep7
    > 8#c.weight 4bn.rep78#c.weight 5bn.rep78#c.weight 1bn.rep78#c.length 2bn.rep78#c.length 3bn.rep78#c.length 4bn.rep78#c.length 5bn.rep78#c.
    > length 1bn.rep78#c.turn 2bn.rep78#c.turn 3bn.rep78#c.turn 4bn.rep78#c.turn 5bn.rep78#c.turn 1bn.rep78#c.displacement 2bn.rep78#c.displace
    > ment 3bn.rep78#c.displacement 4bn.rep78#c.displacement 5bn.rep78#c.displacement 1bn.rep78#c.gear_ratio 2bn.rep78#c.gear_ratio 3bn.rep78#c
    > .gear_ratio 4bn.rep78#c.gear_ratio 5bn.rep78#c.gear_ratio 3bn.rep78#0bn.foreign 3bn.rep78#1bn.foreign 4bn.rep78#0bn.foreign 4bn.rep78#1bn
    > .foreign 5bn.rep78#0bn.foreign 5bn.rep78#1bn.foreign

    Comment


    • #3
      Hi Andrew,

      Somewhat inexplicably... your suggested fix sometimes works for me. But it sometimes does not. See below for accompanying code and below that, the log file.

      Code:
      clear all
      log using "...", text replace
      
      local run = 1
      local max_runs = 3
      
      while `run' <= `max_runs' {
          sysuse auto, clear
          local other_indepvars "c.mpg c.headroom c.trunk c.weight c.length c.turn c.displacement c.gear_ratio i.foreign"
          cap noisily elasticnet linear price (ibn.rep78) `other_indepvars' ibn.rep78#(`other_indepvars'), rseed(8973) nolog
          di e(allvars_sel)
      
          local run = `run' + 1
      }
      
      * End of log file
      log close
      Code:
      -----------------------------------------------------------------------------------------------------------------------
            name:  <unnamed>
             log:  ...
        log type:  text
       opened on:  21 Jul 2025, 19:05:41
      
      . 
      . local run = 1
      
      . local max_runs = 3
      
      . 
      . while `run' <= `max_runs' {
        2.         sysuse auto, clear
        3.         local other_indepvars "c.mpg c.headroom c.trunk c.weight c.length c.turn c.displacement c.gear_ratio i.for
      > eign"
        4.         cap noisily elasticnet linear price (ibn.rep78) `other_indepvars' ibn.rep78#(`other_indepvars'), rseed(897
      > 3) nolog
        5.         di e(allvars_sel)
        6. 
      .         local run = `run' + 1
        7. }
      (1978 automobile data)
      note: 5.rep78 omitted because of collinearity.
      note: 5.rep78 omitted because of collinearity.
      note: 5.rep78 omitted because of collinearity.
      _LASSO_RESULT_APPEND_OBJ::compact_B():  3200  conformability error
      _LASSO_RESULT_APPEND_OBJ::compact():     -  function returned error
      _LASSO_RESULT_APPEND_OBJ::merge():     -  function returned error
                _LASYS_merge():     -  function returned error
                       <istmt>:     -  function returned error
      1bn.rep78 2.rep78 3.rep78 4.rep78 5o.rep78 mpg headroom trunk weight length turn displacement gear_ratio 0bn.foreign 1b
      > n.foreign 2bn.rep78#c.mpg 3bn.rep78#c.mpg 4bn.rep78#c.mpg 5bn.rep78#c.mpg 1bn.rep78#c.headroom 2bn.rep78#c.headroom 3
      > bn.rep78#c.headroom 4bn.rep78#c.headroom 5bn.rep78#c.headroom 1bn.rep78#c.trunk 2bn.rep78#c.trunk 3bn.rep78#c.trunk 4
      > bn.rep78#c.trunk 5bn.rep78#c.trunk 1bn.rep78#c.weight 2bn.rep78#c.weight 3bn.rep78#c.weight 4bn.rep78#c.weight 5bn.re
      > p78#c.weight 1bn.rep78#c.length 2bn.rep78#c.length 3bn.rep78#c.length 4bn.rep78#c.length 5bn.rep78#c.length 1bn.rep78
      > #c.turn 2bn.rep78#c.turn 3bn.rep78#c.turn 4bn.rep78#c.turn 5bn.rep78#c.turn 1bn.rep78#c.displacement 2bn.rep78#c.disp
      > lacement 3bn.rep78#c.displacement 4bn.rep78#c.displacement 5bn.rep78#c.displacement 1bn.rep78#c.gear_ratio 2bn.rep78#
      > c.gear_ratio 3bn.rep78#c.gear_ratio 4bn.rep78#c.gear_ratio 5bn.rep78#c.gear_ratio 3bn.rep78#0bn.foreign 3bn.rep78#1bn
      > .foreign 4bn.rep78#0bn.foreign 4bn.rep78#1bn.foreign 5bn.rep78#0bn.foreign 5bn.rep78#1bn.foreign
      (1978 automobile data)
      note: 5.rep78 omitted because of collinearity.
      note: 5.rep78 omitted because of collinearity.
      note: 5.rep78 omitted because of collinearity.
      
      Elastic net linear model                         No. of obs        =         69
                                                       No. of covariates =         61
      Selection: Cross-validation                      No. of CV folds   =         10
      
      -------------------------------------------------------------------------------
                     |                               No. of      Out-of-      CV mean
                     |                              nonzero       sample   prediction
      alpha       ID |     Description      lambda    coef.    R-squared        error
      ---------------+---------------------------------------------------------------
      1.000          |
                   1 |    first lambda     2785.52        4      -0.1062      9247143
                  46 |     last lambda     48.9729       18       0.3033      5823684
      ---------------+---------------------------------------------------------------
      0.750          |
                  47 |    first lambda     2785.52        4      -0.1062      9247143
                 134 |   lambda before    .9839796       58       0.3629      5325658
               * 135 | selected lambda    .8965656       59       0.3629      5325641
                 136 |    lambda after    .8169172       58       0.3627      5327660
                 139 |     last lambda    .6179679       59       0.3602      5348664
      ---------------+---------------------------------------------------------------
      0.500          |
                 140 |    first lambda     2785.52        4      -0.1062      9247094
                 240 |     last lambda    .2935848       59       0.3593      5356194
      -------------------------------------------------------------------------------
      * alpha and lambda selected by cross-validation.
      1bn.rep78 2.rep78 3.rep78 4.rep78 5o.rep78 mpg headroom trunk weight length turn displacement gear_ratio 0bn.foreign 1b
      > n.foreign 2bn.rep78#c.mpg 3bn.rep78#c.mpg 4bn.rep78#c.mpg 5bn.rep78#c.mpg 1bn.rep78#c.headroom 2bn.rep78#c.headroom 3
      > bn.rep78#c.headroom 4bn.rep78#c.headroom 5bn.rep78#c.headroom 1bn.rep78#c.trunk 2bn.rep78#c.trunk 3bn.rep78#c.trunk 4
      > bn.rep78#c.trunk 5bn.rep78#c.trunk 1bn.rep78#c.weight 2bn.rep78#c.weight 3bn.rep78#c.weight 4bn.rep78#c.weight 5bn.re
      > p78#c.weight 1bn.rep78#c.length 2bn.rep78#c.length 3bn.rep78#c.length 4bn.rep78#c.length 5bn.rep78#c.length 1bn.rep78
      > #c.turn 2bn.rep78#c.turn 3bn.rep78#c.turn 4bn.rep78#c.turn 5bn.rep78#c.turn 1bn.rep78#c.displacement 2bn.rep78#c.disp
      > lacement 3bn.rep78#c.displacement 4bn.rep78#c.displacement 5bn.rep78#c.displacement 1bn.rep78#c.gear_ratio 2bn.rep78#
      > c.gear_ratio 3bn.rep78#c.gear_ratio 4bn.rep78#c.gear_ratio 5bn.rep78#c.gear_ratio 3bn.rep78#0bn.foreign 3bn.rep78#1bn
      > .foreign 4bn.rep78#0bn.foreign 4bn.rep78#1bn.foreign 5bn.rep78#0bn.foreign 5bn.rep78#1bn.foreign
      (1978 automobile data)
      note: 5.rep78 omitted because of collinearity.
      note: 5.rep78 omitted because of collinearity.
      note: 5.rep78 omitted because of collinearity.
      _LASSO_RESULT_APPEND_OBJ::compact_B():  3200  conformability error
      _LASSO_RESULT_APPEND_OBJ::compact():     -  function returned error
      _LASSO_RESULT_APPEND_OBJ::merge():     -  function returned error
                _LASYS_merge():     -  function returned error
                       <istmt>:     -  function returned error
      1bn.rep78 2.rep78 3.rep78 4.rep78 5o.rep78 mpg headroom trunk weight length turn displacement gear_ratio 0bn.foreign 1b
      > n.foreign 2bn.rep78#c.mpg 3bn.rep78#c.mpg 4bn.rep78#c.mpg 5bn.rep78#c.mpg 1bn.rep78#c.headroom 2bn.rep78#c.headroom 3
      > bn.rep78#c.headroom 4bn.rep78#c.headroom 5bn.rep78#c.headroom 1bn.rep78#c.trunk 2bn.rep78#c.trunk 3bn.rep78#c.trunk 4
      > bn.rep78#c.trunk 5bn.rep78#c.trunk 1bn.rep78#c.weight 2bn.rep78#c.weight 3bn.rep78#c.weight 4bn.rep78#c.weight 5bn.re
      > p78#c.weight 1bn.rep78#c.length 2bn.rep78#c.length 3bn.rep78#c.length 4bn.rep78#c.length 5bn.rep78#c.length 1bn.rep78
      > #c.turn 2bn.rep78#c.turn 3bn.rep78#c.turn 4bn.rep78#c.turn 5bn.rep78#c.turn 1bn.rep78#c.displacement 2bn.rep78#c.disp
      > lacement 3bn.rep78#c.displacement 4bn.rep78#c.displacement 5bn.rep78#c.displacement 1bn.rep78#c.gear_ratio 2bn.rep78#
      > c.gear_ratio 3bn.rep78#c.gear_ratio 4bn.rep78#c.gear_ratio 5bn.rep78#c.gear_ratio 3bn.rep78#0bn.foreign 3bn.rep78#1bn
      > .foreign 4bn.rep78#0bn.foreign 4bn.rep78#1bn.foreign 5bn.rep78#0bn.foreign 5bn.rep78#1bn.foreign
      
      . 
      . * End of log file
      . log close
            name:  <unnamed>
             log:  ...
        log type:  text
       closed on:  21 Jul 2025, 19:05:55
      -----------------------------------------------------------------------------------------------------------------------

      Comment


      • #4
        I think you need to contact Technical Services about this and provide them with this reproducible example. I can't explain the inconsistency across iterations of the same code.

        Comment

        Working...
        X