Stata命令整理教学内容

合集下载

零基础小白STATA数据分析实用常见命令整理

零基础小白STATA数据分析实用常见命令整理

STATA基础入门零基础实用命令整理第一章数据的读入与熟悉1.读入文件中的部分变量. use[变量] using [文件名]Eg . use age sex height weight using [文件名]2.读入文件中的部分观察量. use[文件名] in X/Y. use "I:\stata\chapter3.dta" in 601/1000软件只读入从第601个观察到第1000个观察之间的400个观察量3.描述、管理数据的基本命令命令功能. describe描述数据的基本情况:样本总量、变量总数、变量的格式等. list. list [变量名]-列出数据中所有变量的分布,从第一个样本到最后一个样本-列出选定变量的分布. list [变量名] in X/Y 列出数据中被选定的变量分布。

in限定数据的观察值范围。

比如,若只想查看第100个-200个观察值的分布,则将X/Y替换成100/200. order [变量名]按选定变量排序。

比如,样本的编号、年龄、性别、教育程度,……,等. aorder 将所有变量从 a-z 排序. label variable给变量贴上标签命令功能. sort [变量名] -将某个变量的数值进行排序。

一般情况下,排序的方式是从小到大-可同时排序多个变量-Stata将缺失值描述为最大数值,故排列在最后. sort [变量名] [in] 对某些变量的某个取值范围进行排序;没有指定的取值范围保持在原地方. gsort [+|-][变量名] -可从小到大和从大到小-若变量名前没有任何符号或加上+号,则按升序排列;若在变量名前加上-号,则按降序排列-变量可以是数值型、也可以是字符型. gsort [+|-][变量名] ,mfirst -mfirst指定将缺失值置于所有有效数值之前. gsort -age第二章变量的生成与处理1.离散和连续测量离散方式(discrete measure):由定性测量和定序测量组成;适用于低层次数据连续方式(continuous measure):由定距测量和定比测量组成。

计量经济学stata命令汇总

计量经济学stata命令汇总

计量经济学stata命令汇总1. 数据处理与描述性统计summarize 变量1 变量2…计算变量的均值、中位数等统计量tabulate 变量1 变量2…制表histogram 变量画单变量直方图scatter 变量1 变量2…画双变量散点图graph twoway 程序名变量1 变量2…绘制双变量图形sort 变量按照变量排序by 变量: 命令按照变量拆分数据并执行命令replace 变量=表达式替换变量中的值generate 新变量=表达式生成新变量egen 新变量=函数(变量) 生成新变量2. 回归分析regress 因变量自变量1 自变量2…普通最小二乘回归reg 相关变量,robust 异方差鲁棒性回归logit 因变量自变量1 自变量2…二元Logit模型probit 因变量自变量1 自变量2…二元Probit模型tobit 因变量自变量1 自变量2… 截尾变量(下界或上界)cens(下界或上界) 截尾Tobit模型heckman 因变量自变量1 自变量2… 难以观察到自变量矩阵决策过程变量名称=接收权值做二阶段回归Heckman选择模型pheckman 因变量自变量1 自变量2… 难以观察到自变量矩阵决策过程经验Bayes做二阶段回归Pooled Heckman选择模型xtset 变量1 变量2…指定面板数据xtreg 因变量自变量1 自变量2…, fe/be/fevd/arellano间隔估计xtlogit 因变量自变量1 自变量2…, fe面板Logit模型xtprobit 因变量自变量1 自变量2…, fe面板Probit模型3. 时间序列分析dfuller 变量单位根检验tsset 变量指定时间序列数据tsline 变量绘制时间序列图arma 阶数, lags(*laglist*) ARMA过程估计arima 阶数, lags(*laglist*) 差分阶数(*diff*) 现有模型(*model*) ARIMA模型估计arch hq/aic, lags(*laglist*) ARCH模型估计garch q=p o=r t=m, arch(q) garch(p) GARCH模型估计ivregress (2SLS)因变量自变量1(内生变量)编号=gmm/cluster(varname) 内生变量外生变量IV或2SLS回归分析4. 面板数据分析&横截面数据分析xtsum 等对面板数据的描述统计量xttest0 2个变量计算相对于H0的t值,考虑了异方差和面板数据结构(前提是两个变量符合随机效应或固定效应假设)xttobit 因变量自变量1 自变量2… 下界 cens(下界或上界)面板Tobit模型xtreg 因变量自变量1 自变量2…, fe/be/fevd/arellano面板回归模型xtlogit/xtprobit 因变量自变量1 自变量2…, fe面板分类模型5. 高级统计方法cluster 变量聚类分析pca 变量1 变量2…, components(4)主成分分析mvreg 因变量向量1 向量2…, clustervar(cluster)多元回归及聚类分析multilevel 因变量自变量1 自变量2…, mle 内部命令(通常是cov)多层线性模型分析glm 因变量自变量1 自变量2…, family(binomial) 连接函数(logit/probit) 难以观察到自变量(即随机拦截模型)其他选项广义线性模型分析heckprob/reg3 因变量自变量1 自变量2… 等随机效应模型分析。

