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

发布时间:2022-01-12 阅读 3379

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

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

New! lianxh 命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc, ihelp, rdbalance, gitee, installpkg

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

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

⛳ Stata 系列推文:

PDF下载 - 推文合集

作者: 连玉君 (中山大学)
邮箱: arlionn@163.com

仓库地址:连享会-Lasso专题

图形模板

. 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 
 
*-不同参数设定下的 RSS  
  set obs 100000
  gen b1 = .
  gen b2 = .
  gen yhat = .
  gen ehat2 = .
  gen rss  = .
  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 rss = r(sum) in `id'               // RSS
  		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 与 系数估计值的关系

*-RSS v.s. b1 and b2

  use "lasso_sim_contourline.dta", clear 
  sum b1 rss  b2
  sort rss 
  list rss b1 b2 in 1/10
  *-对比:OLS
    reg y x1 x2
    dis e(rss)

  *-图示:RSS = f(b1)|b2  
  use "lasso_sim_contourline.dta", clear 
  line rss b1, sort
  line rss b2, sort
  line rss b1 if abs(b2-0.9)<0.01
  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 等值曲线

*-RSS 等值曲线
  local J "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)) 
  addplot: contourline rss b1 b2 if rss!=., ///
         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))
  addplot: contourline rss b1 b2 if rss!=., ///
         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 
 
*-不同参数设定下的 RSS  
  set obs 100000
  gen b1 = .
  gen b2 = .
  gen yhat = .
  gen ehat2 = .
  gen rss  = .
  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 rss = r(sum) in `id'               // RSS
  		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 
  sum b1 rss  b2
  sort rss 
  list rss b1 b2 in 1/10
  *-对比:OLS
    reg y x1 x2
    dis e(rss)

  *-图示:RSS = f(b1)|b2  
  use "lasso_sim_contourline.dta", clear 
  line rss b1, sort
  line rss b2, sort
  line rss b1 if abs(b2-0.9)<0.01
  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 等值曲线
  local J "rss"
  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)) 
  addplot: contourline rss b1 b2 if rss!=., ///
         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))
  addplot: contourline rss b1 b2 if rss!=., ///
         level(`k') colorlines xlabel(none)

相关课程

免费公开课

最新课程-直播课

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

课程主页

课程主页

关于我们

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

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

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

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

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