文档库 最新最全的文档下载
当前位置:文档库 › STATA时间序列分析

STATA时间序列分析

STATA时间序列分析
STATA时间序列分析

1

ECONOMICS452

TIME SERIES WITH STATA

0.1Introduction

This manual is intended for the?rst half of the Economics452course and introduces some of the time series capabilities in Stata8.I will be writing programs and?xing others throughout the term so this is really just a manual to get started.Other details will follow.

Stata has an excellent online help facility and there are manuals available.The emphasis in this outline is on time series in Stata(note:earlier versions of Stata did not do time series analysis).If you have never used Stata before,it would be a good idea to get the tutorial manual for Economics351which goes over the basics for using Stata(http://www.econ.queensu.ca/faculty/abbott/econ351/) Keep in mind that the principal di?erence between cross-section and time series data is that order matters in the latter.We cannot shu?e the data as we did,say in a cross-section,using perhaps a sort command.The calendar time gives us the order of the variables and it makes no sense to mix this up.We often wish to create leads or lags of certain variables and Stata needs to know what variable in our set it should identify with the time(much more on this below).Once the time variable is identi?ed,Stata can construct leads or lags in a logical manner.

There are many sources for time series data(for example you probably have downloaded some from the CANSIM databank in one of your courses).

0.2Starting Stata

Double click on the Stata icon.The?rst screen you will see contains four Stata windows:

The Stata Command window,in which you type all Stata commands.

The Stata Results window,which displays the results of each Stata command as you enter it.

The Review window,which displays the past commands you have issued during the current Stata session.

The Variables window,which lists the variables in the currently-loaded data ?le.

?There are a number of programs(called ado?les)that I have written for Stata for the Econ452(time series)course.To ensure you have the latest of these type:Qpro?le and then check Econ452dialog box that appears.This will

2

—automatically download the most recent ado?les(creating directories

where they do not exist)onto the hardrive and make them accesible to

you

—put you in the appropriate directory for using my demo programs(re-

ferred to in the chapters and in theis manual)

?To get out of the QED session,you will need to type:BREAK(all capitals) 0.3Log File

?As you work in Stata,be sure to make a record of your session by creating a ‘log’?le

?In a Stata do?le(explained later)type:log using a:\log?le,replace text(the replace writes over any existing?le so if you do not want that change the name and the text command writes the?le in an ordinary and readable text?le)?Alternatively,open the log?le by clicking on the Log button which is the fourth from the top left corner(chose.log option and not.smcl).Name the ?le and change directory to a:\.(if using a diskette)

?You should always open a log?le before you type any commands in Stata, and close it at the end of your session..Always save?les to your diskette, usb datakey or the temporary directory on c:\temp which you can e-mail to yourself at the end of the session.

?Files left on public sites tend to disappear.

?At the end of your session type:log close or click on the same button to close the log?le.

0.4Help in Stata

There are three useful ways to get online help.

Suppose you want to use the generate command but forget its syntax.

1.You can type:help generate

2.Another neat way to?nd out stu?is to use the search command.For instance

I might want to know about all the di?erent appearances of“generate”,so we

could type:search generate.This will generate all kinds of information that you could follow up on.

3.In Stata9,there are drop down dialogue boxes for all the commands.

0.5.READING IN DATA3 0.5Reading in Data

There are several types of data sets that Stata can use:

1.Stata data sets usually identi?ed with a.dta su?x

2.ASCI data sets or?at?les(this is what most of the CANSIM and Journal of

Applied Econometric data sets are in)and usually has an extension.raw

3.Others kinds(like Excel spreadsheets)can be read in but typically it is easier

to paste data in this format directly into the Stata editor and save!

Eventually,you will want to convert any ASCI data?le to a Stata data?le,since this can be read in faster to Stata than?at?les and will save any labels or other information that we have created.We will show how to do this later in the tutorial. One other advantage of a Stata data?le is the you can simply click on the data?le and it will open directly into Stata.

Suppose you have just downloaded a Journal of Applied Econometrics dataset and want to read it into Stata.Since it is not a Stata data?le you will have to let Stata know how to read it.To keep things simple,let us assume that you have200 observations on a variable temperature(called temp)stored on your diskette with the name:bntemp.raw.This is an ASCI data?le.

To load the variable temp into Stata,we use the in?le command.

.infile temp using a:\bntemp

Stata will reply

(200observations read)

Notice that we left o?the extension.raw since this is assumed by Stata.If instead our?le was called bntemp.txt,we would write

.infile temp using a:\bntemp.txt

Now that we have loaded the data into Stata we might just start to analyze the data,create new variables and so on.Imagine that we have done this and created several new variables and we wish to save the entire dataset as a Stata data?le.This is easily done by typing

.save_all using a:\bntemp

4

This will create a Stata data?le(called bntemp.dta)on our a directory(we use lower case letters in this manual,since all commands in Stata must be lower case). Notice we left out the.dta extension(it is the default extension).

Whenever we want to use this dataset in the future,we can simply click on it with our mouse and it will open in Stata.If on another occasion we were to create new variables or add data,we can save the new dataset by writing over the old Stata dataset(we could create a new dataset by using another name too)by typing save_all using a:\bntemp,replace

Now part of the?rst project involves calculating descriptive statistics of your data set.This can be accomplished by using the summarize command.If you do not specify a particular variable,Stata will summarize the entire dataset.

Again we will assume that we have only one variable called temp.

.summarize

Variable|Obs Mean Std.Dev.Min Max

---------+-----------------------------------------------------temp|20033.1619.27429513.856.5

If you want more descriptive statistics(percentiles,kurtosis,skewness and so on) type

.summarize temp,detail

To have your data listed type list

.list

temp

1.2

2.9

2.1

3.8

3.31.4

.

.

.

198.44.8

199.46.7

200.49.4

0.6Creating a time variable

Often when we download data,there will be a variable that identi?es the time frequency of the variable.Typically this variable would not be in the appropriate

0.6.CREATING A TIME VARIABLE5 time format for Stata and will be of no use.Instead,we must construct the time variable.To do this,we need to know what the date is for the?rst observation and the frequency of the data(daily,weekly,quarterly or annual).

Suppose we know that the?rst observation is for the?rst quarter of1990,the second for the second quarter,and so on,one should type:

generate time=q(1990q1)+n-1

format time%tq

tsset time

The command tsset time,tells Stata that the variable time is to be identi?ed as the variable giving the calendar time,all leads and lags are then based on the ordering from this variable.For all of the time series commands,you will need to declare a time variable through the command tsset.

For yearly data starting at1842type:

generate time=y(1842)+n-1

format time%ty

tsset time

For half yearly data starting at1921h2type:

generate time=h(1921h2)+n-1

format time%th

tsset time

For monthly data starting at2004m7type:

generate time=m(2004m7)+n-1

format time%tm

tsset time

For weekly data starting at1994w1type:

generate time=m(1994w1)+n-1

format time%tw

tsset time

For daily data starting at1jan1999type:

.generate time=d(1jan1999)+_n-1

.format time%td

.tsset time

time variable:time,01jan1999to19jul1999

.list

temp time

1.2

2.901jan1999

2.1

3.802jan1999

3.31.403jan1999

.

6

.

.

198.44.817jul1999

199.46.718jul1999

200.49.419jul1999

0.7Di?erencing,Lags and Leads

Once you have speci?ed a time variable using the ttset command,there are various Stata commands https://www.wendangku.net/doc/753813965.html,eful operators include the Di?erencing,Lags and Leads operators.

?L.temp=temp

?L2.temp=temp t-2

?F.temp=temp t+1

?D.temp=temp t-temp t-1

?D2.temp=temp-temp t-1-(temp t-1-temp t-2)=temp t-2temp t-1 +temp

?S.temp=temp t-temp t-1

?S2.temp=temp t-temp

?S12.temp=temp-temp t-12

?ld.temp=?temp t?1

0.8Graphing

With time series data,it is always a good idea to graph your data to get a feel for the data and how the data?uctuated over time.For instance,does the data cycle (interest rate data)or does it simply trend(like real GNP or population).Most time series papers that you will encounter have graphs.ALl graph commands have been changed in Stata8.(To use the old commands in7just put gr7and proceed with the commands)

To graph the time series we shall use the graph command.To graph1-month Treasury Bill Rate against time with the title“1Month Treasury Bill Rates”and saving this to the?le

0.9.CORRELOGRAM AND PARTIAL AUTOCORRELATION7

.twoway line r1time,title(”1Month Treasury Bill Rates”)legend(label(1”1-month TB Rate”)

graph save int.gph

Suppose we use short rates as predictors of long rates.we could graph the actuals vs.the predicted with the following commands(notice the semi colon for end of line)

regress r11r1;

predict?t;

twoway(line r11time)(line?t time),title(”Short-Rate as Predictor of Long”) legend(label(1”10-year TB Rate”)label(2”Predictions”));

To print a graph:

1.For a graph in memory just type:graph print

2.Display the graph.Either

(a)Draw it using the graph command,or

(b)Redisplay your last graph by pressing the Graph button,or

(c)Retrieve a previously saved graph by typing graph using dtemp

3.Pull down File and choose Print Graph

After you have graphed something you can save it as a Stata graph?le or as a bitmap?le,which can then be put into most word processors(Word,WordPerfect and so on).You can also pull down Prefs and choose Graph Preferences.This will allow you to set options specifying how the graph is printed.

0.9Correlogram and Partial Autocorrelation

The corrgram command lists a table of autocorrelations,partial autocorrelations, and Q statistics.It will also list a character-based plot of the autocorrelations and partial autocorrelations.

.corrgram D.temp,lags(20)

LAG AC PAC Q Prob>Q

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

1-0.1178-0.1180 2.80380.0940

2-0.1706-0.18318.71630.0128

3-0.0497-0.09399.21960.0265

8

4-0.0795-0.142910.5150.0326

5-0.0258-0.091010.6520.0587

6-0.0375-0.108110.9440.0901

7-0.0161-0.092910.9980.1387

8-0.0037-0.083511.0010.2017

90.0477-0.009211.480.2442

100.0310-0.012811.6840.3067

110.02290.011211.7960.3792

120.02190.032711.8980.4539

13-0.00490.020111.9030.5356

14-0.0392-0.012412.2350.5874

15-0.1196-0.125915.3440.4269

16-0.0301-0.092515.5420.4853

170.0473-0.030916.0340.5214

180.0445-0.008716.4710.5597

190.06570.036817.4290.5608

20-0.0660-0.074218.4020.5609

The ac command produces a correlogram(the autocorrelations)with pointwise con?dence intervals obtained from the Q statistic.

.ac D.temp,lags(20)

We can emphasize the discrete nature of the autocorrelations using the needle option.

.ac D.temp,lags(20)needle

The pac command produces a graph of the partial correlogram(the partial

autocorrelations)with con?dence intervals calculated using a standard error of1√

n .

The residual variances for each lag are also included on the graph.

.pac D.temp,lags(20)needle

The command wntesq calculates the portmanteau Q test for randomness(see Chapter6).

wntesq D.temp,lags(20)

0.10Arima Estimation

The basic syntax for an ARIMA(p,d,q)model is:

arima depvar,ar(1/#p)ma(1/#q)bfgs

or

arima depvar,arima(#p,#d,#q)bfgs

0.10.ARIMA ESTIMATION9

For the?rst command if you want to have di?erenced data(say4y)you would have to enter it via the D.y.To run a pure AR(p)or MA(q),you need to use the commands:

arima depvar,ar(1/#

p

)bfgs

arima depvar,ma(1/#q)bfgs

or

arima depvar,arima(p,d,0)bfgs

arima depvar,arima(0,d,q)bfgs

The ar(1/#

p )says to use lags1through p.If we just wrote ar(#

p

),Stata would

estimate

y t=α+φp y t?p+εt

omiting the lags1...p?1.

I have also included the option for bfgs optimizer(see Chapter4),since I have found it to work the best.The switching algorithm(which is the default)seems to get stuck and often produces rubbish.Estimation can take quite a while,so be judicious in the number of these you consider at one time.

0.10.1A word on the Intercept in ST AT A

?Stata scales the intercept(this allows a wider class of ARMA models the so-called ARMAX models that include other regressors).

?For example,in an ARMA(p,0,q)model

y t=α+φ1y t?1+φ2y t?2+...+φp y t?p+ t+θ1 t?1+...+θq t?q

Stata would estimate theφ0s andθ0s as above but the constant estimate would be scaled asα

(1?φ1?...?φp)

By the way,this scaled estimate is also a consistent estimate of the unconditional mean(why?).

To estimate an arima(0,1,1)model(with a constant)on our temperature data we type:

.arima D.temp,ma(1)bfgs

ARIMA regression

The estimates would follow and for brevity we omit them.

10

To estimate an arima(0,1,2)model on our temperature data we type:

.arima D.temp,ma(1/2)

We again omit the results except to note the value of the loglikelihood:

Log likelihood=-569.2156Prob>chi2=0.0000 0.11ARIMA:Hypothesis Testing

Sometimes we want to test one model against another:a restricted model against an unrestricted model(see Chapter5).For example,the unrestricted model may be an ARMA(0,1,2)model and the restricted model may be an ARMA(0,1,1). Strictly speaking we are testing an hypothesis about the?rst di?erence of temper-ature(4temp t=temp t?temp t?1),since the level of the variable is nonstationary.

We may formally state this hypothesis test as

H o:ARMA(0,1,1)

H1:ARMA(0,1,2)

First we estimate the unrestricted model,and denote the maximized value of the log likelihood as logL(?).Next we estimate the restricted model and obtain the restricted log likelihood as logL(ω).Then asymptotically

?2(logL(ω)?logL(?))~χ2m

under the null hypothesis that the restricted model is correct.m is the number of restrictions.In our example m=1as we have the single restriction that the coe?cient on the MA(2)term is zero.The values for the log likelihoods can be found in the output of the arima estimation.

Let the value of the statistic be LR(using the above likelihoods).Then to calculate the p-value type:

display chiprob(m,LR)

In our example we would type:

.display chiprob(1,(-2*(-576.1117--569.2156)))

The result would be

.00020418

Thus we would reject(for any reasonable level of signi?cance),the restricted model of ARMA(0,1,1)for our di?erenced data,in favour of the ARMA(0,1,2) model.Of course,a quick glance at the t?statistic(a Wald test-see Chapter6) ARMA(0,1,2)on the second MA term clearly rejects the null hypothesis that this coe?cient is0.

0.12.ARIMA:PREDICTION11 0.12ARIMA:Prediction

Generating predictions for a time series is often very important.To do this in Stata, one must?rst extend the time variable into the forecast region.

0.12.1Use fore.ado(see Chapter6)

In the lecture notes(see Chapter6:Programing Tips there is a discussion on using fore.ado)which automaticllay extends the interval

0.12.2Using the Editor to Extend the Sample

Another way to do this is to edit your time variable using the Stata editor(located right by the browse button).

Go down to the bottom of your time variable and note at the top what value STATA is using for your date(although your dates show up as dates to you,keep in mind Stata assigns its own consecutive number for each date).You can then add as many dates(using the consecutive numbers that Stata assigns)as you want for forecasting.Check to see as you type in the consecutive numbers that they are correctly translated into consecutive dates.As you add new dates,all of the variables in your dataset will get a‘.’,which is a missing observation for Stata.

In our case we will forecast7days so we modify the?le to look like this at the end.

44.8

46.7

49.4

.

.

.

.

.

.

.

When you quit the editor,Stata will ask you whether to preserve the changes: answer yes(answering no will leave you with the old data set).Now that you have changed the data set,you will want to save it(either use a new name or use the replace command).For instance,we could save this?le under another name,say bntempP.raw.

Now we re-estimate our chosen model but specify the dates that we want to use (since we can’t use observations that are blank)

.arima temp,arima(0,1,2)bfgs,if tin(,19jul1999)

12

Notice the handy interval command tin(d1,d2)where d1gives the start date and d2gives the end date.If one of the?elds is left blank(as the above d1)then the ?rst observation is presumed).

Now we are ready to use the predict command.However,?rstly we need to know the date of the?rst future date we want to forecast for.If we type browse we can scroll down our time series to?nd this date.In our example it is20jul1999.

The basic syntax for predict is:

predict newvarname,y dynamic(timeconstant)

where newvarname is the name of the new variable we are creating that holds the predicted values of temperature.y is to specify that we want the predicted values in levels rather than di?erences.dynamic is to specify that we will be gen-erating forecasts from after the date timeconstant using past forecasts.The date timeconstant is always the?rst date into the future.i.e.20jul1999in our example. The command predict will use the estimates of the last estimated model,which is the ARIMA(0,1,2).Thus we type:

.predict ptemp,y dynamic(d(20jul1999))

(8missing values generated)

(1missing value generated)

.list

temp time ptemp

1.2

2.901jan1999.

2.1

3.802jan199923.01461

3.31.403jan199915.16839

4.30.104jan199931.21309

.

.

.

196.40.915jul199946.59648

197.41.116jul199944.80204

198.44.817jul199943.92847

199.46.718jul199945.91249

200.49.419jul199946.34773

201..20jul199948.54581

202..21jul199947.66978

203..22jul199947.78439

204..23jul199947.899

205..24jul199948.01361

206..25jul199948.12821

207..26jul199948.24282

To graph our predictions we type:

.gr ptemp temp time,

c(ll)title(’’Predicted Temperature’’)saving(pretemp)

0.13.DO FILES13 0.13Do Files

The discussion in this manual has presumed that you are working interactively on Stata.That is,you type a command and Stata executes it and then you issue another command.Often you might want to run a single program which is contained a?le that has a number of Stata commands.This is called a batch?le which Stata has called a do?le.

Suppose that you have a?le called a:\temp.do which contained the following: #delimit;

set more off;

capture drop_all;

infile temp using a:\bntemp;

log using a:\bntemp,replace;

generate time=d(1jan1999)+_n-1;

format time%td;

tsset time;

corrgram D.temp,lags(20);

arima temp,arima(0,1,2)tin(,19jul1999);

predict ptemp,y dynamic(d(20jul1999));

gr ptemp temp time,

c(ll)title(’’Predicted Temperature’’)saving(a:\pretemp);

log close;

save_all using a:\bntemp,replace;

This program contains most of the commands that we did in this manual in a single(executable)?le.To run this program in Stata,type in the command line do a:\temp.do

You can read more about the do command in the Stata manual.

0.14ADO Files

I have written a bunch of.ado?les that are discussed in your notes at various stages (see Programming Tips at the end of the chapters).These are programs that for all intents and purposes are just like built-in STATA functions.I have created help ?les(.hlp)that can give you information on how to use the program.

We have these kinds of?les that automatically downloaded to you upon clicking into STATA and telling us what course you are in(see above).We will discuss this more in class.

0.15Help

For more information Stata has a Help menu.It is near the top of the screen.

14

0.16Exit

To Exit Stata,?rst close the log?le,then click on File and then Exit

统计学第四版贾俊平人大_回归与时间序列stata

回归分析与时间序列 一、一元线性回归 11.1 (1)编辑数据集,命名为linehuigui1.dat 输入命令scatter cost product,xlabel(#10, grid) ylabel(#10, grid),得到如下散点图,可以看到,产量和生产费用是正线性相关的关系。 (2)输入命令reg cost product,得到如下图: 可得线性函数(product为自变量,cost为因变量):y=0.4206832x+124.15,即β0=124.15,β1=0.4206832 (3)对相关系数的显著性进行检验,可输入命令pwcorr cost product, sig star(.05) print(.05),得到下图:

可见,在α=0.05的显著性水平下,P=0.0000<α=0.05,故拒绝原假设,即产量和生产费用之间存在显著的正相关性。 11.2 (1)编辑数据集,命名为linehuigui2.dat 输入命令scatter fenshu time,xlabel(#4, grid) ylabel(#4, grid),得到如下散点图,可以看到,分数和复习时间是正线性相关的关系。 2)输入命令cor fenshu time计算相关系数,得下图: 可见,r=0.8621,可见分数和复习时间之间存在高度的正相关性。 11.3 (1)(2)对于线性回归方程y=10-0.5x,其中β0=10,表示回归直线的截距为10;β1=-0.5,表示x变化一单位引起y的变化为-0.5。 (3)x=6时,E(y)=10-0.5*6=7。 11.4 (1) ,判定系数 测度了回归直线对观测数据的拟合程度,即在分数的变差中,有90%可以由分数与复习时间之间的线性关系解释,或者说,在分数取值的变动中,

运用stata进行时间序列分析

运用stata进行时间序列分析 1 时间序列模型结构模型虽然有助于人们理解变量之间的影响关系,但模型的预测精度比较低。在一些大规模的联立方程中,情况更是如此。而早期的单变量时间序列模型有较少的参数却可以得到非常精确的预测,因此随着Box and Jenkins(1984)等奠基性的研究,时间序列方法得到迅速发展。从单变量时间序列到多元时间序列模型,从平稳过程到非平稳过程,时间序列分析方法被广泛应用于经济、气象和过程控制等领域。本章将介绍如下时间序列分析方法,ARIMA模型、ARCH族模型、 VAR模型、VEC模型、单位根检验及协整检验等。 一、基本命令 1.1时间序列数据的处理 1)声明时间序列:tsset 命令 use gnp96.dta, clear list in 1/20 gen Lgnp = L.gnp tsset date list in 1/20 gen Lgnp = L.gnp 2)检查是否有断点:tsreport, report use gnp96.dta, clear tsset date tsreport, report drop in 10/10 list in 1/12 tsreport, report tsreport, report list /*列出存在断点的样本信息*/ 3)填充缺漏值:tsfill tsfill tsreport, report list list in 1/12 4)追加样本:tsappend use gnp96.dta, clear tsset date list in -10/-1 sum tsappend , add(5) /*追加5个观察值*/ list in -10/-1 sum 2 5)应用:样本外预测: predict reg gnp96 L.gnp96 predict gnp_hat list in -10/-1 6)清除时间标识: tsset, clear tsset, clear 1.2变量的生成与处理 1)滞后项、超前项和差分项 help tsvarlist use gnp96.dta, clear tsset date gen Lgnp = L.gnp96 /*一阶滞后*/ gen L2gnp = L2.gnp96 gen Fgnp = F.gnp96 /*一阶超前*/ gen F2gnp = F2.gnp96 gen Dgnp = D.gnp96 /*一阶差分*/ gen D2gnp = D2.gnp96 list in 1/10 list in -10/-1 2)产生增长率变量: 对数差分 gen lngnp = ln(gnp96) gen growth = D.lngnp gen growth2 = (gnp96-L.gnp96)/L.gnp96 gen diff = growth - growth2 /*表明对数差分和变量的增长率差别很小*/ list date gnp96 lngnp growth* diff in 1/10 1.3日期的处理日期的格式 help tsfmt 基本时点:整数数值,如 -3, -2, -1, 0, 1, 2, 3 .... 1960年1月1日,取值为 0; 3 显示格式: 定义含义默认格式%td 日%tdDlCY %tw 周%twCY!ww %tm 月 %tmCY!mn %tq 季度 %tqCY!qq %th 半年 %thCY!hh %ty 年 %tyCY 1)使用tsset 命令指定显示格式 use B6_tsset.dta, clear tsset t, daily list use B6_tsset.dta, clear tsset t, weekly list 2)指定起始时点 cap drop month generate month = m(1990-1) + _n - 1 format month %tm list t month in 1/20 cap drop year gen year = y(1952) + _n - 1 format year %ty list t year in 1/20 3)自己设定不同的显示格式日期的显示格式 %d (%td) 定义如下: %[-][t]d<描述特定的显示格式> 具体项目释义: “<描述特定的显示格式>”中可包含如下字母或字符 c y m l n d j h q w _ . , : - / ' !c C Y M L N D J W 定义如下: c an d C 世纪值(个位数不附加/附加0)

