zhao1198的个人博客分享 http://blog.sciencenet.cn/u/zhao1198

博文

STATA V.4 IN THE SUN: BASICS

已有 4776 次阅读 2009-8-26 13:33 |个人分类:Stata|系统分类:科研笔记| STATA, Basics

STATA V.4 IN THE SUN: BASICS

http://www.nber.org/~veronica/stata.html
Last updated: 6/21/95

[按] 虽然版本较低(部分命令失效),但是有一些使用方法和STATA习惯值得推荐。

另外,此人的homepage还提供EViews等软件的入门级教程。


I. Interactive Mode

Before starting stata, type:

/opt/X11R6/bin/xterm &

This command will enable you to get graphs from stata. A black window will open.

Start stata in this window by typing: stata

You will then see the Stata logo and some messages. After pressing <return> once, you can start entering your stata commands. To save the output of the interactive session type:

log using filename

as the first line of your program.

Before asking for any graph, enter the following command:

window define gr

This will bring up a new smaller window on the top right corner of the screen. Every time you enter a graph command, the graph will appear in that window.

To quit stata, type: exit

If you get the message: "no; data in memory would be lost" and you don't want to save any of it, type: exit, clear

If you want to save the data, use the command: save filename before exiting.

II. Batch Mode

To run a stata program the steps are:

1) Use any editor or word processor to write your stata program. The filename extension should be .do.

Example: use crisp to edit the program myprogram.do: cr myprogram.do

2) Run your program with the command:

stata -b do myprogram

You don't need to type the extension. Stata will run in the background, and will put the output in the file myprogram.log

If you have long lines in your program or if you need to use more than one line for some commands, Stata will interpret carriage returns as the end of the line (end of the command). To make Stata read more than one line for a command you have 2 options:

1) Use "#delimit ;"

This will let you change the line delimiter to semicolon. If you use this, all lines, even short ones, must end in semicolons.

To change the delimiter back to carriage return, type:

"#delimit cr"/

2) Use /* */ to comment out carriage returns.

This means you will put comment brackets right before the carriage return and at the beginning of the next line.

Data files in Stata format

(Replace filename with the name of your file)

To read a data file in stata format, use the command:

use filename.dta

You do not need to list the variables in the data set.

To get a listing of the variables in the stata data file, use the command:

describe using filename.dta

This command can be used even before you read the data file with use filename.dta.

To get descriptions in terms of memory requirements of your data set, use:

bmemsize using filename.dta

This will give you the basic information you need to distribute the maximum memory available to Stata between observations and variables.

Use the information from bmemsize to write the next command to allocate memory between variables and observations:

set maxvar number width number maxobs number

where the numbers to put after maxvar, width and maxobs are part of the output from the bmemsize command.

Regressions and Graphs

Following is a simple stata program that:

Defines some new variables (lag of prices, average price, time trend).

Draws a histogram and a scatter plot and save them to be printed later.

Estimates basic univariate summary statistics for selected variables.

Estimates some linear regressions.

File: mytest.do

*********************************

#delimit ;

infile year foodcons prretail dispinc foodprod

prfarm using fooddata.dat;

generate prlag=prfarm[_n-1] ;

graph dispinc, histogram normal t1("Distribution of Disposable Income")

saving(graph1,replace);

generate trend=_n ;

generate avgprice=((prfarm+prlag)/2) ;

summarize foodcons prretail dispinc

foodprod prfarm avgprice;

regress foodcons prretail dispinc trend ;

predict newfood ;

summarize newfood foodcons ;

graph newfood foodcons, t1("Observed and Fitted Values of Food Consumption")

saving(grfoodc,replace);

*********************************

After saving and closing the file mytest.do, I run the Stata program with the command:

stata -b do mytest

The output will be in the file mytest.log and the graphs will be in the files graph1.gph and grfoodc.gph. I can open the output file and look at it with cr mytest.log.

To print or look at the graph do the following:

1) Type the command:

gphpen -dps.pen graph1.gph (for portrait)

or

gphpen -dpsls.pen graph1.gph (for landscape)

This will generate a file called: graph1.ps

2) To see the file graph1.eps, use the command: gs graph1.ps

To print the file graph1.eps, use the command: lpr -Plw5 graph1.ps

************************************************************

Stata manuals are available from room 4.124. For questions regarding Stata in the Sun, send e-mail to:consultant@eco.utexas.edu - Veronica Ruiz de Castilla Brinson

The output file mytest.log will look like (you can open the output file with: cr mytest.log):

___ ____ ____ ____ ____ tm

/__ / ____/ / ____/

___/ / /___/ / /___/ 4.0 Copyright 1984-1995

Statistics/Data Analysis Stata Corporation

702 University Drive East

College Station, Texas 77840 USA

409-696-4600 409-696-4601 (fax)

800-STATA-PC stata@stata.com

2-user Sun (network) perpetual license:

Serial number: SI-54041251

Licensed to: University of Texas Economics Department

Austin

Notes:

1. (-k### option) 1024K bytes allocated to data

2. Command line editing disabled

3. Stata running in batch mode

Note: Your site can add messages to the introduction by editing the file

/usr/local/stata/stata.msg.

. do mytest

. #delimit ;

delimiter now ;

. infile year foodcons prretail dispinc foodprod

>

> prfarm using fooddata.dat;

(20 observations read)

. generate prlag=prfarm[_n-1] ;

(1 missing value generated)

. graph dispinc, histogram normal t1("Distribution of Disposable Income")

>

> saving(graph1,replace);

. generate trend=_n ;

. generate avgprice=((prfarm+prlag)/2) ;

(1 missing value generated)

. summarize foodcons prretail dispinc

>

> foodprod prfarm avgprice;

Variable | Obs Mean Std. Dev. Min Max

---------+-----------------------------------------------------

foodcons | 20 100.755 2.825909 96 107.6

prretail | 20 100.685 5.540403 88.6 108.7

dispinc | 20 97.535 11.83048 75.1 127.1

foodprod | 20 104.24 4.540161 94.3 111.3

prfarm | 20 97.095 12.90644 68.6 110.8

avgprice | 19 96.79211 12.11276 69.75 109.5

. regress foodcons prretail dispinc trend ;

Source | SS df MS Number of obs = 20

---------+------------------------------ F( 3, 16) = 39.55

Model | 133.698084 3 44.5660281 Prob > F = 0.0000

Residual | 18.0314314 16 1.12696446 R-squared = 0.8812

---------+------------------------------ Adj R-squared = 0.8589

Total | 151.729516 19 7.98576399 Root MSE = 1.0616

------------------------------------------------------------------------------

---------+--------------------------------------------------------------------

dispinc | .3152365 .0320669 9.831 0.000 .2472576 .3832154

trend | -.2244297 .0612175 -3.666 0.002 -.3542049 -.0946544

_cons | 105.0938 5.687198 18.479 0.000 93.03747 117.1501

------------------------------------------------------------------------------

. predict newfood ;

. summarize newfood foodcons ;

Variable | Obs Mean Std. Dev. Min Max

---------+-----------------------------------------------------

newfood | 20 100.755 2.652686 97.02164 108.068

foodcons | 20 100.755 2.825909 96 107.6

. graph newfood foodcons, t1("Observed and Fitted Values of Food Consumption")

>

> saving(grfoodc,replace);

. #delimit cr

delimiter now cr

..

end of do-file



https://wap.sciencenet.cn/blog-285749-251533.html

上一篇:Online Seminars and Classes
下一篇:Archive for the ‘Stata’ Category
收藏 IP: .*| 热度|

0

评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-19 12:23

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部