[转贴] http://jinhe.xjtu.edu.cn/bbs/list.asp?boardid=5
STATA入门——报告提纲 各位: 下面是我下午报告的提纲,你们可以先浏览一下,有条件的同学最好可以先操作一下里面的命令,这样听起来效果会好的多。最好能带上钟老师的讲义。 arlion ******************************************************************* ******************chapter1 stata简介******************************** ******************************************************************* *----------------------------------1.1-----------stata的安装-------------------------------------------- **1.1 stata的安装 **1.1.1 安装目录和快捷方式 ** 开始--〉所有程序-->stata8--->右击设定起始位置 cd d:\stata8\ado\personal /*进入指定目录,并成为当前工作目录*/ cd chps **1.1.2系统设定 sysdir /*显示系统的安装目录信息*/ help set /*所有系统相关的设定*/ //whelp set set more on /*分屏显示,你可以试一下 set more off 的效果*/ clear set memory 30m /*设定数据的存储空间*/ set matsize 500 /*设定矩阵的大小 default is 400*/ // 记录所有操作结果和命令 capture log close /*capture 的作用在于“吃掉”错误信息,继续执行*/ capture cmdlog close log using chp1_intro , text replace cmdlog using chp1_intro_cmd , replace *----------------------------------------1.2-------------------------资料处理-------------------------- **1.2 资料管理 /* 这是实证分析中最为繁琐的一部分,一般而言,在资料分析和处理完毕后,实证分析工作也就 完成一半了。主要包括数据的读入,筛选,存储等。 */ **1.2.1 输入资料 /* 几乎在99%的情况下,我们都不会手动输入资料,资料都是通过文本文档(.txt )或是 excel文档(.xls)直接读入stata的。stata提供了一系列命令可以使我们很方便的进行资料的 读入和输出。在很多时候,使用text文本编辑器的替换功能往往能够起到事半功倍的作用。 */ // 读入以 -tab- 分割的资料 insheet using "d:\stata8\ado\personal\chps\chp1\auto.txt" **另一种方法是直接从excel中粘贴到stata中,这种方法比较实用。 //clear **1.2.2 浏览资料 ** 变量的描述信息 describe /*whelp describe*/ d , d ** 列示部分资料 list price in 1/5 list price turn if foreign == "domestic" ** 简单的统计描述 sum sum price len head ,d ** 利用表格进行统计 tabstat price mpg length turn , statistic(mean med min max) format(%6.3f) col(s) ** 给资料和变数加标签 gen aa = price^2 /*whelp gen ; whelp egen*/ label var aa "the sqaure of price" /*whelp label*/ label data "this is the data used for stata introduction" **1.2.3 修改存储资料 replace foreign = "china" if foreign == "domestic" preserve /*将当前资料备份,以下任何操作完成后仍可复原至当前状态*/ keep if price > 5000 save ex1 , replace save "d:\stata8\ado\personal\chps\chp1\e1.dta" , replace restore /*复原*/ /*说明: 一般情况下,如果你是使用 do 文档来处理数据,那么在完成分析后不必保存数据。*/ clear ** 调入已存储数据 use ex1 clear use "d:\stata8\ado\personal\chps\chp1\e1.dta" **1.2.4 合并资料 //merge /*横向合并*/ //apppend /*纵向追加*/ *-----------------------------------1.3----------------------------执行指令-------------------------------- **1.3 执行指令 ** 命令的基本格式 dis in yellow "the format of command in stata is : " _n "command [varlist] [if] [in] [,options] whelp reg ** 以sum 命令为例 sum price if foreign=="china" sum price in 1/10 bysort foreign: sum price , d format *------------------------------1.4----------------------------帮助的使用-------------------------- **1.4 帮助的使用 help sum whelp sum search sum *--------------------------------1.5----------------------------绘图--------------------------------------- ** 1.5 绘图 ** 基本语法格式:[twoway] graphtype varlist [weight] [if exp] [in range] [,options] scatter price weight twoway (scatter price weight) (lfit price weight) ** 处理断行的方法1:采用 #delimit 命令 #delimit ; twoway scatter price weight || lfit price weight , title(a graphing example) subtitle("given by arlion, sep2005") note("note: for details, see help twoway") ; #delimit cr set scheme economist /*设定图形的显示风格*/ ** 处理断行的方法2:采用 histogram price, /// title("histogram for price") set scheme s1mono ** 处理断行的方法3:采用 /**/ twoway ( function y = cos(x),range(0 12.6) ) /* */ ( function y = sin(x) ,range(0 6.29) ) , /* */ title("break the long line using /**/ ") /* */ legend(label (1 cos) label(2 sin)) **说明: see help legend. 作用在于为不同的子图形加标签 *-----------------------------------1.6------------------------程序初步---------------------------------------- **1.6 do文档和ado文档 ** 二者的区别 ** 一个简单的ado文档(stata程序) chp1_title **1.6.1 简介 tab_title , title("introduction to stata") tc(result) lc(text) tab_title tab_title , title("given by arlion") tc(abc) **1.6.2 暂元 local macro local title "introduction to stata" dis "`title'" local i = 1 dis `i' local i = `i' + 2 dis `i' local ++i dis `i' local a "i am" local b " a stata user" local c "`a'""`b'" dis "`c'" local aa 2+2 dis `aa' local aa = 2+2 dis `aa' local aa "2+2" dis "`aa'" **1.6.3 控制语句 ** ----if----- 条件语句 ** whelp ifcmd //------eg1------- if foreign=="china"{ sum price } if price<8000{ sum mpg } ** ----forvaues---- 数字循环控制语句 //------eg2------- forvalues i = 1(2)10{ dis "line: " `i' } //------eg3------- mat a = j(10,3,0) mat list a forvalues rr = 1(1)10{ forvalues cc = 1(1)3{ mat a[`rr' , `cc' ] = `rr' + `cc' } } mat list a ** ----foreach--- 文字、变量等的循环控制 //-----eg4----- local names "arlion jones lucas xiaoming" foreach nam of local names{ dis dis "`nam'" dis } //-----eg4----- foreach var of varlist price mpg weight length{ dis dis in white "===summary of variable=====" in yellow "`var'" sum `var' dis _n(2) /*空两行显示后面的内容*/ } ** ----while--- local i = 1 while `i' <10{ dis in g "this is the " in y `i' "th" in g " number" local i = `i' + 1 } ** 1.6.4 程序的调试和勘误 set trace on tab_title , title("haha") tc(adfal) set trace off *-----------------------------------1.7------------------------其他内容-------------------------------------- ** 函数 see help functions ** 矩阵 see help matrix ** 如有任何问题,请在本版交流讨论,或到金禾论坛(http://jinhe.xjtu.edu.cn/bbs/list.asp?boardid=5) ** 以上只是一个简要的介绍,具体内容请参考钟老师的讲义,那是我所见过的关于stata ** 的最好的讲义了。 |