时间序列模型stata 基本命令汇总..

时间序列模型 结构模型虽然有助于人们理解变量之间的影响关系,但模型的预测精度比较低。在一些大规模的联立方程中,情况更是如此。而早期的单变量时间序列模型有较少的参数却可以得到非常精确的预测,因此随着Box and Jenkins(1984)等奠基性的研究,时间序列方法得到迅速发展。从单变量时间序列到多元时间序列模型,从平稳过程到非平稳过程,时间序列分析方法被广泛应用于经济、气象和过程控制等领域。本章将介绍如下时间序列分析方法,ARIMA模型、ARCH族模型、VAR模型、VEC模型、单位根检验及协整检验等。 一、基本命令 1.1时间序列数据的处理 1)声明时间序列:tsset 命令 use gnp96.dta, clear list in 1/20 gen Lgnp = L.gnp tsset date list in 1/20 gen Lgnp = L.gnp 2)检查是否有断点:tsreport, report use gnp96.dta, clear tsset date tsreport, report drop in 10/10 list in 1/12 tsreport, report tsreport, report list /*列出存在断点的样本信息*/ 3)填充缺漏值:tsfill tsfill tsreport, report list list in 1/12 4)追加样本:tsappend use gnp96.dta, clear tsset date list in -10/-1 sum tsappend , add(5) /*追加5个观察值*/ list in -10/-1 sum

