2
Stata数据处理
source link: https://ask.hellobi.com/blog/guofushuaizuo/36872
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
作者:Economicoder
1. 快捷键
- Fn + F2 : 描述数据(describe)
- Fn + PgUp : 搜索先前命令
- Ctrl + 8 : 打开 data editor (browse)
- Ctrl + 9 : 新建do文档
- Ctrl + D : 执行在do文档中选中的命令
- clear : 删除存储数据
- cls : clear the console
2. 目录
- pwd : print current working directory
- cd : change working directory
- dir : 显示工作目录中的所有文件
- dir *.dta : 显示工作目录中的dta文件
- capture log close
3. 探索数据
sysuse auto, clear // 用该数据举例
(1). 查看数据结构
- describe make price: 显示变量的类型、格式和标签
- lookfor "circle" : 在变量名、变量类型和标签中查找某字符串
- count : 统计观测值的数量
- count if price > 5000
- ds : 显示所有变量名称
- ds, has(type string)
- isid mpg : 检查变量的值是否唯一
(2). 查看数据分布
- codebook make price : 综述变量的类型、统计量、缺失值和唯一值的数量
- summarize make price mpg : 显示变量的总体统计量(mean, stdev, min, max)
- inspect mpg : 显示数据的直方图、缺失值与零值的数量
- histogram mpg , frequency : 绘制直方图
(3). 浏览观测值
- display price[4] : 显示变量的第4个观测值,只作用于该变量
- gsort price mpg : 升序排序
- gsort - price - mpg : 降序排序
- levelsof rep78 : 显示变量的唯一值
4. 转换数据格式
auto 数据中的 foreign 变量为蓝色字体,byte 格式,含有两种值:Foreign 和 Domestic
(1). 将数值转化为字符串
- gen foreignString = string( foreign )
- tostring foreign , gen( foreignString )
- decode foreign , gen( foreignString )
(2). 将字符串转化为数值
- gen foreignNumeric = real( foreignString )
- destring foreignString , gen( foreignNumeric )
- encode foreignString , gen( foreignNumeric )
5. 总结数据
- tabulate rep78 , mi gen( repairRecord ) : 显示变量的每个值出现的频数
- tabulate rep78 foreign , mi
- bysort rep78 : tabulate foreign
- tabstat price weight mpg , by( foreign ) stat( mean sd n )
6. 创建新变量
- gen byte lowPr = price < 4000 : 创建二值变量
- gen id = _n : 创建索引,即观测值所在行数,本例中为1-74
- bysort rep78 : gen repairIdx = _n
- gen totRows = _N : 创建一列,显示每组中的观测值总数,本例中都为74
- bysort rep78 : gen repairTot = _N
- egen meanPrice = mean( price ), by( foreign ) : 计算每组的平均价格
本文由Economicoder 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK