Announcement

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

  • Normalisation to 1

    Hi everyone!

    I am trying to normalise the Idexmsa to 1. I have tried the code:

    g Indexmsanor = (Indexmsa - min(Indexmsa))/(max(Indexmsa)- min(Indexmsa))

    This gives me the result invalid syntax. I however do not see where it would be a syntax error. I have provided a small snapshot of the database below. The actual database exist of more than 40K observations.

    Thanks in advance!
    Rick

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long gvkey double year float Indexmsa
    1004 1992         .
    1004 1993 2.3308494
    1004 1994  2.406004
    1004 1995  2.506281
    1004 1996  2.574835
    1004 1997  2.651267
    1004 1998  2.713008
    1004 1999  2.817756
    1004 2000  3.041942
    1004 2001  3.227166
    1004 2002 3.4175005
    1004 2003 3.5546095
    1004 2004  3.852246
    1004 2005 4.2420692
    1004 2006  4.510326
    1004 2007 4.5599318
    1004 2008 4.3738556
    1004 2009 3.9382584
    1004 2010  3.618267
    1004 2011 3.3870556
    1004 2012  3.260166
    1004 2013 3.2980626
    1004 2014  3.475623
    1004 2015  3.596338
    1004 2016 3.7176924
    1004 2017 3.8422396
    1004 2018  3.956994
    1004 2019  4.067277
    1004 2020 4.1092186
    1010 1992         .
    1010 1993         .
    1010 1994         .
    1010 1995         .
    1010 1996         .
    1010 1997         .
    1010 1998         .
    1010 1999         .
    1010 2000         .
    1010 2001         .
    1010 2002         .
    1010 2003         .
    1013 1992         .
    1013 1993         .
    1013 1994         .
    1013 1995         .
    1013 1996         .
    1013 1997         .
    1013 1998         .
    1013 1999         .
    1013 2000         .
    1013 2001         .
    1013 2002         .
    1013 2003         .
    1013 2004         .
    1013 2005         .
    1013 2006         .
    1013 2007         .
    1013 2008         .
    1013 2009         .
    1013 2010         .
    1019 1992         .
    1019 1993         .
    1019 1994         .
    1019 1995         .
    1019 1996         .
    1019 1997         .
    1019 1998         .
    1019 1999         .
    1019 2000         .
    1019 2001         .
    1019 2002         .
    1019 2003         .
    1019 2004         .
    1019 2005         .
    1019 2006         .
    1019 2007         .
    1019 2008         .
    1019 2009         .
    1019 2010         .
    1019 2011         .
    1019 2012         .
    1025 1992         .
    1025 1993         1
    1025 1994 1.0023696
    1025 1995 1.0055636
    1034 1992         .
    1034 1993 1.9326962
    1034 1994  1.937276
    1034 1995 1.9434488
    1034 1996 1.9802867
    1034 1997 2.0266826
    1034 1998 2.1264436
    1034 1999   2.26822
    1034 2000  2.577857
    1034 2001  2.844285
    1034 2002   3.16368
    1034 2003  3.446037
    1034 2004  3.925329
    1034 2005  4.555556
    1034 2006  4.908602
    end

  • #2
    You are guessing that max() and min() in Stata return the maximum and minimum over a variable but they don't do that. They return the row-wise or observation-wise maximum or minimum over two or more arguments. In fact, offering a single argument is a syntax error, as you found.

    Code:
    . di max(2, 1)
    2
    
    . di max(2)
    invalid syntax
    r(198);
    This should be (a little) better documented,

    Here is some technique:

    Code:
    . sysuse auto, clear
    (1978 automobile data)
    
    . su price, meanonly
    
    . gen wanted = (price - r(min)) / (r(max) - r(min))
    
    . su price wanted
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
           price |         74    6165.257    2949.496       3291      15906
          wanted |         74    .2278444    .2338086          0          1

    Mata comes closer to your view of what functions should do:

    Code:
    . mata :
    ------------------------------------------------- mata (type end to exit) -------------------------------------
    : price = st_data(., "price")
    
    : st_addvar("float", "WANTED")
      
    : st_store(., "WANTED", (price :- min(price)) / (max(price) - min(price)))
    
    : end
    ---------------------------------------------------------------------------------------------------------------
    
    . assert wanted == WANTED

    Comment


    • #3
      Thanks a lot the last code worked perfectly!

      Comment

      Working...
      X