Stata学习讲义

Stata学习讲义

Stata学习讲义刘志阔一、如何导入数据Stata的数据处理功能是极其强大的,不过我们最好在excel中整理数据,然后导入到stata中就可以了。

命令:insheet using name.csv*注意,Stata只能用csv格式,另外把数据放到stata的目录中。

二、如何进行回归Stata中有很多命令,这些命令都是现成的,直接用就可以了。

不过,怎么用是个问题。

熟悉命令的基础上学会如何使用Help。

最简单的命令reg做ols回归,xtreg处理面板等。

命令:reg y x*注意,Stata命令的格式,自己回去看手册。

网络帮助可以采用如下命令获得findit scat3, net;search scat3, net三、如何导出结果Stata可以直接导出发表论文中回归结果,当然不是完全一样。

命令:outreg2 Results using name.word四、如何画图Stata的画图功能也是极其强大的,可以画出各种类型的图标。

命令:scatter y x || lfit y x五、如何存储结果Stata可以储存回归结果,便于分析。

命令:log using name log closed1.codebook可以查看数据有没有缺失2.xml_tab estout 可以输出结果3.qui tab year, gen(yr) 可以生产时间虚拟变量。

4.g q=quarterly( qtr,"YQ")5.form q %tq6.recode province (min/11=1) (12/19=2) (20/31=3)gen eastern=(province==1)gen middle=(province==2)gen western=(province==3)Logout 命令可以把界面内容存到word里面,而不用复制。

Logout,save(名称) word/excel replace:各种描述性命令,statsXml_tab可以输出Excel格式的结果。

Stata操作讲义知识讲解

Stata操作讲义知识讲解

Stata操作讲义知识讲解S t a t a操作讲义Stata操作讲义第一讲 Stata操作入门第一节概况Stata最初由美国计算机资源中心(Computer Resource Center)研制,现在为Stata公司的产品,其最新版本为7.0版。

它操作灵活、简单、易学易用,是一个非常有特色的统计分析软件,现在已越来越受到人们的重视和欢迎,并且和SAS、SPSS一起,被称为新的三大权威统计软件。

Stata最为突出的特点是短小精悍、功能强大,其最新的7.0版整个系统只有10M左右,但已经包含了全部的统计分析、数据管理和绘图等功能,尤其是他的统计分析功能极为全面,比起1G以上大小的SAS系统也毫不逊色。

另外,由于Stata在分析时是将数据全部读入内存,在计算全部完成后才和磁盘交换数据,因此运算速度极快。

由于Stata的用户群始终定位于专业统计分析人员,因此他的操作方式也别具一格,在Windows席卷天下的时代,他一直坚持使用命令行/程序操作方式,拒不推出菜单操作系统。

但是,Stata的命令语句极为简洁明快,而且在统计分析命令的设置上又非常有条理,它将相同类型的统计模型均归在同一个命令族下,而不同命令族又可以使用相同功能的选项,这使得用户学习时极易上手。

更为令人叹服的是,Stata语句在简洁的同时又拥有着极高的灵活性,用户可以充分发挥自己的聪明才智,熟练应用各种技巧,真正做到随心所欲。

除了操作方式简洁外,Stata的用户接口在其他方面也做得非常简洁,数据格式简单,分析结果输出简洁明快,易于阅读,这一切都使得Stata成为非常适合于进行统计教学的统计软件。

Stata的另一个特点是他的许多高级统计模块均是编程人员用其宏语言写成的程序文件(ADO文件),这些文件可以自行修改、添加和下载。

