归一化和标准化:别再傻傻分不清

发布时间:2021-04-29 阅读 23289

Stata连享会   主页 || 视频 || 推文 || 知乎

温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装命令如下:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh

课程详情 https://gitee.com/lianxh/Course

课程主页 https://gitee.com/lianxh/Course

⛳ Stata 系列推文:

作者: 陈波 ( 深圳大学 )
邮箱: 1900123011@email.szu.edu.cn


目录


1. 引言

在阅读文献时,我们经常会看到作者出于特定目的,对某些变量进行归一化( Normalization )或者标准化处理( Standardization )。许多作者行文简洁,对具体处理过程往往一笔带过,却也让初入此径的小白不知所措。下面本文将对其作用与具体方法展开详细介绍 ( 为行文方便,以下统称标准化 ) 。

2. 标准化的作用

在介绍方法之前,我们绕不开的一个问题就是:为什么要使用该方法呢?换言之,该方法的应用场景是什么呢?为了解答这一问题,po主翻阅了部分文献,发现标准化多是为了实现可比性,而这种可比性又具体分为三点:变量可比性、组间可比性与跨期可比性,下文我们对其展开详细介绍。当然,受自身学识和专业所限,本文仅讨论标准化在经济学领域的部分作用

2.1 变量可比性

首先,对变量进行标准化处理可以实现不同变量间的可比性。

例如 Liang et al. ( 2020 ) 在研究空气污染对鸟类数量的影响时,为比较不同污染物 ( 臭氧和可吸入颗粒物 ) 浓度的差异性影响,作者即对两类污染的值做了标准化处理:

We use the standardized values so that we can compare the relative magnitudes of βozone and βPM, since the different pollutants have different units of measurement. The coefficients can be interpreted as the standard deviation change in bird abundance from a 1 standard deviation increase in ozone or particulate matter.

标准化之后,变量已被剔除量纲,回归系数可以直接解释为 x 变动一个标准差对 y 的标准差的影响。

在教育经济学中,为了进行跨学科之间的比较,学者们也经常对变量进行标准化处理。例如 Bietenbeck et al. ( 2018 ) 在研究教师学科知识水平对学生成绩的影响时,即对不同学科教师的知识水平,以及学生不同学科的成绩,都进行的标准化处理,由此得出的结果才是可比的。

另外,在主成分分析时,通常也会对变量进行标准化处理,目的之一也是对剔除各变量的量纲,实现变量间的可比性。

2.2 组间可比性

标准化处理也可以实现组间可比性。例如 Lee et al. ( 2021 ) 研究极端天气 ( 干旱、洪涝、雪灾、低温 ) 对名人死亡率的影响时,即对黄河、长江和珠江三条河流的洪涝灾害数进行了标准化处理,因为三个流域之间的自然条件和制度背景都存在差异,跨流域间的洪涝灾害数无法直接进行比较:

While the flood counts are provided separately for the Yellow River, the Yangtze river, and the Pearl River. To make the three counts comparable ( rivers have different frequencies of floods for natural and institutional reasons ) and aggregatable, we first conducted normalization on each of them.

劳动经济学中也经常将各种指标进行标准化处理,以实现不同组别之间的比较。例如 Fort et al. ( 2020 ) 在研究日托对儿童认知能力 ( IQ ) 与非认知能力 ( 人格特征测试 ) 的影响时,即对学生两种能力的得分进行了标准化处理。因为不同班级、不同年级,乃至不同学校之间,学生的学习基础、考试难度、教学环境等一系列因素都存在极大的异质性,单纯比较分数的绝对值可能没有任何意义。

2.3 跨期可比性

再者,为了实现不同时期间的可比性,学者们通常也会对变量做标准化处理。这么做的潜在假设是,不同时期个体所处的环境可能存在巨大的差异。举个两个简单的例子,同样的温度,夏天的 20 摄氏度和冬天 20 摄氏度给人的感觉是完全不一样的。在考研数学中,也经常流传中单数年容易双数年难的逸闻。

此类研究多基于时间 ( 年份、学期、季度等 ) 对变量进行标准化。如 Attanasio et al. ( 2020 ) 在研究父母投入对子女人力资本发展的影响时,为了实现跨期可比性,即对孩子的身高、体重、语言能力等指标基于年龄进行了标准化处理:

valid comparisons over time require that the scaling of the latent factors are comparable between periods. One way to meet this condition is to normalize each factor on the same measure every period.

有时候,我们也会使用面板数据,此时对数据进行标准化处理,则可以同时实现两个功能:组间可比性与跨期可比性。例如 Thompson ( 2021 ) 在研究上课时间对学生成绩时,为了实现各年级、年份的考试分数具有可比性,即对数据进行了标准化处理:

In order to make test scores comparable across grades and years, I use a common convention in the literature and standardize these test scores within grade and school year so that the grade-by-year test scores have means of zero and standard deviations of one.

3. 三种方法

3.1 Z-score Normalization

目前有三种主流方法,其中最常见的叫做 z-score normalization ,也被称之为 Standardization ,具体方法为:

