clear /* 清空内存 */
capture do close /* 关闭已开启命令文件 */
capture log close /* 关闭已开启日志文件 */
set more off /* 满屏后自动翻屏 */
cd "E:\统计\regression analysis\lab1" /* 转换工作目录 */
log using DataManagement.log, text replace /* 开启新的日志文件 */
use binlfp2, clear /* 将数据读入内容 */
****************
* 查看数据.
****************
describe /* 数据信息描述 */
codebook /* 变量编码册 */
codebook age
browse /* 打开数据浏览窗口 */
list in 1/10 /* 列出前10个案例 */
list inc hc wc if age>=30 & age<=35 /* 列出特定案例 */
summarize age inc /* 变量描述性统计: 连续变量 */
sum age inc
sum age inc, detail
tabulate hc /* 分类变量的分布表 */
tab hc
tab hc wc /* 分类变量交互表: 频数 */
tab hc wc, cell nofreq /* 交互表:百分比 */
tab hc wc, row nofreq /* 交互表:行百分比 */
tab hc wc, col nofreq /* 交互表:列百分比 */
tab hc wc, chi2 /* 交互表独立性卡方检验 */
order inc age wc hc lfp k5 k618 lwg /* 变量排序 */
list in 1/5
sort inc age /* 数据排序 */
list in 1/5
***********************
* 创建新变量
***********************
// 创建数值型变量: generate (缩写gen)
generate age2=age*age /* 新变量:年龄的平方 */
label variable age2 "age square" /* 添加变量标签 */
format age2 %5.2f /* 指定变量取值显示格式 */
// 创建分组(categorical)变量
tab age
gen agegrp1=1 /* 方式1: generate + replace */
replace agegrp1=2 if age>39 & age<50
replace agegrp1=3 if age>49 & age<61
tab agegrp1
format agegrp1 %2.0f
label variable agegrp1 "age group"
label define agegrplb 1 "30-39" 2 "40-49" 3 "50-60" /* 指定取值标签 */
label values agegrp1 agegrplb /* 将取之标签指定给变量 */
tab agegrp1
recode age (30/39=1) (40/49=2) (50/60=3), ///
gen(agegrp2) /* 方式2: recode,gen() */
tab agegrp1 agegrp2 /* 变量转换检查 */
label values agegrp2 agegrplb /* 取值标签的借用 */
recode age (30/39=1 "30-39") (40/49=2 "40-49") ///
(50/60=3 "50-60"), gen(agegrp3) /* 方式3: 方式2的变种 */
tab agegrp2 agegrp3 /* 变量转换检查 */
// 生成虚拟变量(dummy variable)
tab agegrp1, gen(dumage)
tab1 dumage*
// 利用系统变量_n新建ID变量
gen id=_n
order id
***********************
* 数据文件合并
***********************
// 观测案例的叠加:append
use newf1, clear
des
list
use newf2, clear
des
list
append using newf1
list
sort year
list
// 匹配合并:关键变量
use newf3,clear
des
list
use newf4, clear
des
list
sort year /* 对关键变量排序 */
merge year using newf3
des
list
**************************
* 画图:graph
**************************
use binlfp2.dta, clear
// 直方图
hist k618, percent by(wc)
// 直方图:正态分布密度图
hist inc,freq norm xlabel(0(10)100) xtitle("Family Income Excluding Women's")
//散点图
tw scatter lwg k618
log close