用户可随时到Stata网站寻找并下载最新的升级文件。

事实上,Stata的这一特点使得他始终处于统计分析方法发展的最前沿,用户几乎总是能很快找到最新统计算法的Stata程序版本,而这也使得Stata 自身成了几大统计软件中升级最多、最频繁的一个。

stata操作介绍之基础部分(一)讲述

stata操作介绍之基础部分(一)讲述

3.1 变量与变量值
• Stata变量的命名原则:
. 变量名中字符的组成部分为A~Z,a~z、0~9与下划线“ _ ” ,这些字符以外的其他符号不能出现在变量名当中; . 变量名不能以数字作为开始符号; . 变量名区分大小写字母,而且不能识别汉字;
• 变量的取值类型: 1、字符型变量:由特定的字符串构成,用来分辨不同的类型; 2、数值型变量:数值变量的取值由数字构成,参与数字运算; 3、日期型变量:在Stata中,1960 年1 月1 日被认为是第0 天, 因此1959 年12 月31 日为第-1天,表示形式为:jan/10/2001或者 10jan2001; 4、缺失值:STATA 默认的缺失值用“.”来表示;
• 网络帮助: 如 . net from (连接stata官网)
二、Stata使用基础
2.1 Stata命令结构
• Stata的通用命令结构如下:
[ prefix : ] command [ varlist ] [= exp.] [ if exp. ] [ using filename ] [ in range ] [ weight = ] [ , options ]
术语 prefix command 含义 命令前缀 命令 术语 using filename in range 含义 使用的文件 观察个案范围
varlist
= exp.
变量串
表达式 条件表达式
weight
权重
选项
options
if exp.
• Stata常用命令及其缩写
命令或选项 list describe display summarize tabulate lable li des di, dis sum ta, tab lab 缩写 含义 列出变量 描述分析 展示变量 统计摘要 列表显示 标签 命令或选项 rename generate graph regress variable column ren gen, g gr reg var col 缩写 含义 重命名 新建变量 绘图 回归 变量 列

stata语法

stata语法

Stata语法简介Stata是一种常用的统计分析软件,具有强大的数据管理和统计功能。

本文将详细介绍Stata的基本语法和常用命令,以帮助读者快速上手使用Stata进行数据分析和统计建模。

安装和启动Stata1.安装Stata软件:首先,需要从Stata官网下载并安装Stata软件。

按照安装向导进行操作,完成安装过程。

2.启动Stata软件:双击桌面上的Stata图标,或者在开始菜单中找到Stata程序,点击打开。

基本语法Stata的基本语法遵循以下几个规则: 1. 命令不区分大小写:Stata中的命令不区分大小写,例如summarize和SUMMARIZE是等效的。

2. 命令以英文句点(.)结尾:在Stata中,每条命令都要以英文句点结尾。

例如,使用summarize命令计算变量的描述统计信息,应该输入summarize varname.。

3. 使用分号(;)分隔多个命令:如果需要在一行中输入多个命令,可以使用分号进行分隔。

例如,clear; use filename表示先清除当前的数据,然后使用指定的数据文件。

4. 使用斜杠(/)表示换行:当命令太长时,可以使用斜杠表示换行。

例如,summarize varname1 varname2 / varname3 varname4表示对变量varname1和varname2进行描述统计,并对变量varname3和varname4进行描述统计。

数据管理Stata提供了丰富的数据管理功能,包括数据导入、数据清洗、数据变换等。

数据导入使用Stata导入数据的常用命令有: - use:使用指定的数据文件,例如use mydata.dta。

- import excel:导入Excel文件,例如import excel "myfile.xlsx",sheet("Sheet1") firstrow clear。

- import delimited:导入文本文件,例如import delimited "mydata.csv", clear.数据清洗Stata提供了多种数据清洗工具,例如: - drop:删除指定的变量,例如drop varname。

Stata命令语法和基本命令语法的基本教程,以及控制数据列表的外观说明书

Stata命令语法和基本命令语法的基本教程,以及控制数据列表的外观说明书

