Announcement

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

  • margins - factor variables may not contain noninteger values

    Dear all,

    i want to create a marginsplot of the variables returnoninvestment and pat_yr but I got two types of errors. Trying this code:

    Code:
    margins returnoninvestment#pat_yr
    I got the error "factor variables may not contain noninteger values'' (gives the error for both variables) so I tried to solve it using this code:

    Code:
    margins c.returnoninvestment#c.pat_yr
    I got the error "only factor variables and their interactions are allowed"

    Could someone help me how to fix these errors?

  • #2
    Dear Thomas,

    your independent variables seem to be continuous. Margins will only be able to deal with those if you specify at which levels of the independent variables you want the margins to be calculated.
    To obtain the margins from your interaction, you could do something like this:


    Loop through the two variables, because you might want to do this with more variables in the future:

    Code:
    foreach var of varlist returnoninvestment pat_yr {
    sum `var' //To store information about the minimum and maximum of the continuous variable.
    local max_`var' = round(r(max), 1) //creates a macro that rounds the maximum to the nearest integer
    local min_`var' = round(r(min), 1) //creates a macro that rounds the minimum to the nearest integer
    }

    Specify that you need the margins for both variables at each level from minimum to maximum. The (#) in brackets specifies the steps you take until calculating the next margins. (1) indicates that you increase the level by one:

    Code:
    margins, at (returnoninvestment= (`min_returnoninvestment' (1) `max_returnoninvestment') pat_yr= (`min_pat_yr' (1) `max_pat_yr') )

    You can also solve this by selecting a range of levels that you need, for example:

    Code:
    margins, at(returnoninvestment = (10 (1) 60) pat_yr = (1980 (1) 1998))
    or to select only specific values:

    Code:
    margins, at(returnoninvestment = (10 20 30 40 55 68) pat_yr = (1980 2011 2015) )

    Comment


    • #3
      Dear Florian,

      Many thanks for responding. I tried this code

      Code:
       
       foreach var of varlist returnoninvestment pat_yr { sum `var' //To store information about the minimum and maximum of the continuous variable. local max_`var' = round(r(max), 1) //creates a macro that rounds the maximum to the nearest integer local min_`var' = round(r(min), 1) //creates a macro that rounds the minimum to the nearest integer }
      and after that this code.
      Code:
       
       margins, at (returnoninvestment= (`min_returnoninvestment' (1) `max_returnoninvestment') pat_yr= (`min_pat_yr' (1) `max_pat_yr') )
      but I got the error: ''variable returnoninvestment not found in list of covariates"

      This is what happened in my data after using the first code (the variables _est_fe and _est_re were created:


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input double fyear float returnoninvestment double pat_yr byte(_est_re _est_fe)
      1997   48.01397              195.5 1 1
      1998   44.20482              172.5 1 1
      1999  38.035473              152.5 1 1
      2000  33.813366              130.5 1 1
      2001   26.41248                136 1 1
      2002  26.380386              134.5 1 1
      2003   25.20349              128.5 1 1
      2004  23.309574               99.5 1 1
      2005  25.467196               64.5 1 1
      2006  23.073746                178 1 1
      2007   20.25775              153.5 1 1
      2008   24.72846              170.5 1 1
      2009   20.20704              212.5 1 1
      2010   22.96796                480 1 1
      2011  24.603336                630 1 1
      2012  21.790276              692.5 1 1
      2013   9.331705                674 1 1
      2014   11.68603              573.5 1 1
      2015  12.398426              465.5 1 1
      2014  12.102242                  6 1 1
      2015  11.211775                  0 1 1
      1997  -3.367206              273.5 1 1
      1998  -4.845113                556 1 1
      1999  -8.298296                830 1 1
      2000  20.479486               1053 1 1
      2001   .8975403             1091.5 1 1
      2002 -20.079834 1153.8333740234375 1 1
      2003  -4.891128                908 1 1
      2004  4.1479573              812.5 1 1
      2005   5.039897              539.5 1 1
      2006  4.1140866                510 1 1
      2007 -14.615013              339.5 1 1
      2008  -20.21299                235 1 1
      2009  -8.584337              246.5 1 1
      2010  15.370196  230.8333282470703 1 1
      2011  15.880654                253 1 1
      2012  1.2038835                262 1 1
      2013   3.343824                287 1 1
      2014   6.705671  297.8333282470703 1 1
      2015  -19.69136                246 1 1
      1997  13.899348               72.5 1 1
      1998  16.468351                 82 1 1
      1999  14.267895                 87 1 1
      2000  15.012787                105 1 1
      2001  15.262887                 87 1 1
      2002   13.40005                 77 1 1
      2003   11.73117              105.5 1 1
      2004  12.459675                 77 1 1
      2005  14.033336               61.5 1 1
      2006   13.72897               71.5 1 1
      2007   15.47853               73.5 1 1
      2008   17.16711                 74 1 1
      2009  13.350144                 62 1 1
      2010   15.42095                 62 1 1
      2011   16.50078               58.5 1 1
      2012   12.84138                 87 1 1
      2013  11.941256                 92 1 1
      2014   12.69443                 81 1 1
      2015  16.507212                 63 1 1
      1997  3.0086274                  1 1 1
      1998   3.198363                4.5 1 1
      1999   5.880355                5.5 1 1
      2000  8.2584505                  5 1 1
      2001  10.337478                  2 1 1
      2002  11.113247                  1 1 1
      2003   14.16007                  0 1 1
      2004  16.055586                  2 1 1
      2005  19.710047                  1 1 1
      2006  17.422897                  1 1 1
      2007   21.99972                  0 1 1
      2008  22.781183                  1 1 1
      2009  22.267435                  0 1 1
      2010   26.56302                  1 1 1
      2011   27.55534                  2 1 1
      2012   24.93992                  7 1 1
      2013  25.466797                  0 1 1
      2014  27.289564                  0 1 1
      2015  29.500755                  0 1 1
      1997   29.40546                191 1 1
      1998   28.96796                241 1 1
      1999  29.585747                408 1 1
      2000   26.58998                496 1 1
      2001   19.76836                535 1 1
      2002  18.381706                558 1 1
      2003   13.98343                531 1 1
      2004  15.312317                641 1 1
      2005   19.86607                535 1 1
      2006  26.003376                650 1 1
      2007   29.14418                640 1 1
      2008  34.791603                799 1 1
      2009   25.39474              873.5 1 1
      2010  22.572845               1111 1 1
      2011  18.652647               1059 1 1
      2012   23.45232               1126 1 1
      2013   25.11101             1039.5 1 1
      2014   26.73292             1095.5 1 1
      2015   30.39321               1176 1 1
      1997   20.34571                  2 1 1
      1998   23.92722                  0 1 1
      1999   13.62236                  1 1 1
      end
      Do you know what went wrong?

      Comment


      • #4
        Dear Thomas,
        can you provide your code prior to margins? This error prompt will usually appear if your margins-command does not find a list of covariates from some model that was calculated before the command.

        Comment


        • #5
          Dear Florian,

          It's a panel data set and I merged a few other datasets with it. The variable was created by dividing to other variables (profit/invested capital). the code I used before using the margin command was
          Code:
          xtreg returnoninvestment pat_yr maactive maactive#c.pat_yr totalma employees assetstotal i.fyear i.industry, fe
          estimate store fe

          Comment


          • #6
            The specified model shines light on your error message:

            returnoninvestment is not a covariate in your model (hence the error message). returnoninvestment is the dependent variable/predicted variable. To obtain margins, you need to use the independent variables in the model, like par_yr, maactive or totalma.

            Comment


            • #7
              Ah thank you Florian, that is indeed the case. I still get an error but I don't know why. I tried this code:
              Code:
               xtreg c.returnoninvestment pat_yr maactive maactive#c.pat_yr totalma employees assetstotal i.fyear i.industry, fe 
              estimate store fe
              margins, at (pat_yr= (`min_pat_yr' (1) `max_pat_yr') maactive= (`min_maactive' (1) `max_maactive') )
              But I get the error r111 "maactive ambiguous abbreviation". the variable maactive is a dummy variable which gives firms a 0 in years before first M&A and a 1 I the years from M&A and afterwards. See below

              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input double fyear float returnoninvestment double pat_yr float maactive
              1997   48.01397              195.5 0
              1998   44.20482              172.5 0
              1999  38.035473              152.5 1
              2000  33.813366              130.5 1
              2001   26.41248                136 1
              2002  26.380386              134.5 1
              2003   25.20349              128.5 1
              2004  23.309574               99.5 1
              2005  25.467196               64.5 1
              2006  23.073746                178 1
              2007   20.25775              153.5 1
              2008   24.72846              170.5 1
              2009   20.20704              212.5 1
              2010   22.96796                480 1
              2011  24.603336                630 1
              2012  21.790276              692.5 1
              2013   9.331705                674 1
              2014   11.68603              573.5 1
              2015  12.398426              465.5 1
              2014  12.102242                  6 1
              2015  11.211775                  0 1
              1997  -3.367206              273.5 0
              1998  -4.845113                556 0
              1999  -8.298296                830 0
              2000  20.479486               1053 0
              2001   .8975403             1091.5 0
              2002 -20.079834 1153.8333740234375 1
              2003  -4.891128                908 1
              2004  4.1479573              812.5 1
              2005   5.039897              539.5 1
              2006  4.1140866                510 1
              2007 -14.615013              339.5 1
              2008  -20.21299                235 1
              2009  -8.584337              246.5 1
              2010  15.370196  230.8333282470703 1
              2011  15.880654                253 1
              2012  1.2038835                262 1
              2013   3.343824                287 1
              2014   6.705671  297.8333282470703 1
              2015  -19.69136                246 1
              1997  13.899348               72.5 1
              1998  16.468351                 82 1
              1999  14.267895                 87 1
              2000  15.012787                105 1
              2001  15.262887                 87 1
              2002   13.40005                 77 1
              2003   11.73117              105.5 1
              2004  12.459675                 77 1
              2005  14.033336               61.5 1
              2006   13.72897               71.5 1
              2007   15.47853               73.5 1
              2008   17.16711                 74 1
              2009  13.350144                 62 1
              2010   15.42095                 62 1
              2011   16.50078               58.5 1
              2012   12.84138                 87 1
              2013  11.941256                 92 1
              2014   12.69443                 81 1
              2015  16.507212                 63 1
              1997  3.0086274                  1 0
              1998   3.198363                4.5 0
              1999   5.880355                5.5 0
              2000  8.2584505                  5 0
              2001  10.337478                  2 0
              2002  11.113247                  1 0
              2003   14.16007                  0 0
              2004  16.055586                  2 0
              2005  19.710047                  1 0
              2006  17.422897                  1 0
              2007   21.99972                  0 0
              2008  22.781183                  1 0
              2009  22.267435                  0 0
              2010   26.56302                  1 0
              2011   27.55534                  2 0
              2012   24.93992                  7 0
              2013  25.466797                  0 0
              2014  27.289564                  0 0
              2015  29.500755                  0 0
              1997   29.40546                191 0
              1998   28.96796                241 0
              1999  29.585747                408 0
              2000   26.58998                496 1
              2001   19.76836                535 1
              2002  18.381706                558 1
              2003   13.98343                531 1
              2004  15.312317                641 1
              2005   19.86607                535 1
              2006  26.003376                650 1
              2007   29.14418                640 1
              2008  34.791603                799 1
              2009   25.39474              873.5 1
              2010  22.572845               1111 1
              2011  18.652647               1059 1
              2012   23.45232               1126 1
              2013   25.11101             1039.5 1
              2014   26.73292             1095.5 1
              2015   30.39321               1176 1
              1997   20.34571                  2 0
              1998   23.92722                  0 0
              1999   13.62236                  1 0
              end
              Many thanks in advance!

              Comment


              • #8
                To specify: what I finally want is a margin plot with on the y-axis average patenting output and the x-axis maactive. and then on the left side of the graph average patenting output when maactive =0 and on the right side when maactive=1.

                Comment

                Working...
                X