Stata时间序列笔记

文档结尾是FAQ和var建模的15点注意事项 【梳理概念】 向量自回归(VAR, Vector Auto regression)常用于预测相互联系的时间序列系统以及分析随机扰动对变量系统的动态影响。 VAR模型: VAR方法通过把系统中每一个内生变量,作为系统中所有内生变量的滞后值的函数来构造模型,从而回避了结构化模型的要求。 VAR模型对于相互联系的时间序列变量系统是有效的预测模型,同时,向疑自回归模型也被频繁地用于分析不同类型的随机误差项对系统变量的动态影响。如果变量之间不仅存在滞后影响,而不存在同期影响关系,则适合建立VAR模型,因为VAR模型实际上是把当期关系隐含到了随机扰动项之中。 协整: Engle和Granger (1987a)指岀两个或多个非平稳时间序列的线性组合可能是平稳的。假如这样一种平稳的或的线性组合存在,这些非平稳(有单位根)时间序列之间被认为是具有协整关系的。这种平稳的线性组合被称为协整方程且可被解释为变量之间的长期均衡关系。 * 第六讲时间序列分析 *一一目录—— ? d?简介 *6」时间序列数据的处理 d ■平稳时间序列模型 * 6.2 ARIMA 模型 * 6.3 VAR 模型 非平稳时间序列模型一近些年得到重视,发展很快 * 6.4非平稳时间序列简介 * 6.5单位根检验——检验非平稳 * 6.6协整分析一一非平稳序列的分析 黑-自回归条件异方差模型 * 6.7 GARCH模型一一金融序列不同时点上序列的差界 反映动态关系的时间数据顺序不可颠倒 cd d:\stata 10\ado\personal\Net_Course\B6_TimcS *时间序列数据的处理help time *声明时间序列:tsset命令 use gnp96.dta, clear list in 1/20