10Listing data and basic command syntaxCommand syntaxThis chapter gives a basic lesson on Stata’s command syntax while showing how to control the appearance of a data list.As we have seen throughout this manual,you have a choice between using menus and dialogs and using the Command window.Although many find the menus more natural and the Command window baffling at first,some practice makes working with the Command window often much faster than using menus and dialogs.The Command window can become a faster way of working because of the clean and regular syntax of Stata commands.We will cover enough to get you started;help language has more information and examples,and [U ]11Language syntax has all the details.The syntax for the list command can be seen by typing help list :list varlistif in ,optionsHere is how to read this syntax:•Anything inside square brackets is optional.For the list command,a.varlist is optional.A varlist is a list of variable names.b.if is optional.The if qualifier restricts the command to run only on those observations for which the qualifier is true.We saw examples of this in [GSW ]6Using the Data Editor .c.in is optional.The in qualifier restricts the command to run on particular observation numbers.d.,and options are optional.options are separated from the rest of the command by a comma.•Optional pieces do not preclude one another unless explicitly stated.For the list command,it is possible to use a varlist with if and in .•If a part of a word is underlined,the underlined part is the minimum abbreviation.Any abbreviation at least this long is acceptable.a.The l in list is underlined,so l ,li ,and lis are all equivalent to list .•Anything not inside square brackets is required.For the list command,only the command itself is required.Keeping these rules in mind,let’s investigate how list behaves when called with different arguments.We will be using the dataset afewcarslab.dta from the end of the previous chapter.list with a variable listVariable lists (or varlist s)can be specified in a variety of ways,all designed to save typing and encourage good variable names.•The varlist is optional for list .This means that if no variables are specified,it is equivalent to specifying all variables.Another way to think of it is that the default behavior of the command is to run on all variables unless restricted by a varlist .•You can list a subset of variables explicitly,as in list make mpg price .•There are also many shorthand notations:m*means all variables starting with m .price-weight means all variables from price through weight in the dataset order.ma?e means all variables starting with ma ,followed by any character,and ending in e .12[GSW]10Listing data and basic command syntax•You can list a variable by using an abbreviation unique to that variable,as in list gear r~o.If the abbreviation is not unique,Stata returns an error message..listmake price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic5.Datsun5105079242280 3.54foreign6.Buick Regal5189203280 2.93domestic7.Datsun8108129.2750 3.55foreign.l make mpg pricemake mpg price1.VW Rabbit2546972.Olds982188143.Chev.Monza.36674.2240995.Datsun5102450796.Buick Regal2051897.Datsun810.8129.list m*make mpg1.VW Rabbit252.Olds98213.Chev.Monza.4.225.Datsun510246.Buick Regal207.Datsun810..li price-weightprice mpg weight1.46972519302.88142140603.3667.27504.40992229305.50792422806.51892032807.8129.2750[GSW]10Listing data and basic command syntax3.list ma?emake1.VW Rabbit2.Olds983.Chev.Monza4.5.Datsun5106.Buick Regal7.Datsun810.l gear_r~ogear_r~o1. 3.782. 2.413. 2.734. 3.585. 3.546. 2.937. 3.55list with ifThe if qualifier uses a logical expression to determine which observations to use.If the expression is true,the observation is used in the command;otherwise,it is skipped.The operators whose results are either true or false are<less than<=less than or equal==equal>greater than>=greater than or equal!=not equal&and|or!not(logical negation;~can also be used)()parentheses are for grouping to specify order of evaluationIn the logical expressions,&is evaluated before|(similar to multiplication before addition in arithmetic).You can use this in your expressions,but it is often better to use parentheses to ensure that the expressions are evaluated in the proper order.See[U]13.2Operators for complete details.4[GSW]10Listing data and basic command syntax.listmake price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic5.Datsun5105079242280 3.54foreign6.Buick Regal5189203280 2.93domestic7.Datsun8108129.2750 3.55foreign.list if mpg>22make price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign3.Chev.Monza3667.2750 2.73domestic5.Datsun5105079242280 3.54foreign7.Datsun8108129.2750 3.55foreign.list if(mpg>22)&!missing(mpg)make price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign5.Datsun5105079242280 3.54foreign.list make mpg price gear if(mpg>22)|(price>8000&gear<3.5)make mpg price gear_r~o1.VW Rabbit254697 3.782.Olds98218814 2.413.Chev.Monza.3667 2.735.Datsun510245079 3.547.Datsun810.8129 3.55.list make mpg if mpg<=22in2/4make mpg2.Olds98214.22In the listings above,we see more examples of Stata treating missing numerical values as large values, as well as the care that should be taken when the if qualifier is applied to a variable with missing values.See[GSW]6Using the Data Editor.[GSW]10Listing data and basic command syntax5 list with if,common mistakesHere is a series of listings with common errors and their corrections.See if you canfind the errors before reading the correct entry..listmake price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic5.Datsun5105079242280 3.54foreign6.Buick Regal5189203280 2.93domestic7.Datsun8108129.2750 3.55foreign.list if mpg=21=exp not allowedr(101);The error arises because“equal”is expressed by==,not by=.Corrected,it becomes.list if mpg==21make price mpg weight gear_r~o foreign2.Olds988814214060 2.41domesticOther common errors with logic:.list if mpg==21if weight>4000invalid syntaxr(198);.list if mpg==21and weight>4000invalid’and’r(198);Joint tests are specified with&,not with the word and or multiple if s.The if qualifier should be if mpg==21&weight>4000,not if mpg==21if weight>4000.Here is its correction:.list if mpg==21&weight>4000make price mpg weight gear_r~o foreign2.Olds988814214060 2.41domestic6[GSW]10Listing data and basic command syntaxA problem with string variables:.list if make==Datsun510Datsun not foundr(111);Strings must be in double quotes,as in make=="Datsun510".Without the quotes,Stata thinks thatDatsun is a variable that it cannotfind.Here is the correction:.list if make=="Datsun510"make price mpg weight gear_r~o foreign5.Datsun5105079242280 3.54foreignConfusing value labels with strings:.list if foreign=="domestic"type mismatchr(109);Value labels look like strings,but the underlying variable is numeric.Variable foreign takes on values 0and1but has the value label that attaches0to“domestic”and1to“foreign”(see[GSW]9Labeling data).To see the underlying numeric values of variables with labeled values,use the label list command(see[D]label),or investigate the variable with codebook varname.We can correct the error here by looking for observations where foreign==0.There is a second construction that also allows the use of the value label directly..list if foreign==0make price mpg weight gear_r~o foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic6.Buick Regal5189203280 2.93domestic.list if foreign=="domestic":originmake price mpg weight gear_r~o foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic6.Buick Regal5189203280 2.93domestic[GSW]10Listing data and basic command syntax7 list with inThe in qualifier uses a numlist to give a range of observations that should be listed.numlist s have the form of one number orfirst/last.Positive numbers count from the beginning of the dataset.Negative numbers count from the end of the dataset.Here are some examples:.listmake price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic5.Datsun5105079242280 3.54foreign6.Buick Regal5189203280 2.93domestic7.Datsun8108129.2750 3.55foreign.list in1make price mpg weight gear_r~o foreign1.VW Rabbit4697251930 3.78foreign.list in-1make price mpg weight gear_r~o foreign7.Datsun8108129.2750 3.55foreign.list in2/4make price mpg weight gear_r~o foreign2.Olds988814214060 2.41domestic3.Chev.Monza3667.2750 2.73domestic4.4099222930 3.58domestic.list in-3/-2make price mpg weight gear_r~o foreign5.Datsun5105079242280 3.54foreign6.Buick Regal5189203280 2.93domesticControlling the list outputThefine control over list output is exercised by specifying one or more options.You can use sepby()to separate observations by variable.abbreviate()specifies the minimum number of characters to abbreviate a variable name in the output.divider draws a vertical line between the variables in the list.8[GSW]10Listing data and basic command syntax.sort foreign.list ma p g f,sepby(foreign)make price gear_r~o foreign1.Olds9888142.41domestic2.Chev.Monza3667 2.73domestic3.Buick Regal5189 2.93domestic4.4099 3.58domestic5.Datsun5105079 3.54foreign6.VW Rabbit4697 3.78foreign7.Datsun8108129 3.55foreign.list make weight gear,abbreviate(10)make weight gear_ratio1.Olds9840602.412.Chev.Monza2750 2.733.Buick Regal3280 2.934.2930 3.585.Datsun5102280 3.546.VW Rabbit1930 3.787.Datsun8102750 3.55.list,dividermake price mpg weight gear_r~o foreign1.Olds9888142140602.41domestic2.Chev.Monza3667.2750 2.73domestic3.Buick Regal5189203280 2.93domestic4.4099222930 3.58domestic5.Datsun5105079242280 3.54foreign6.VW Rabbit4697251930 3.78foreign7.Datsun8108129.2750 3.55foreignThe separator()option draws a horizontal line at specified intervals.When not specified,it defaults to a value of5.[GSW]10Listing data and basic command syntax9.list,separator(3)make price mpg weight gear_r~o foreign1.Olds9888142140602.41domestic2.Chev.Monza3667.2750 2.73domestic3.Buick Regal5189203280 2.93domestic4.4099222930 3.58domestic5.Datsun5105079242280 3.54foreign6.VW Rabbit4697251930 3.78foreign7.Datsun8108129.2750 3.55foreignMoreWhen you see a more prompt at the bottom of the Results window,it means that there is more information to be displayed.This happens,for example,when you are list ing many observations..list make mpgmake mpg1.Linc.Continental122.Linc.Mark V123.Cad.Deville144.Cad.Eldorado145.Linc.Versailles146.Merc.Cougar147.Merc.XR-7148.Peugeot604149.Buick Electra1510.Merc.Marquis1511.Buick Riviera1612.Chev.Impala1613.Dodge Magnum1614.Olds Toronado1615.AMC Pacer1716.Audi50001717.Dodge St.Regis1718.Volvo2601719.Buick LeSabre1820.Dodge Diplomat18moreIf you want to see the next screen of text,you have a few options:press any key,such as the Spacebar;click on the More button,;or click on the blue more at the bottom of the Results window.To see just the next line of text,press Enter.10[GSW]10Listing data and basic command syntaxBreakIf you want to interrupt a Stata command,click on the Break button,.If you see a more prompt at the bottom of the Results window and wish to interrupt it,click on the Break button or press q..list make mpgmake mpg1.Linc.Continental122.Linc.Mark V123.Cad.Deville144.Cad.Eldorado145.Linc.Versailles146.Merc.Cougar147.Merc.XR-7148.Peugeot604149.Buick Electra1510.Merc.Marquis1511.Buick Riviera1612.Chev.Impala1613.Dodge Magnum1614.Olds Toronado1615.AMC Pacer1716.Audi50001717.Dodge St.Regis1718.Volvo2601719.Buick LeSabre1820.Dodge Diplomat18breakr(1);It is always safe to click on the Break button.After you click on Break,the state of the system is the same as if you had never issued the original command.。

