Announcement

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

  • factor 'xxx' not found in list of covariates

    After I executed regress syntax, I want to run the margins line. But I got this error - factor 'xxx' not found in list of covariates.

    Tom

  • #2
    We can't tell you precisely what you did wrong if you can't tell us precisely what you did.

    Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Section 12.1 is particularly pertinent

    12.1 What to say about your commands and your problem

    Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
    as is Section 12.3 on the use of code delimiters to present code and output from the results window.

    Show us your regression command and its output, and the margins command, and its output. To assure maximum readability of results that you post, please copy them from the Results window or your log file into a code block in the Forum editor, as explained in section 12 of the Statalist FAQ linked to at the top of the page. For example, the following:

    [code]
    . sysuse auto, clear
    (1978 Automobile Data)

    . describe make price

    storage display value
    variable name type format label variable label
    -----------------------------------------------------------------
    make str18 %-18s Make and Model
    price int %8.0gc Price
    [/code]

    will be presented in the post as the following:
    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . describe make price
    
                  storage   display    value
    variable name   type    format     label      variable label
    -----------------------------------------------------------------
    make            str18   %-18s                 Make and Model
    price           int     %8.0gc                Price

    Comment


    • #3
      My guess is you have something like

      Code:
      reg y x
      margins x
      That won’t work. X needs to be a categorical variable. If it is categorical, not continuous, you can do

      Code:
      reg y i.x
      margins x
      As William says, though, it would be better if you showed code and output so we didn’t need to guess.
      -------------------------------------------
      Richard Williams, Notre Dame Dept of Sociology
      StataNow Version: 19.5 MP (2 processor)

      EMAIL: [email protected]
      WWW: https://www3.nd.edu/~rwilliam

      Comment


      • #4
        You just need to type;

        Code:
        .help margins
        Best regards,

        Marcos

        Comment


        • #5
          Code:
          . use 20171114.dta
          
          . regress averagedailydoseduringtheinitial bsa gendercode agey af hypertension nonaaornonanona n
          > on11ornon1non1 chf ttestindication ttesttargetinr
          
                Source |       SS           df       MS      Number of obs   =       175
          -------------+----------------------------------   F(10, 164)      =     10.05
                 Model |  43.1617679        10  4.31617679   Prob > F        =    0.0000
              Residual |  70.4175304       164  .429375185   R-squared       =    0.3800
          -------------+----------------------------------   Adj R-squared   =    0.3422
                 Total |  113.579298       174  .652754588   Root MSE        =    .65527
          
          ---------------------------------------------------------------------------------
          averagedailyd~l |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          ----------------+----------------------------------------------------------------
                      bsa |   1.188892   .3362718     3.54   0.001     .5249113    1.852872
               gendercode |   .1272196   .1238465     1.03   0.306    -.1173195    .3717588
                     agey |  -.0155522   .0047775    -3.26   0.001    -.0249855    -.006119
                       af |    .127866   .1221302     1.05   0.297    -.1132843    .3690162
             hypertension |   .3461978   .1281871     2.70   0.008      .093088    .5993076
          nonaaornonanona |   .8642623   .1477174     5.85   0.000     .5725892    1.155935
          non11ornon1non1 |  -.4617247   .1669169    -2.77   0.006    -.7913078   -.1321415
                      chf |  -.1948752   .1412051    -1.38   0.169    -.4736895    .0839391
          ttestindication |   -.269428    .170816    -1.58   0.117      -.60671     .067854
           ttesttargetinr |  -.0804833   .1887749    -0.43   0.670    -.4532259    .2922593
                    _cons |   1.517105   .6921503     2.19   0.030     .1504305     2.88378
          ---------------------------------------------------------------------------------
          
          . margins gendercode
          factor 'gendercode' not found in list of covariates
          r(322);

          Comment


          • #6
            You need

            i.gendercode
            -------------------------------------------
            Richard Williams, Notre Dame Dept of Sociology
            StataNow Version: 19.5 MP (2 processor)

            EMAIL: [email protected]
            WWW: https://www3.nd.edu/~rwilliam

            Comment


            • #7
              Code:
              . margins i.gendercode
              factor 'gendercode' not found in list of covariates
              r(322);

              Comment


              • #8
                No you need to have i.gendercode in the regress command.
                -------------------------------------------
                Richard Williams, Notre Dame Dept of Sociology
                StataNow Version: 19.5 MP (2 processor)

                EMAIL: [email protected]
                WWW: https://www3.nd.edu/~rwilliam

                Comment


                • #9
                  Elaborating on Richard's advice, you should be using factor variable notation for any categorical variables in your model, even those that are coded 0/1. Review the discussion provided in the output of help factor variables and understand that it is the use of factor variable notation that conveys to Stata that a given variable is a factor - that is, categorical - variable. If you have other categorical variables in your model (I'm thinking hypertension may be categorical) they too should be coded using factor variable notation.

                  Comment


                  • #10
                    Originally posted by Richard Williams View Post
                    No you need to have i.gendercode in the regress command.
                    It works! the prefix of "I" is only for qualitative independent variable(s)? and the output via margins is the 95% PI (prediction interval) of the dependent variable (in my case the averagedailydoseduringtheinitial)?

                    Much appreciated!

                    Tom

                    Comment


                    • #11
                      Code:
                      . regress averagedailydoseduringtheinitial bsa i.gendercode agey i.af i.hypertension i.nonaaorno
                      > nanona i.non11ornon1non1 i.chf i.ttestindication i.ttesttargetinr
                      
                            Source |       SS           df       MS      Number of obs   =       175
                      -------------+----------------------------------   F(10, 164)      =     10.05
                             Model |  43.1617679        10  4.31617679   Prob > F        =    0.0000
                          Residual |  70.4175304       164  .429375185   R-squared       =    0.3800
                      -------------+----------------------------------   Adj R-squared   =    0.3422
                             Total |  113.579298       174  .652754588   Root MSE        =    .65527
                      
                      -----------------------------------------------------------------------------------
                      averagedailydos~l |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
                      ------------------+----------------------------------------------------------------
                                    bsa |   1.188892   .3362718     3.54   0.001     .5249113    1.852872
                           1.gendercode |   .1272196   .1238465     1.03   0.306    -.1173195    .3717588
                                   agey |  -.0155522   .0047775    -3.26   0.001    -.0249855    -.006119
                                   1.af |    .127866   .1221302     1.05   0.297    -.1132843    .3690162
                         1.hypertension |   .3461978   .1281871     2.70   0.008      .093088    .5993076
                      1.nonaaornonanona |   .8642623   .1477174     5.85   0.000     .5725892    1.155935
                      1.non11ornon1non1 |  -.4617247   .1669169    -2.77   0.006    -.7913078   -.1321415
                                  1.chf |  -.1948752   .1412051    -1.38   0.169    -.4736895    .0839391
                      1.ttestindication |   -.269428    .170816    -1.58   0.117      -.60671     .067854
                       1.ttesttargetinr |  -.0804833   .1887749    -0.43   0.670    -.4532259    .2922593
                                  _cons |   1.517105   .6921503     2.19   0.030     .1504305     2.88378
                      -----------------------------------------------------------------------------------
                      
                      . margins i.gendercode i.af i.hypertension i.nonaaornonanona i.non11ornon1non1 i.chf i.ttestindi
                      > cation i.ttesttargetinr
                      
                      Predictive margins                              Number of obs     =        175
                      Model VCE    : OLS
                      
                      Expression   : Linear prediction, predict()
                      
                      ---------------------------------------------------------------------------------
                                      |            Delta-method
                                      |     Margin   Std. Err.      t    P>|t|     [95% Conf. Interval]
                      ----------------+----------------------------------------------------------------
                           gendercode |
                                   0  |   2.608814   .0852265    30.61   0.000     2.440531    2.777097
                                   1  |   2.736034    .073641    37.15   0.000     2.590627    2.881441
                                      |
                                   af |
                                   0  |   2.639871   .0626649    42.13   0.000     2.516137    2.763605
                                   1  |   2.767737   .0972987    28.45   0.000     2.575617    2.959856
                                      |
                         hypertension |
                                   0  |   2.593013    .059096    43.88   0.000     2.476326      2.7097
                                   1  |   2.939211   .1079877    27.22   0.000     2.725985    3.152436
                                      |
                      nonaaornonanona |
                                   0  |    2.56153   .0535161    47.86   0.000      2.45586    2.667199
                                   1  |   3.425792   .1367456    25.05   0.000     3.155783    3.695801
                                      |
                      non11ornon1non1 |
                                   0  |   2.727549   .0524245    52.03   0.000     2.624035    2.831063
                                   1  |   2.265824    .157728    14.37   0.000     1.954385    2.577264
                                      |
                                  chf |
                                   0  |   2.712351   .0547825    49.51   0.000     2.604181    2.820521
                                   1  |   2.517476   .1277955    19.70   0.000     2.265139    2.769812
                                      |
                      ttestindication |
                                   0  |   2.860189   .1244822    22.98   0.000     2.614395    3.105983
                                   1  |   2.590761   .0752239    34.44   0.000     2.442229    2.739293
                                      |
                       ttesttargetinr |
                                   0  |   2.695694   .0616338    43.74   0.000     2.573996    2.817392
                                   1  |   2.615211   .1599611    16.35   0.000     2.299362    2.931059
                      ---------------------------------------------------------------------------------
                      When comparing the margins of the qualitative "gendercode", are other independent variables hold constant? And the margins is for prediction interval rather than the confidence interval for conditional means?

                      I guess when comparing the margins of the qualitative "gendercode", other independent variables are hold constant. The predicted dependent variable for gendercode of 0 and 1 are 2.608814 (gendercode = 0)and 2.736034 (gendercode = 1), respectively. And 2.736034 minus 2.608814 equals 0.12722, which is the rounded value of the Coef. of gendercode (0.1272196). So only when other independent variables (except gendercode) are hold constant, we can get this result (0.12722).

                      Tom
                      Last edited by Tom Hsiung; 20 Nov 2017, 06:37.

                      Comment


                      • #12
                        Please read the Stata Manual on the subject. The explanation plus the examples will be fully rewarding to you.
                        Best regards,

                        Marcos

                        Comment


                        • #13
                          Originally posted by Marcos Almeida View Post
                          Please read the Stata Manual on the subject. The explanation plus the examples will be fully rewarding to you.
                          I don't know where to start. I don't have the Stata manual. And I think this issue is related to the logic behind the regression procedure.

                          Comment


                          • #14
                            If you installed Stata correctly, Stata comes with online help backed up by extensive PDF documentation.

                            Comment


                            • #15
                              I don't have the Stata manual. And I think this issue is related to the logic behind the regression procedure.
                              Both sentences are hardly believable.


                              For the first, you may get the Stata Manuals:

                              A) Directly from the command window.

                              B) In the Web, starting by taking a look at Stata's main page.


                              For the second, margins and marginsplot represent a great resource to explore the results from the regression analysis. That said:

                              A) The issue on the factor notation is clearly displayed in the Manual as well as when we type "help margins".

                              B) If by " the logic behind the regression procedure" you mean the core-knowledge concerning linear regression analysis, it is not that difficult to grasp the main aspects. Any decent textbook will present it, and this will surely be the best starting point.
                              Best regards,

                              Marcos

                              Comment

                              Working...
                              X