与时间序列相关的STATE命令及其统计量的解析

与时间序列相关的STATE命令及其统计量的解析与时间序列相关的STATA 命令及其统计量的解析残差U 序列相关: ①DW 统计量——针对一阶自相关的(高阶无效) STATA 命令: 1.先回归 2.直接输入dwstat 统计量如何看:查表②Q 统计量——针对高阶自相关correlogram-Q-statistics STATA 命令: 1.先回归reg 2.取出残差predict u,residual(不要忘记逗号) 3. wntestq u Q 统计量如何看:p 值越小(越接近0)Q 值越大——表示存在自相关具体自相关的阶数可以看自相关系数图和偏相关系数图:STATA 命令: 自相关系数图: ac u( 残差) 或者窗口操作在Graphics ——Time-series graphs ——correlogram(ac) 偏相关系数图: pac u 或者窗口操作在Graphics——Time-series graphs—— (pac) 自相关与偏相关系数以及Q 统计量同时表示出来的方法: corrgram u 或者是窗口操作在 Statistics——Time-series——Graphs—— Autocorrelations&Partial autocorrelations ③LM 统计量——针对高阶自相关 STATA 命令: 1.先回归reg 2.直接输入命令 estate bgodfrey,lags(n) 或者窗口操作在 Statistics——Postestimation(倒数第二个)——Reports and Statistics(倒数第二个) ——在里面选择 Breush-Godfrey LM(当然你在里面还可以找到方差膨胀因子还有DW 统计量等常规统计量) LM 统计量如何看:P 值越小(越接近 0)表示越显著(显著拒绝原假设),存在序列相关具体是几阶序列相关,你可以把滞后期写为几,当然默认是 1,(通常的方法是先看图,上面说的自相关和偏相关图以及Q 值,然后再利用LM 肯定)。 平稳时间序列存在自相关的问题的解决方案残差出现序列相关的补救措施: 一阶自相关最近简单的方法是用AR(1)模型补救,就是在加一个残差的滞后项即可。 高阶的自相关用AR(n)模型补救。 AR 模型的识别与最高阶数的确定: 可以通过自相关系数来获得一些有关 AR(p) 模型的信息,如低阶 AR(p) 模型系数符号的信息。但是,对于自回归过程AR(p),自相关系数并不能帮助我们确定 AR(p) 模型的阶数 p。所以,可以考虑使用偏自相关系数 k,k,以便更加全面的描述自相关过程AR(p)的统计特征。 且对于一个AR(p) 模型, k,k 的最高阶数为p,也即AR(p) 模型的偏自相关系数是 p 阶截尾的。因此,可以通过识别AR(p)模型的偏