stata命令总结

stata命令总结

stata命令总结.docStata命令总结引言Stata是一款强大的统计分析软件,广泛应用于经济学、社会学、医学等领域。

Stata命令是进行数据处理、统计分析、图形展示等操作的基础。

本文将对Stata中常用的命令进行总结,以帮助用户更高效地使用Stata进行数据分析。

Stata基础命令1. 数据管理导入数据:import excel, import delimited导出数据:export excel, export delimited数据集保存:save, saveold2. 变量管理创建变量:generate, egen修改变量:replace删除变量:drop3. 数据清洗数据类型转换:destring, encode, format缺失值处理:mvdecode, drop if missing()异常值检测:tabulate, summarize描述性统计分析1. 基本统计量描述性统计:summarize频率统计:tabulate相关系数:correlate2. 分组统计分组描述:bysort, xtsum 分组汇总:collapse3. 数据转换数据长格式:reshape long 数据宽格式:reshape wide 推断性统计分析1. 假设检验t检验:ttest方差分析:anova卡方检验:tabulate, chi2 2. 回归分析线性回归:regress逻辑回归:logit泊松回归:poisson3. 时间序列分析时间序列描述:tsreport自回归模型:arima高级统计分析1. 面板数据分析面板数据描述:xtset, xtsum固定效应模型:xtreg fe随机效应模型:xtreg re2. 多层次模型多层次线性模型:xtmelogit3. 结构方程模型结构方程模型:sem绘图与可视化1. 基本图形散点图:scatter线图:line柱状图:bar2. 高级图形箱线图:boxplot直方图:histogram核密度估计图:kdensity3. 交互式图形交互式图形:twoway, graph edit编程与自动化1. 循环与条件语句循环:foreach, forvalues条件语句:if, else2. 脚本与批处理脚本编写:do-file批处理:batch3. 宏与用户定义命令宏:macro用户定义命令:program define结语Stata命令的掌握是进行高效数据分析的前提。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

