stata 计量数据标准化(min-max, z-scores)_24Mar2020

重要性:数据标准化是消除量纲影响的重要手段,构造新指标或应用某些算法前需要使用到。

写作理由:在 R 或者 Python 中,即使不使用第三方包,实现标准化也是非常容易的,但在 stata 中就显得不太顺手(仅我个人手感而言,并不代表我否认stata是优秀的软件),
本人也没多去寻找现存的包,顺手写了一个小工具,分享给需要的人。

可实现:z-score标准化,minmax标准化

* by Rujiang
* 19Dec2019
capture program drop stand
program stand
    version 14.1
    syntax varlist [if] [in] [, prefix(name) methods(name)]
    if "`methods'" == "" {
    
        if "`prefix'" == "" {
            local prefix "norm_"
        }
        
        foreach var in `varlist' {
            qui: summarize `var' `if' `in'
            qui: generate `prefix'`var' = (`var'- r(mean)) / r(sd) `if' `in'
            qui: label variable `prefix'`var' "normalized `var'"
            di "====> `prefix'`var' generated"
        }
            
        di "-----------xxxxxx-----------"
        di "all the variables you specified are **normalized**"
    }
    
    
    if "`methods'" == "minmax" {
    
            if "`prefix'" == "" {
                local prefix "minmax_"
            }
        
        foreach var in `varlist' {
            qui: summarize `var' `if' `in'
            qui: generate `prefix'`var' = (`var'- r(min)) / (r(max) - r(min)) `if' `in'
            qui: label variable `prefix'`var' "minmax standardized `var'"
            di "====> `prefix'`var' generated"
        }
            
        di "-----------xxxxxx-----------"
        di "all the variables you specified are **minmax standardized**"
    }

    
    if "`methods'" != "" {
        if "`methods'" != "minmax" {
            di as err `"nothing happened, only methods("minmax") allowed"'
        }
    }
    
end


使用举例


. clear

. sysuse auto
(1978 Automobile Data)

. des

Contains data from D:\来自华硕_RJL_25Apr2018\stata\StataCorp Stata 14.1 (Revision
>  20.06.2016)\ado\base/a/auto.dta
  obs:            74                          1978 Automobile Data
 vars:            12                          13 Apr 2014 17:45
 size:         3,182                          (_dta has notes)
---------------------------------------------------------------------------------
              storage   display    value
variable name   type    format     label      variable label
---------------------------------------------------------------------------------
make            str18   %-18s                 Make and Model
price           int     %8.0gc                Price
mpg             int     %8.0g                 Mileage (mpg)
rep78           int     %8.0g                 Repair Record 1978
headroom        float   %6.1f                 Headroom (in.)
trunk           int     %8.0g                 Trunk space (cu. ft.)
weight          int     %8.0gc                Weight (lbs.)
length          int     %8.0g                 Length (in.)
turn            int     %8.0g                 Turn Circle (ft.)
displacement    int     %8.0g                 Displacement (cu. in.)
gear_ratio      float   %6.2f                 Gear Ratio
foreign         byte    %8.0g      origin     Car type
---------------------------------------------------------------------------------
Sorted by: foreign


. stand price
====> norm_price generated
-----------xxxxxx-----------
all the variables you specified are **normalized**

. stand price, methods("minmax")
====> minmax_price generated
-----------xxxxxx-----------
all the variables you specified are **minmax standardized**


. stand price-head, methods("minmax") prefix("stand_")
====> stand_price generated
====> stand_mpg generated
====> stand_rep78 generated
====> stand_headroom generated
-----------xxxxxx-----------
all the variables you specified are **minmax standardized**

. su stand*

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
 stand_price |         74    .2278444    .2338086          0          1
   stand_mpg |         74    .3205965    .1995001          0          1
 stand_rep78 |         69    .6014493    .2474831          0          1
stand_head~m |         74    .4266409    .2417128          0          1


你可能感兴趣的:(stata 计量数据标准化(min-max, z-scores)_24Mar2020)