与时间序列相关的STATA-命令及其统计量的解析

与时间序列相关的STATA 命令及其统计量的解析 残差U 序列相关: ①DW 统计量——针对一阶自相关的(高阶无效) STATA 命令: 1.先回归 2.直接输入dwstat 统计量如何看:查表 ②Q 统计量——针对高阶自相关correlogram-Q-statistics STATA 命令: 1.先回归reg 2.取出残差predict u,residual(不要忘记逗号) 3. wntestq u Q 统计量如何看:p 值越小(越接近0)Q 值越大——表示存在自相关 具体自相关的阶数可以看自相关系数图和偏相关系数图: STATA 命令: 自相关系数图: ac u( 残差) 或者窗口操作在 Graphics ——Time-series graphs —— correlogram(ac) 偏相关系数图: pac u 或者窗口操作在Graphics——Time-series graphs—— (pac) 自相关与偏相关系数以及Q 统计量同时表示出来的方法: corrgram u或者是窗口操作在 Statistics——Time-series——Graphs——Autocorrelations&Partial autocorrelations ③LM 统计量——针对高阶自相关 STATA 命令: 1.先回归reg 2.直接输入命令estate bgodfrey,lags(n) 或者窗口操作 在 Statistics——Postestimation(倒数第二个)——Reports and Statistics(倒数第二个) ——在里面选择 Breush-Godfrey LM(当然你在里面还可以找到方差膨胀因子还有DW 统计量等常规统计量) LM 统计量如何看: P 值越小(越接近 0)表示越显著(显著拒绝原假设),存在序列相关 具体是几阶序列相关,你可以把滞后期写为几,当然默认是 1,(通常的方法是先看图,上面说的自相关和偏相关图以及Q 值,然后再利用LM 肯定)。 平稳时间序列存在自相关的问题的解决方案 残差出现序列相关的补救措施: 1、一阶自相关 : 最近简单的方法是用AR(1)模型补救,就是在加一个残差的滞后项即可。 2、高阶的自相关: 用AR(n)模型补救。