Stata 命令语句格式:[by varlist:] command [varlist] [=exp] [if exp] [in range] [weight] [, options]1、[by varlist:]*如果需要分别知道国产车和进口车的价格和重量,可以采用分类操作来求得,sort foreign //按国产车和进口车排序. by foreign: sum price weight*更简略的方式是把两个命令用一个组合命令来写。

. by foreign, sort: sum price weight如果不想从小到大排序,而是从大到小排序,其命令为gsort。

. sort - price //按价格从高到低排序. sort foreign -price /*先把国产车都排在前,进口车排在后面,然后在国产车内再按价格从大小到排序,在进口车内部,也按从大到小排序*/2、[=exp]赋值运算. gen nprice=price+10 //生成新变量nprice,其值为price+10/*上面的命令generate(略写为gen) 生成一个新的变量,新变量的变量名为nprice,新的价格在原价格的基础上均增加了10 元。

. replace nprice=nprice-10 /*命令replace 则直接改变原变量的赋值,nprice 调减后与price 变量取值相等*/3、[if exp]条件表达式. list make price if foreign==0*只查看价格超过1 万元的进口车(同时满足两个条件),则. list make price if foreign==1 & price>10000*查看价格超过1 万元或者进口车(两个条件任满足一个). list make price if foreign==1 | price>100004、[in range]范围筛选sum price in 1/5注意“1/5”中,斜杠不是除号,而是从1 到 5 的意思,即1,2,3,4,5。