其中 mean(x) 表示变量 x 的均值, σ 表示变量的标准差。z-score normalization 之后,数据会变成均值为 0 、标准差为 1 的分布。

3.2 Min-Max Normalization

另一种较为常用的方法,叫做 min-max normalization ,通常简写为 normalization ,具体方法为:

其中 max(x) 是变量最大值, min(x) 是变量最小值。这种方法可以将数据范围映射到 [0,1] 区间内。

更广义的,它也可以将数据映射到任意你想要的范围内,具体方法为:

其中 ymin 和 ymax 是映射的目标区间最小值和最大值。

3.3 Mean Normalization

还有一种方法叫做 mean normalization ,看到有翻译成中心化或去中心化 ( 迷惑。。。) ,具体方法为:

其中 mean(x) 表示变量 x 的均值。

4. Stata 实现

下面我们随机生成两个正态分布: dist1N(3,22)dist2N(21,32) ,演示一下三种数据变换的实际效果。

clear
set seed 1234
set obs 1000

*- 生成正态分布
gen dist1 = rnormal(3, 2)
gen dist2 = rnormal(21, 3)

*- 绘图
twoway (histogram dist1)(histogram dist2) ///
 	(kdensity dist1)(kdensity dist2),	  ///
 	xlabel(-1(3)28) legend(off) graphregion(color(white))

这是数据的原始分布:

4.1 Z-score Normalization 实现

首先,我们对这两个变量进行 Z-score normalization ,Stata 中有专门的命令 norm ,大家通过 ssc install norm 安装该命令即可:

*-  Z-score normalization
ssc install norm, replace  //安装norm命令
norm dist1 dist2, method(zee)

*- 绘图
twoway (histogram zee_dist1)	///
	(histogram zee_dist2, color(white) lstyle(foreground)) ///
 	(kdensity zee_dist1)(kdensity zee_dist2),	  ///
 	legend(off) graphregion(color(white))

下图展示了标准化之后的结果,可以发现两个分布重合了。

sum 一下也可以发现,标准化之后,两个变量都成为均值为 0 ,标准差为 1 的分布。

. sum

  Variable |    Obs       Mean   Std. Dev.       Min        Max
-----------+---------------------------------------------------
     dist1 |  1,000   2.998486   2.035739  -3.435818   9.040881
     dist2 |  1,000   20.85914   2.870078   11.35959   32.52251
 zee_dist1 |  1,000  -1.95e-15          1  -3.160673   2.968159
 zee_dist2 |  1,000   2.71e-15          1  -3.309858   4.063779

当然,还有一个命令: center 也可以实现标准化,它与 norm + method(zee) 是等价的。

*- center + standardize 等价于 method(zee)
center dist1 dist2, prefix(z_) standardize

. sum zee_dist1 zee_dist2 z_dist1 z_dist2

  Variable |    Obs       Mean   Std. Dev.       Min        Max
-----------+---------------------------------------------------
 zee_dist1 |  1,000  -1.95e-15          1  -3.160673   2.968159
 zee_dist2 |  1,000   2.71e-15          1  -3.309858   4.063779
   z_dist1 |  1,000  -1.95e-15          1  -3.160673   2.968159
   z_dist2 |  1,000   2.71e-15          1  -3.309858   4.063779

4.2 Min-Max Normalization 实现

接下来我们同样借助 norm 命令,对两个变量进行标准化处理:

*- Min-Max Normalization
norm dist1 dist2, method(mmx)

*- 绘图
twoway (histogram mmx_dist1)	///
	(histogram mmx_dist2, color(white) lstyle(foreground)) ///
 	(kdensity mmx_dist1)(kdensity mmx_dist2),	  ///
 	legend(off) graphregion(color(white))

下图展示了标准化后的结果,两个变量的分布同样存在一定程度的重合。

随后我们 sum 一下,发现与原始数据相比,归一化之后的变量范围已经映射至 [0,1] 内。

. sum dist1 dist2 mmx_dist1 mmx_dist2

  Variable |    Obs       Mean   Std. Dev.       Min        Max
-----------+---------------------------------------------------
     dist1 |  1,000   2.998486   2.035739  -3.435818   9.040881
     dist2 |  1,000   20.85914   2.870078   11.35959   32.52251
 mmx_dist1 |  1,000   .5157056   .1631632          0          1
 mmx_dist2 |  1,000   .4488773   .1356183          0          1

4.3 Mean Normalization 实现

mean ormalization 在 Stata 中则没有专门的命令,我们可以写一个简单的循环来实现 ( 实际上,前两种方法也可以自己编写循环来实现 ) :

*- Mean normalization
foreach v in dist1 dist2 {
	sum `v'
	gen de_`v' = (`v' - r(mean)) / (r(max) - r(min))
	}

*- 绘图
twoway (histogram de_dist1)	///
	(histogram de_dist2, color(white) lstyle(foreground)) ///
 	(kdensity de_dist1)(kdensity de_dist2),	  ///
 	legend(off) graphregion(color(white))

