Stata连享会 主页 || 视频 || 推文 || 知乎 || Bilibili 站
温馨提示: 定期 清理浏览器缓存,可以获得最佳浏览体验。
New!
lianxh
命令发布了:
随时搜索推文、Stata 资源。安装:
. ssc install lianxh
详情参见帮助文件 (有惊喜):
. help lianxh
连享会新命令:cnssc
,ihelp
,rdbalance
,gitee
,installpkg
⛳ Stata 系列推文:
作者: 连玉君 (中山大学)
邮箱: arlionn@163.com
仓库地址:连享会-Lasso专题
. ssc install schemepack, replace //white_tableau 模板
. set scheme white_tableau //设定绘图风格为white_tableau
*-生成数据
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|
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 图示 // 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小时] |
⛳ 课程主页
⛳ 课程主页
关于我们
课程, 直播, 视频, 客服, 模型设定, 研究设计, stata, plus, 绘图, 编程, 面板, 论文重现, 可视化, RDD, DID, PSM, 合成控制法
等
连享会小程序:扫一扫,看推文,看视频……
扫码加入连享会微信群,提问交流更方便
✏ 连享会-常见问题解答:
✨ https://gitee.com/lianxh/Course/wikis
New!
lianxh
命令发布了:
随时搜索连享会推文、Stata 资源,安装命令如下:
. ssc install lianxh
使用详情参见帮助文件 (有惊喜):
. help lianxh