Tutorial5 Data Functions
Tutorial5 Data Functions
Note: Information for examples in this tutorial can be found in these files.
Data: Data.xlsx
Results: Results.wf1
Practice Workfile: Data.wf1
Data and Workfile Documentation
• Data.wf1 and Data.xlsx have the following data:
2
Data Functions
• EViews has a large number of built-in functions that allow you to perform
data manipulation.
3
Generating Random Numbers
Generating Random Numbers (Series):
Example 1
• You can generate a series of (pseudo) random numbers drawn from a variety
of distributions.
• There are a number of ways to generate a random series.
Generating a random series: Example 1
1. Open EViews workfile Data.wf1.
2. Select Quick → Generate Series from the main menu.
3. Type y1 = nrnd in the dialog box and press Enter.
5
Generating Random Numbers (Series):
Example 1 (cont’d)
• The result is shown here.
6
Generating Random Numbers (Series):
Example 2
• Alternatively, you can create a random series by typing in the command window.
7
Generating Random Numbers (Series):
Example 3
• Suppose you want to simulate a random walk process with distribution
properties similar to the observed distribution of an existing series (for
example, gdp):
Generating a random series: Example 3
1. Type in the command window:
smpl @all
series newdata = 0
smpl 1980q2 @last
newdata=newdata(-1)+@mean(d(gdp))
+@stdev(d(gdp))*nrnd
2. Press Enter after each command line.
9
Generating Random Numbers (Series):
Common Functions/Commands
• EViews has a number of functions/commands that allow you to draw from a
variety of distributions.
• The most commonly used functions/commands are summarized below.
Common Commands/Functions
Function/Command Description
series y=nrnd or (y=@rnorm) Normal distribution (mean 0, st. dev. 1)
series y=rndint(0,100) Fills series y with random integers drawn randomly from 0
to 100
10
Generating Random Numbers (Series):
PDF and CDF
• It is also very easy to generate the pdf and cdf of a random variable.
Function Description
series d1 = @runif(0,2) Creates d1 as uniform distribution on (0,2)
show d1 @dunif(d1,0,2) @cunif(d1,0,2) Displays series d1 created above, pdf of
d1 and cdf of d1
11
Generating Random Numbers (Series):
PDF and CDF (cont’d)
• The result is shown here.
• As you can see, EViews displays
all three columns: the actual new
random variable we created (d1),
its density function (in the second
column) and its cumulative
distribution function (third
column).
12
Functions for Descriptive Statistics
Descriptive Statistics
• EViews has extensive built-in descriptive statistical functions.
• These descriptive statistical functions take an optional sample as an argument.
• The default sample is the current workfile range.
14
Descriptive Statistics
Common Commands/Functions
Common Commands/Functions
Function Description
series y=@gmean(x) Computes the geometric average of X
series y=@mean(x) Creates a series where each observation is equal to the mean
of X
series y=@mean(x,“1980m01 Creates a series where each observation is equal to the mean
1990m12”) of X for the defined sample (1980m01 to 1990m12)
15
Descriptive Statistics Functions:
Example 1
• Suppose you want to create a series x1, which is equal to the mean of the
“gdp” series.
16
Descriptive Statistics Functions:
Example 2
• As a next example, consider creating another series x2, which is equal to the
mean of the “gdp” series, defined over several sub-samples:
Descriptive Statistics Functions: Example 2
1. Type in the command window:
smpl 1980q1 1989q4
series x2=@mean(gdp, "1980q1 1989q4")
smpl 1990q1 1999q4
series x2=@mean(gdp, "1990q1 1999q4")
smpl 2000q1 @last
series x2=@mean(gdp, "2000q1 @last")
smpl @all
2. Press Enter after each command line.
17
Descriptive Statistics Functions:
Example 2 (cont’d)
• The result is shown here. For
ease of exposition, we plot
the graph of the series.
18
Descriptive Statistics Functions:
Example 3
• Suppose you want to create a variable which is the average (or sum) of
multiple series.
Descriptive Statistics Functions: Example 3
1a. One way to do this is to type the following in the command window :
series new=(gdp+pce+inv)/3
1b. Another way, would be to type in the command window group row functions:
group groupdata gdp pce inv
This creates a new group (named “groupdata”) with containing the three series.
2b. Now type the following command in the command window:
series new=@rmean(groupdata)
This creates a series which is computed by taking the mean of all the three series for each
row.
3. Press Enter after each command line.
19
Descriptive Statistics Functions:
Example 4
• Suppose you want to collect descriptive statistics in a vector (or matrix).
20
Descriptive Statistics Functions:
Example 4 (cont’d)
Explanations of commands/functions for Example 4
Function Description
smpl 1980q01 1990q04 Defines the sample over which descriptive stats are computed
vector(3) v Creates a vector v with 3 elements
v(1)=@mean(gdp) Assigns the first element of the vector v to equal the mean of gdp
over the defined sample
• Note that the optional sample argument may only be used if the results are assigned to
a series. For example: series y=@mean(x[,s]) – where s is the sample, allows you to
define the sample as the last argument of the descriptive statistic function.
• If results are assigned into a matrix, vector or scalar object (as in example above), the
sample needs to be defined explicitly before using the descriptive statistical functions.
21
Common Time-Series Functions
(Lags/Leads, Differences, Percent Change, Moving Statistics, Trends)
Lags & Leads
• It is easy to work with lags/leads and other time series functions in Eviews.
• You do not need to generate series of lags/leads in many places in
EViews; simply write the command for them when needed (i.e., when
estimating a regression).
Common Commands/Functions
Function Description
gdp(-4) Denotes the 4th lag of the GDP series
gdp(2) Denotes the 2nd lead of the GDP series
gdp (-1 to -4) Specifies all GDP lags from 1 to 4
gdp(to -5) OR gdp (0 to -5) Specifies all GDP lags from 0 to -5
series y = @lag(gdp,3) Generates series y, as the 3rd lag of GDP
series y =@lag((gdp-inv)/gdp,4) Generates series y, as the 4th lag of the transformation
(gdp-inv)/gdp
23
Lags & Leads:
Example 1
• Create and show the 4th lag of the variable gdp.
24
Lags & Leads:
Example 2
• Create and show the 2nd lead of the variable gdp.
25
Lags & Leads:
Example 3
• You can just as easily create a number of lags in EViews
• For example, you may show the actual value and all first three lags of gdp.
26
Lags & Leads:
Example 4
• You can also create a series as a lagged transformation of other series.
27
Differences:
Example 1
• EViews has several build-in functions for working with differenced data
(levels and logs).
•Function Description
d(gdp) = gdp - gdp(-1) Takes the first difference of the GDP series
Differences : Example 1
1. Type in the command window:
show d(gdp)
2. Press Enter.
• Note that for comparison purposes
here we have shown both the d(gdp)
series and another series where the
difference is computed manually
(gdp-gdp(-1)). As seen, they are the
same.
28
Differences:
Example 2
• Log-difference:
•Function Description
dlog(gdp) = log(gdp) – log(gdp(-1)) Takes the first difference of log(GDP) series
Differences : Example 2
1. Type in the command window:
show dlog(gdp)
2. Press Enter.
29
Differences:
Example 3
• EViews also allows you to take higher order differences (of levels and logs):
Function Description
d(gdp,3) 3rd order difference of GDP series ((1-L)3X) (Note: This is
NOT the same as X-X(-3); it is X-3X(-1)+3X(-2)-X(-3))
Differences : Example 3
1. Type in the command window:
show d(gdp,3)
2. Press Enter.
30
Differences:
Example 4
• Higher-order differences in logs
Function Description
dlog(gdp,4) 4th order difference of log(GDP) series ((1-L)3log(X).
Differences : Example 4
1. Type in the command window:
show dlog(gdp,4)
2. Press Enter.
31
Seasonal Differences
• You can also take seasonal differences after specifying both ordinary and
seasonal difference terms:
Function Description
d(gdp,1,4) 1st order differences with seasonal difference in lag 4
32
Percent Change
• EViews also has a number of functions dealing with percent changes.
Common Functions
Function Description
@pc(gdp) calculates the one-period percent change in GDP (in percent)
@pca(gdp) calculates the one-period annualized percent change in GDP (in percent)
(1+@pch(x))n-1 (where n=4 for quarterly data, n=12 for monthly, ect)
@pcha(gdp) calculates the one-period annualized percent change in GDP (in decimal)
33
Percentages:
Example 1
• Calculate the percent change in gdp from the previous period.
Percentages : Example 1
1. Type in the command window:
show @pc(gdp)
34
Percentages:
Example 2
• Calculate the annualized one-period percent change in gdp.
Percentages : Example 2
1. Type in the command window:
show @pca(gdp)
2. Press Enter.
35
Percentages:
Example 3
• Calculate the year-over-year percent change in gdp.
Percentages : Example 3
1. Type in the command window:
show @pcy(gdp)
2. Press Enter.
36
Cumulative Statistic Functions
• Cumulative functions perform “running-total”-type calculations.
• For these functions the length of the window changes with each observation.
Common Functions
Function Description
@cumsum(x,s) Cumulative Sum of the values in X over sample s
@cumprod(x,s) Cumulative Product of the values in X over sample s
@cummean(x,s) Mean of the values in X over sample s up to and including current observation
37
Cumulative Statistic Functions:
Example 1
• Show the cumulative sum for series y over a pre-defined sample.
38
Cumulative Statistic Functions:
Example 2
• Show the cumulative backward sum for series y over a pre-defined sample.
39
Cumulative Statistic Functions:
Example 3
• Show the cumulative mean of series y over a pre-defined sample.
40
Cumulative Statistic Functions:
Example 4
• Show the cumulative standard deviation of series y over a pre-defined sample.
41
Moving Statistic Functions
• These types of functions have shorter, fixed, user-specified window lengths.
• They provide information on n observations (including the current observation).
• The window length n is chosen by the user.
• If the original data has missing values (NA), results may or may not
propagate NA.
Generic Function Description
@mov[specified statistic] This command generates missing values
@m[specified statistic] This command skips NA observations and does not generate
NA values
42
Moving Statistic Functions (cont’d)
Common Functions
Function Description
@movav(x,n) n-period backwards moving average (if n=3, X+X(-1)+X(-2)/3).
NAs are generated
@movav(x(-1),n) n-period backwards moving average (if n=3, X+X(-1)+X(-2)/3) lagged
by one period. NAs are generated
@movsum(x,n) n-period backwards moving sum (if n=3, X+X(-1)+X(-2)).
NAs are generated
@movvar(x,n) n-period backwards moving variances: population variance for the
current and previous n-1 observations. NAs are generated
@mav(x,n) n-period backwards moving average (if n=3, X+X(-1)+X(-2)/3).
NAs are NOT generated
@msum(x,n) n-period backwards moving sum (if n=3, X+X(-1)+X(-2)).
NAs are NOT generated
@mvars(x,n) n-period backwards moving sample variance: sample variance for
the current and previous n-1 observations. NAs are NOT generated
43
Moving Statistic Functions:
Example 1
• Show a 4-period moving average for series y.
44
Moving Statistic Functions:
Example 2
• You can combine operators (functions) to perform more complex examples.
• Suppose you would like to show the 4-period moving average for series y
lagged by one period.
45
Moving Statistic Functions:
Example 3
• Show a 5-period backward moving sum for series y.
46
Moving Statistic Functions:
Example 4
• Show a centered 5-period backward moving sum for series y.
47
Trends
• It is very easy to generate trends in EViews by using a number of built-in
functions.
Common Functions
Function Description
@trend Time trend increasing with each observation
@trendc Calendar-based time trend (in irregular workfiles @trend and @trendc
return different results)
@trend^2 Quadratic time trend increasing with each observation
48
Trends: Examples
• Create two trend series one with initial value 0 and the other one 1.
Generating Trends:
1. Type in the command window:
series trend1=@trend
series trend2=@trend+1
Trend1 Trend2
2. Press Enter after each command.
49