时间序列分析STATA 第三课

第三课:ARMA/ARIMA建模及预测 核心问题:1,前提:同方差,因此,不用考虑不同时刻变量的离散性; 2,建立的是变量分布的均值模型, 也就是,随机变量分布的均值所在的位置 3,难点在于,时间序列数据建立模型并没有唯一性 以quarterly.dta的数据来说明。这个数据是美国的季度GDP数据,数据从1947年一季度开始,到2012年一季度结束。研究对象,GDP,存在通货膨胀问题。所以要用GDP平减指数(GDP Deflator)进行矫正,这里是以2005年的美元作为基准的。所以考察变量是GDP2005,即以2005年的美元作为基准的各季度的GDP 真实值。 一,一些基本符号: D L F 二,建立模型: 前期准备:观察时序图 相关命令:tsset, tsline/twoway

从图上可以看出,GDP2005值呈线性的向右上方倾斜, 第一种方法: 可以用确定性分析理的方法,使用研究变量对时间变量进行回归 即:regress lrgdp date (采用的是最小二乘估计) 然后对残差项进行White Noise 检验 观察此图:特征大值跟大值,小值跟小值,这说明Residual 中存在着自相关信息。肯定不是White Noise 因此,从这里看出,确定性的方法比较直观,简单,但效果不好,弥补:对残差序列进行回归,建立AR 模型,这就是所谓的 残差自回归模型。 第二种方法 BOX-JENKINS 方法,也就是通常所说的ARMA(p,d) /ARIMA(p,d,d)建模(采用的是最大似然估计)。 四大步骤:1、模型识别Identification :决定p 和q 2、模型估计Estimation :估计εσθφμ 个),(,个)(, q p 3、模型检验Diagnostic Checking 4、模型优化Parsimony

相关文档
相关文档 最新文档