上述代码中, r(mean)r(max)r(min) 分别表示变量的均值、最大值和最小值。我们同样画图展示 mean normalization 之后的变量分布:

可以发现,此时分布的均值已经变成零了。

5. 小结

以上就是关于标准化的基本内容了,由于中文翻译的不同,也有文章将其称为中心化、去中心化等,大家可以视其语境,具体问题具体分析。

需要强调的是,标准化本质上都是对数据进行线性变换,并不会改变数据的分布类型。变量的原始分布是非正态的,变换之后依然是非正态的。也正是因为这一点,许多领域都会在分析前对数据进行标准化,因为它即可以无损数据的原有信息,同时还能赋予数据一些新的优良性质。

6. 参考文献

  • Attanasio, O., Meghir, C., & Nix, E. (2020). Human capital development and parental investment in India. The Review of Economic Studies, 87(6), 2511-2541. -PDF-
  • Bietenbeck, J., Piopiunik, M., & Wiederhold, S. (2018). Africa’s Skill Tragedy Does Teachers’ Lack of Knowledge Lead to Low Student Performance?. Journal of Human Resources, 53(3), 553-578. -PDF-
  • Lee, W. S., & Li, B. G. (2021). Extreme weather and mortality: Evidence from two millennia of Chinese elites. Journal of Health Economics, 76, 102401. -PDF-
  • Liang, Y., Rudik, I., Zou, E. Y., Johnston, A., Rodewald, A. D., & Kling, C. L. (2020). Conservation cobenefits from air pollution regulation: Evidence from birds. Proceedings of the National Academy of Sciences, 117(49), 30900-30906. -PDF-
  • Fort, M., Ichino, A., & Zanella, G. (2020). Cognitive and noncognitive costs of day care at age 0–2 for children in advantaged families. Journal of Political Economy, 128(1), 158-205. -PDF-
  • Thompson, P. N. (2021). Is four less than five? Effects of four-day school weeks on student achievement in Oregon. Journal of Public Economics, 193, 104308. -PDF-

7. 附:本文代码

clear
set seed 1234
set obs 1000
gen dist1 = rnormal(3, 2)
gen dist2 = rnormal(21, 3)

twoway (histogram dist1)(histogram dist2) ///
 	(kdensity dist1)(kdensity dist2),	  ///
 	xlabel(-3(2)32) legend(off)           ///
    graphregion(color(white))

*-  Z-score normalization
ssc install norm, replace 
norm dist1 dist2, method(zee)

twoway ///
    (histogram zee_dist1)	///
	(histogram zee_dist2, color(white) lstyle(foreground)) ///
 	(kdensity zee_dist1)(kdensity zee_dist2),	  ///
 	legend(off) graphregion(color(white))

*- Min-Max Normalization
norm dist1 dist2, method(mmx)

twoway (histogram mmx_dist1)	///
	(histogram mmx_dist2, color(white) lstyle(foreground)) ///
 	(kdensity mmx_dist1)(kdensity mmx_dist2),	  ///
 	legend(off) graphregion(color(white))

*- center/standardize 等价于method(zze)
center dist1 dist2, prefix(z_) standardize

sum sum zee_dist1 zee_dist2 z_dist1 z_dist2

*- Mean normalization
foreach v in dist1 dist2 {
	sum `v'
	gen de_`v' = (`v' - r(mean)) / (r(max) - r(min))
	}

twoway (histogram de_dist1)	///
	(histogram de_dist2, color(white) lstyle(foreground)) ///
 	(kdensity de_dist1)(kdensity de_dist2),	  ///
 	legend(off) graphregion(color(white))

8. 相关推文

Note:产生如下推文列表的 Stata 命令为:
lianxh 离群值 对数 时间趋势 稳健性检验 安慰剂检验
安装最新版 lianxh 命令:
ssc install lianxh, replace

相关课程

免费公开课

最新课程-直播课

专题 嘉宾 直播/回看视频
最新专题 文本分析、机器学习、效率专题、生存分析等
研究设计 连玉君 我的特斯拉-实证研究设计-幻灯片-
面板模型 连玉君 动态面板模型-幻灯片-
面板模型 连玉君 直击面板数据模型 [免费公开课,2小时]
  • Note: 部分课程的资料,PPT 等可以前往 连享会-直播课 主页查看,下载。

课程主页

课程主页

关于我们

  • Stata连享会 由中山大学连玉君老师团队创办,定期分享实证分析经验。
  • 连享会-主页知乎专栏,400+ 推文,实证分析不再抓狂。直播间 有很多视频课程,可以随时观看。
  • 公众号关键词搜索/回复 功能已经上线。大家可以在公众号左下角点击键盘图标,输入简要关键词,以便快速呈现历史推文,获取工具软件和数据下载。常见关键词:课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法

连享会小程序:扫一扫,看推文,看视频……

扫码加入连享会微信群,提问交流更方便

✏ 连享会-常见问题解答:
https://gitee.com/lianxh/Course/wikis

New! lianxh 命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh