This is BASICS.prg if you open file when in RATS you will see a number of files of which this is one.
*********************************************************************
*** Set the CALENDAR and ALLOCATE range. CAL to start in November, 1959:
**********************************************************************
calendar 1959 1 12
allocate 1996:2
**********************************************************************
*** Read in the Data:
**********************************************************************
open data basics.xls
data(format=xls,org=columns) / rate m1 m2 ip ppi
* For BASICS.RAT, you would use "data(for=rats) / rate m1 m2 ip ppi"
* For BASICS.PRN, you would use "data(for=prn,org=columns) / rate m1 m2 ip ppi"
* For BASICS.DAT, you would use "data(for=free,org=columns) / rate m1 m2 ip ppi"
*
* Note that you could omit the "/" and series list for all but BASICS.DAT
*
***********************************************************************
*** Look at the data numerically:
**********************************************************************
table
table / ppi rate
table 1970:1 1980:12 ppi rate
statistics rate
print / m1 m2
* Print M1 data for 1960 only:
print 1960:1 1960:12 m1
* Print RATE rounded to one decimal:
print(picture="*.#") / rate
**********************************************************************
*** Data transformations and new series
**********************************************************************
* Need first difference of the produce price index.
* The following two instructions are equivalent:
set ppidiff = ppi - ppi{1}
diff ppi / ppidiff
* Now, need (M1(t) - M1(t-3)):
set m1diff = m1 - m1{3}
print / ppi ppidiff m1 m1diff
* And quarter-to-quarter and annual growth rates for M2 and PPI:
* M2(t) - M2(t-1) divided by M2(t-1):
set grm2 = (m2 - m2{1})/m2{1}
* PPI(t) - PPI(t-1) divided by PPI(t-1):
set grppi = (ppi - ppi{1})/ppi{1}
* IP(t) - IP(t-1) divided by PPI(t-1):
set grip = 100*(ip - ip{1})/ip{1}
* Annualized growth rates:
set anngrppi = 100*( (ppi/ppi{1})**12 -1.0)
set anngrip = 100*( (ip/ip{1})**12 -1.0)
tab
* Need a three period weighted moving average:
set pratio = ppidiff/ppi
set ppisum = pratio + pratio{1} + pratio{2}
* Or:
set ppisum = (ppidiff/ppi) + (ppidiff{1}/ppi{1}) + (ppidiff{2}/ppi{2})
* Or:
set pratio = ppidiff/ppi
filter pratio / ppisum
# 1 2
# 1.0 1.0
* Simple trend and squared-trend series, commonly used:
set trend = t
set trendsq = t**2
**********************************************************************
*** Graph the data:
**********************************************************************
* Look at the rate and index series together:
graph(key=upleft) 3
# rate
# ip
# ppi
* Graph the money supply series
graph(key=below) 2
# m1
# m2
* Again, now using some labelling options:
graph(key=upleft,header='Real Money Supply (M1 and M2)',subhead='Billions US$, Seasonally Adjusted') 2
# m1
# m2
* Limit the range to 1959:11 through 1980:12
graph(key=upleft,header='Real Money Supply (M1 and M2)',subhead='Billions US$, Seasonally Adjusted') 2
# m1 1959:11 1980:12
# m2 1959:11 1980:12
* Use SMPL to change default range:
smpl 1959:11 1980:12
graph(key=upleft) 3
# rate
# ip
# ppi
* Reset the SMPL back to full CALENDAR-ALLOCATE range:
smpl
* Some SCATTER plots:
scatter 1
# grppi rate
scatter(vlabel='Interest Rate',hlabel='PPI Growth Rate',header='Interest Rates vs. PPI Growth') 1
# grppi rate
compute keys = || '1959-1978', '1979-1983', '1984-1999'||
scatter(patterns,klabel=keys,key=below,vlabel='Interest Rate',hlabel='Monthly Growth Rate of PPI') 3
# grppi rate * 1978:12 1
# grppi rate 1979:1 1983:12 2
# grppi rate 1984:1 * 4
**********************************************************************
*** Fit some least squares regressions.
**********************************************************************
* Example 4.2 from 3rd (1991) Edition:
* Data are slightly different, so results do not match text exactly
linreg rate 1960:2 1980:12
# constant ip m1diff ppisum
* Example 4.2 from 4th (1998) edition:
linreg rate 1960:1 1995:8 resids_rate
# constant ip grm2 grppi{1}
**********************************************************************
*** Hypothesis testing:
**********************************************************************
linreg rate
# constant rate{1 to 6}
exclude
# rate{4 to 6}
* Same thing using TEST:
test
# 5 6 7
# 0.0 0.0 0.0
* Test that RATE{1}==1.0 and RATE{2}==0.0
test
# 2 3
# 1.0 0.0
* Test whether coefficient on GRM2 is equal to coeff. on GRPPI{1}:
* Because RESTRICT test linear combinations, to test
* H0: beta1 == beta2
* We write the test as:
* H0: beta1 - beta2 == 0.0
linreg rate 1960:1 1995:8
# constant ip grm2 grppi{1}
* Test 1 resriction: 1.0*(coefficient 3) - 1.0*(coefficient 4) == 0.0
restrict 1
# 3 4
# 1.0 -1.0 0.0
**********************************************************************
*** Forecasting:
**********************************************************************
* Using OLS model:
linreg rate 1960:1 1995:8 resids_rate
# constant ip grm2 grppi{1}
* Fitted values:
prj fitted
graph(key=below,header='Actual vs. Fitted') 2
# rate 1960:1 1995:8
# fitted 1960:1 1995:8
* Forecast staring in 1995:1 and extending beyond
* the estimation range, to 1996:2
prj forecast 1995:1 1996:2
graph(key=below,header='Actual vs. Fitted') 2
# rate 1994:7 1996:2
# forecast
* Forecasts (same results as PRJ for static models like this one)
linreg(define=irateeq) rate 1960:1 1995:8
# constant ip grm2 grppi{1}
* Using UFORECAST:
uforecast(equation=irateeq) olsfore 1995:1 1996:2
* Or using FORECAST:
forecast 1 14 1995:1
# irateeq olsfore
graph(header='Interest Rate and Forecast',key=below) 2
# rate 1994:7 1996:2
# olsfore
**********************************************************************
*** Scalar and matrix examples:
**********************************************************************
compute a = 1.0
display a
make xmat 1960:1 1995:8
# constant ip grm2 grppi{1}
make ymat 1960:1 1995:8
# rate
compute invXX = inv(tr(xmat)*(xmat))
compute XY = tr(xmat)*ymat
compute beta = invXX*XY
display beta
open data states.wks
allocate 50
data(org=col,format=wks) / expend pcaid pop pcinc
set pcexp = expend/pop
set small = pop<5000
set large = pop>=5000
*
linreg(smpl=small) pcexp
# constant pcaid pcinc
compute rsssmall=%rss , ndfsmall=%ndf
*
linreg(smpl=large) pcexp
# constant pcaid pcinc
compute rsslarge=%rss , ndflarge=%ndf
*
* Full sample regression
*
linreg pcexp
# constant pcaid pcinc
compute rsspool=%rss
*
compute rssunr=rsssmall+rsslarge , ndfunr=ndfsmall+ndflarge
compute fstat = ( (rsspool-rssunr)/3 ) / (rssunr/ndfunr)
cdf ftest fstat 3 ndfunr
cal 1935
allocate 1954:1
open data grunfeld.wks
data(org=obs,format=wks) / ige fge cge iwest fwest cwest
*
equation geeq ige
# constant fge cge
equation westeq iwest
# constant fwest cwest
*
linreg(equation=geeq) ige
linreg(equation=westeq) iwest
*
sur(vcv) 2
# geeq
# westeq
*
sur 2 / equate 2 3
# geeq
# westeq
cal 1959 1 4
allocate 1999:2
open data drisampl.rat
data(format=rats) / gdpq fm1
log gdpq
log fm1
*
* Sims' test
*
set filtm1 = fm1-1.50*fm1{1}+.5625*fm1{2}
set filtgnp = gdpq-1.50*gdpq{1}+.5625*gdpq{2}
*
linreg filtgnp
# constant filtm1{-4 to 8}
exclude
# filtm1{-4 to -1}
*
* Geweke-Meese-Dent variation
*
linreg gdpq
# constant fm1{-4 to 8} gdpq{1 to 8}
exclude
# fm1{-4 to -1}
*
* Granger test
*
linreg fm1
# constant fm1{1 to 8} gdpq{1 to 8}
exclude
# gdpq{1 to 8}