# 图解Lasso系列A：Lasso的变量筛选能力

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

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`

⛳ Stata 系列推文：

## 图形模板

``````. ssc install schemepack, replace  //white_tableau 模板
. set scheme white_tableau //设定绘图风格为white_tableau
``````

## DGP

``````*-生成数据
clear
set obs  100
set seed 123
gen obsid = _n
gen x1 = rnormal()
gen x2 = rnormal()
gen  e = rnormal()*0.7
gen  y = 0.3*x1 + 0.9*x2 + e

set obs 100000
gen b1 = .
gen b2 = .
gen yhat = .
gen ehat2 = .
gen   L1 = .
gen   L2 = .
local id = 1  // 计数器
forvalues b1 = -1.2(0.01)0.8{    //-0.2(0.03)0.8
forvalues b2 = -1.3(0.01)1.5{ //0.3(0.03)1.5
qui{
replace yhat  = x1*`b1' + x2*`b2' in 1/100 // yhat
replace ehat2 = (y - yhat)^2      in 1/100 // e^2
sum ehat2 in 1/100
replace L1 = abs(`b1') + abs(`b2') in `id' // L1-norm
replace L2 = (`b1')^2+ (`b2')^2    in `id' // L2-norm,加括号!
replace b1 = `b1'  in `id'
replace b2 = `b2'  in `id++'
}
if mod(`id',50)==0 {
dis "." _c
}
}
}
save "lasso_sim_contourline.dta", replace
``````

``````*-RSS v.s. b1 and b2

use "lasso_sim_contourline.dta", clear
list rss b1 b2 in 1/10
*-对比：OLS
reg y x1 x2

use "lasso_sim_contourline.dta", clear
line rss b2 if abs(b1-0.3)<0.01, sort

tw (line rss b1 if abs(b2-0.9)<0.01) ///
(line rss b2 if abs(b1-0.3)<0.01, sort), ///
xline(0.3 0.9) xlabel(-1 0 0.3 0.9 2) legend(off)
``````

``````*-RSS 等值曲线
twoway contourline `J' b1 b2 if rss!=., ///
level(20) colorlines
``````

## Lasso 约束集

``````*-L1-norm 约束集
local J "L1" // L1-norm = |b1| + |b2|
twoway contourline `J' b1 b2 if rss!=., ///
level(30) colorlines aspectratio(0.7)
``````

## Ridge 约束集

``````*-L2-norm 约束集
local J "L2" // L2-norm = |b1|^2 + |b2|^2
twoway contourline `J' b1 b2 if rss!=., ///
level(30) colorlines aspectratio(0.7)
``````

## Lasso

``````*-Lasso 图示  // L1-norm = |b1| + |b2|
local k=30  // 圆圈个数
twoway contourline L1 b1 b2 if rss!=., ///
level(`k') aspectratio(0.8) ///
xline(0,lc(red)) yline(0,lc(red))
level(`k') colorlines xlabel(none)
``````

## Ridge

``````*-Ridge 图示  // L2-norm = |b1|^2 + |b2|^2
local k=30  // 圆圈个数
twoway contourline L2 b1 b2 if rss!=., ///
level(`k') aspectratio(0.8) ///
xline(0,lc(red)) yline(0,lc(red))
level(`k') colorlines xlabel(none)
``````

## 完整代码

`2022/1/11 11:47`

``````*-生成数据
clear
set obs  100
set seed 123
gen obsid = _n
gen x1 = rnormal()
gen x2 = rnormal()
gen  e = rnormal()*0.7
gen  y = 0.3*x1 + 0.9*x2 + e

set obs 100000
gen b1 = .
gen b2 = .
gen yhat = .
gen ehat2 = .
gen   L1 = .
gen   L2 = .
local id = 1  // 计数器
forvalues b1 = -1.2(0.01)0.8{    //-0.2(0.03)0.8
forvalues b2 = -1.3(0.01)1.5{ //0.3(0.03)1.5
qui{
replace yhat  = x1*`b1' + x2*`b2' in 1/100 // yhat
replace ehat2 = (y - yhat)^2      in 1/100 // e^2
sum ehat2 in 1/100
replace L1 = abs(`b1') + abs(`b2') in `id' // L1-norm
replace L2 = (`b1')^2+ (`b2')^2    in `id' // L2-norm,加括号!
replace b1 = `b1'  in `id'
replace b2 = `b2'  in `id++'
}
if mod(`id',50)==0 {
dis "." _c
}
}
}
save "lasso_sim_contourline.dta", replace

use "lasso_sim_contourline.dta", clear
list rss b1 b2 in 1/10
*-对比：OLS
reg y x1 x2

use "lasso_sim_contourline.dta", clear
line rss b2 if abs(b1-0.3)<0.01, sort

tw (line rss b1 if abs(b2-0.9)<0.01) ///
(line rss b2 if abs(b1-0.3)<0.01, sort), ///
xline(0.3 0.9) xlabel(-1 0 0.3 0.9 2) legend(off)

twoway contourline `J' b1 b2 if rss!=., ///
level(20) colorlines

*-L1-norm 约束集
local J "L1" // L1-norm = |b1| + |b2|
twoway contourline `J' b1 b2 if rss!=., ///
level(30) colorlines aspectratio(0.7)

*-L2-norm 约束集
local J "L2" // L2-norm = |b1|^2 + |b2|^2
twoway contourline `J' b1 b2 if rss!=., ///
level(30) colorlines aspectratio(0.7)

*-Lasso 图示  // L1-norm = |b1| + |b2|
use "lasso_sim_contourline.dta", clear
local k=30  // 圆圈个数
twoway contourline L1 b1 b2 if rss!=., ///
level(`k') aspectratio(0.8) ///
xline(0,lc(red)) yline(0,lc(red))
level(`k') colorlines xlabel(none)
graph export "\$Out/lasso-stata-regular01.png", replace width(3000)

*-Ridge 图示  // L2-norm = |b1|^2 + |b2|^2
use "lasso_sim_contourline.dta", clear
local k=30  // 圆圈个数
twoway contourline L2 b1 b2 if rss!=., ///
level(`k') aspectratio(0.8) ///
xline(0,lc(red)) yline(0,lc(red))
level(`k') colorlines xlabel(none)
``````

## 相关课程

### 最新课程-直播课

• Note: 部分课程的资料，PPT 等可以前往 连享会-直播课 主页查看，下载。

### 关于我们

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

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

New！ `lianxh` 命令发布了：

`. ssc install lianxh`

`. help lianxh`