如果要计算前10 台车中的国产车的平均价格,则可将范围和条件筛选联合使用。

. sum price in 1/10 if foreign==05、[weight] 加权sum score [weight=num] 其中,num为每个成绩所对应的人数6、[, options]其他可选项例如,我们不仅要计算平均成绩,还想知道成绩的中值,方差,偏度和峰度等*/. sum score, detail. sum score, d //d 为detail 的略写,两个命令完全等价. list price, nohead //不要表头Stata 数据类型转换1、字符型转化成数值型destring, replace //全部转换为数值型,replace 表示将原来的变量(值)更新destring date, replace ignore(“ ”) 将字符型数据转换为数值型数据:去掉字符间的空格destring price percent, gen(price2 percent2) ignore(“$ ,%”) 与date 变量类似,变量price 前面有美元符号,变量percent 后有百分号,换为数值型时需要忽略这些非数值型字符2、数值型转化为字符型tostring year day, replace //将年和日转化为字符型gen date1=month+”/”+day+”/”+year //month day变为字符型后可以运算,将年月日构成一个新的日期变量gen date2=date(date1,”mdy”) /* date()为日期函数,它以1960 年1 月1日为第0 天,计算从那天起直到括号中指定的某天date1一共过了多少天。

”mdy”指定date1 的排列顺序,这里是按照月日年的顺序来表示日期。

*/数据显示格式/*format 只控制数据的显示格式,并不改变内存中数据的大小。

*/变量的格式为%14s,表示右对齐,共14 个字符,%为固定用法(字符变量跟s,数值变量跟g)ormat state %-14s // 该命令使stata 的显示格式左对齐,14 前面多了个负号format pop %11.0gc /*pop 的显示格式为%11.0g,后面加上c,则每三位数间用逗号分开,c 为comma 的意思.*/format medage %8.1f //要求所有的medage 都显示一位小数format id %05.0f //对于编号,我们希望前面用零使得位数对齐,通过在前面补零,所有的id 都成了5位数。

导入/导出其他格式数据1、数据导入insheet using 3origin.csv/txt, clearinsheet using 3origin.txt, double clear 当数据中某个变量的位数特别长或者对导入数据的精度要求很高的时候,需要在该命令后面加double 选项。

2、数据导出outsheet using myresult.asc, nonames 如果不希望在第一行存储变量名,则可以使用nonames 选项outsheet using myresult.asc, nonames replace 如果文件已经存在,则需要使用replace 选项数据合并1、纵向合并use male, clear //打开记录男生信息的数据文件maleappend using female //将记录女生信息的female 文件追加到当前数据集中save mydata1, replace2、横向合并use economy,clear //打开经济学成绩数据文件sort id //按学号排序save economy, replace //重新保存一下use student,c clear //打开学生基本信息数据文件sort id //按学号排序merge id using economy //以学号为关联,将学生的信息和成绩一一对应对接tab _merge //显示对接情况,3 表示成功对接,1 和2 表示未成功对接drop _merge //去掉标识对接是否成功变量_mergeStata很多命令可单独使用,单独使用时,一般是对所有变量进行操作,等价于后面加上代表所有变量的_all数据重整1、长宽转换宽:长:1)宽变长use mywide, clearreshape long math economy, i(id name) j(year) //数据重整,宽变长save mylong, replace2)长变宽reshape wide*或者use mylong, clearreshape wide math economy, i(id name) j(yearr) //数据重整,长变宽save mywide2, replace2、多列数据转为少数几列有些数据集虽然有很多列,但实际上只有一个变量,利用stata转化成一项数据。

stack var1-var6, into(x) clear x是新生成变量的名称drop _stack 变量stack 记录观测值原来所在行数3、数据转置use math,clearxpose, clear变量运算:Stata中,加(+)号同样可用于字符运算,当加号出现在两个字符之间时,两个字符将被连成一个字符。

比如把”我爱” “STA TA”合并在一起,命令为:. scalar a=”我爱” +“STATA”一些运算函数:comb(n,k) 从n 中取k 个的组合fill() 自动填充数据int(x) 取整log10(x) 以10 为底的对数mod(x,y) 求余数round(x) 四舍五入di round(3.345,.1) //四舍五入到十分位,结果为 3.3di round(3.345,.01) //四舍五入到百分位,结果为 3.35di round(335.1,10) //四舍五入到十位,结果为 340sqrt(x) 开更号substr(s,n1,n2) 从S 的第n1 个字符开始,截取n2 个字符word(s,n) 返回s 的第n 个字符_n 当前观察值的序号_N 共有多少观察值gen y=sum(x) //求列累积和egen z=sum(x) //求列总和egen avgx=mean(x) //求列均值egen byte dxy = diff(x y) //当x与y相等时,differ取0,若不相等为1分离变量值clearinput str15 x"10*123""543*21""12*422""43532*32134""4349*1"endgen a=strpos(x,"*") //计算出*所在的位数gen b=substr(x,1,a-1) //取*前面的字符gen c=substr(x,a+1,.) //取*后面的字符stata中,系统缺失值大于任何一个数据,因此在生成分类哑变量时:gen agegrp2=(age>=65) if age<.生成的数据中,将缺失值排除在外生成分组变量:clearset obs 100 //设定100 个观察值gen age=_n //生成一个假设的年龄变量age,依次取1,2,…,100recode age (min/30=1) (30/60=2) (60/max=3),gen(agegrp) /*生成新的分组变量agegrp, 当年龄age在30及以下时取值为1,30到60为2,60以上为3*/分组运算:by x, sort: gen n1=_n 根据x的不同,生成n1变量对不同类的x计数by hhid,sort: egen mage=mean(age) //根据不同类别求平均年龄bysort hhid (age): gen nid1=_n //括号中的变量age 只排序,不参于分组。

bysort hhid age: gen nid2=_n // hhid 和ag e 都既用来参与排序也分组encode country, gen(country1) 将文本变量转化为数值变量di splay5+9 显示计算结果sum price weight 描述统计:求价格和重量的观察值个数、平均值、标准差、最小值和最大值scatter price weight 绘出价格和重量的散点图line price weight, sort 绘出价格和重量的折线图clear 清除内存中原有内容cd d:/stata9 在打开数据之前,先定位数据的位置use 打开STATA 格式的数据文件set obs 5 //设定5 个观察值dir 查看当前路径下有哪些文件save mydata //保存数据,数据文件名为mydatasave mydata, replace 如果同一文件夹下已经存有mydata.dta,而你又要再次执行save mydata 时edit 编辑数据log 将输出结果存放入结果文件gen id=_n //生成一个新变量id,根据观测值排列顺序从上到下取值依次为123……replace id=9842 in 3 第三个观测的id值改变compress //压缩数据,使之在不损失任何信息的前提下占用空间最小erase mydata1.dta 删除文件,一定要带上后缀名。

相关文档
最新文档