0% found this document useful (0 votes)
15 views

Tutorial5 Data Functions

The document describes functions in EViews for generating random numbers and performing descriptive statistics on data. It provides examples of: 1) Generating random series from different distributions using functions like @nrnd and @runif. 2) Calculating descriptive statistics like the mean, median, variance and standard deviation of data using functions like @mean, @median, @vars. 3) Creating new series that represent descriptive statistics over different sample periods. The document outlines commonly used functions for random number generation and descriptive statistics in EViews and provides examples of applying these functions to data.

Uploaded by

Quynh Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Tutorial5 Data Functions

The document describes functions in EViews for generating random numbers and performing descriptive statistics on data. It provides examples of: 1) Generating random series from different distributions using functions like @nrnd and @runif. 2) Calculating descriptive statistics like the mean, median, variance and standard deviation of data using functions like @mean, @median, @vars. 3) Creating new series that represent descriptive statistics over different sample periods. The document outlines commonly used functions for random number generation and descriptive statistics in EViews and provides examples of applying these functions to data.

Uploaded by

Quynh Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 49

EViews Training

Data Objects: 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:

• Workfile Page: Timeseries (Data.xlsx tab Timeseries):


quarterly, Q1 1980 – Q1 2012
 GDP – real GDP data (billions of dollars) from the Bureau of Economic Analysis.
 PCE – real consumption data (billions of dollars) from the Bureau of Economic Analysis.
 Inv – real private sector investments (billions of dollars) from the Bureau of Economic Analysis.
 G – real government spending (billions of dollars) from the Bureau of Economic Analysis.
 y – a series that grows over time (trend series).

2
Data Functions
• EViews has a large number of built-in functions that allow you to perform
data manipulation.

• EViews functions are typically denoted by the symbol “@”.


• This tutorial reviews some typical functions used for data manipulation,
such as:
 Random numbers
 Statistical Functions
 Commonly Used Time Series Functions

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.

• As seen, this generates a


series y1, which is normally
distributed with mean 0 and
standard deviation equal to 1.

6
Generating Random Numbers (Series):
Example 2
• Alternatively, you can create a random series by typing in the command window.

Generating a random series: Example 2


1. Type in the command window:
series z=nrnd
2. Press Enter.

• This creates a new series z, which


is normally distributed with mean 0
and standard deviation equal to 1.

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.

• Note: for an explanation of statistical functions @mean, @stdev, or d(gdp), see


Descriptive Statistics in this tutorial.
8
Generating Random Numbers (Series):
Example 3 (cont’d)
• Here we show both the new
series (newdata) and the GDP
series.
• As seen, the command has
generated a series newdata, which
follows the same distribution
process as the gdp series.
• Note that the commands instruct
EViews to first create a new series
(newdata) consisting of zeros.
• Then, from the second observation
(1980q2) onwards it sums the
preceding observation with the
mean and standard deviation of
differenced gdp (multiplied by a
normal random variable).

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=3+@sqr(4)*nrnd Normal distribution (mean 3, variance 4)

series y=@rlognorm(1,4) Lognormal distribution (mean=1, st. dev 4)

series y=@runif(1,3) Uniform distribution on (1,3)

series y=rnd Uniform distribution on (0,1)

series y=1+(3-1)* rnd Uniform distribution on (1,3) (same as @runif(1,3))

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.

Generating a random series together with its pdf and cdf:


1. Type in the command window:
smpl @all
series d1=@ruinf(0,2)
show d1 @dunif(d1,0,2) @cunif(d1,0,2)
2. Press Enter after each command line.

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)

series y=@median(x) Creates a series where each observation is equal to the


median of X
series y=@vars(x) Computes the sample variance of X (adj. by n-1)

series y=@varp(x) or Computes the population variance of X (adj. by n)


y=@var(x)

series y=@stdev(x) or Computes the sample st. dev. of X (adj. by n-1)


y=@stdevs(x)

series y=@stdevp(x) Computes the population st. dev. of X (adj. by n)

15
Descriptive Statistics Functions:
Example 1
• Suppose you want to create a series x1, which is equal to the mean of the
“gdp” series.

Descriptive Statistics Functions: Example 1


1. Type in the command window:
series x1=@mean(gdp)
2. Press Enter.

• This creates a new series x1 which has


all elements equal to the mean of GDP.

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.

• As you can see, EViews has


created a step-wise function
defined over the various samples
as we specified in the command
line.

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).

Descriptive Statistics Functions: Example 4


1. Let’s first define the sample over which descriptive stats are computed. Type in
the command window:
smpl 1980m01 1990m12
2. Next, let’s create a vector v by typing in the command window:
vector(3) v
3. Next, define the vector elements to gather the desired statistics, by typing in the
command window:
v(1)=@mean(gdp)
v(2)= @varp(gdp)
v(3)= @covs(gdp,pce)
4. Press Enter after each command line.

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

v(2)=@varp(gdp) Assigns the second element of the vector v to equal the


population variance of gdp (adj. by n)
v(3)=@covs(gdp,pce) Assigns the third element of the vector v to equal the sample
covariance between gdp and pce (adj. by n-1)

• 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.

Lags & Leads: Example 1


1. Type in the command window:
smpl @all
show gdp gdp(-4)
2. Press Enter.

• Note that this creates a new group


which contains both the gdp series and
its fourth lag (we requested that both
series appear)
• If you would like to save the group,
click the button and name the
group.

24
Lags & Leads:
Example 2
• Create and show the 2nd lead of the variable gdp.

Lags & Leads: Example 2


1. Type in the command window:
show gdp gdp(2)
2. Press Enter.

• Note that this creates a new group


which contains both the gdp series and
its 2nd lead (we requested that both
series appear)
• We have saved and named the group
(group 04).

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.

Lags & Leads: Example 3


1. Type in the command window:
show gdp(0 to -3)
2. Press Enter.

• Note that this creates a new group


which contains the gdp series and
all lags from 1 to 3.

• The group is saved as group 05.

26
Lags & Leads:
Example 4
• You can also create a series as a lagged transformation of other series.

Lags & Leads: Example 4


1. Type in the command window:
series newseries=@lag((gdp-inv)/gdp,4)
2. Press Enter.

• Note that the new series is first computed as:


(gdp-inv)/gdp.

• Then, the fourth lag is taken.

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

d(gdp,0,4) Captures only seasonal difference (at 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)

@pch(gdp) calculates the one-period percent change in GDP (in decimal)

@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)

@pcy(gdp) calculates the one-year percent change (in percent)

@pchy(gdp) calculates the one-year percent change (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)

Note that @pc shows the percent change


in percent. If you would like to compute it
in decimals, type in the command window:
show @pch(gdp)
2. Press Enter.

• Note that we instructed EViews to


display both series (@pc(gdp) and
@pch(gdp)) in the same group. The
group is saved as group06.

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.

• Note this computes the annualized


percent change in the quarterly (one-
period) gdp data.
• This is the same as:
100*((1+@pch(gdp))4-1).

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.

• Note this computes the one-year


percent change in gdp data.
• In quarterly data (which is the case
here), this is the same as:
100*(gdpt+4-gdpt/gdpt).

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

@cumobs(x,s) The number of non-missing observations in X from over sample s


@cumbsum(x,s) Backwards cumulative sum of the values of X over sample s beginning with
the end of the sample
@cumbmean(x,s) Backwards cumulative mean of the values of X over sample s beginning with
the end of the sample up to and including current observation
@cumbstdev Backwards cumulative standard deviation of the values of X over sample s
beginning with the end of the sample up to and including current observation

37
Cumulative Statistic Functions:
Example 1
• Show the cumulative sum for series y over a pre-defined sample.

Cumulative Statistic: Example 1


1. Type in the command window:
show @cumsum(y,"1980Q3 1981Q4")
2. Press Enter. Original series @cumsum(y,s)

• Note that to facilitate comparisons, we


have shown both the original (y) series
and the new cumulative sum defined
over the specific sample.
• The cumulative sum series is calculated
by adding the values from the start of the
sample to the current value.
• The cumulative sum starts at the
beginning of the sample (1980Q3) and
ends at the end of the sample (1981Q4).

38
Cumulative Statistic Functions:
Example 2
• Show the cumulative backward sum for series y over a pre-defined sample.

Cumulative Statistic: Example 2


1. Type in the command window:
show @cumbsum(y,"1980Q3 1981Q4")
2. Press Enter. Original series @cumbsum(y,s)

• The backward cumulative sum series is


calculated by adding the values from the
end of the sample to the current value.
• Note that, unlike the previous example,
the backward cumulative sum starts at the
end of the sample (1984Q1) and ends at
the start of the sample (1980Q3).

39
Cumulative Statistic Functions:
Example 3
• Show the cumulative mean of series y over a pre-defined sample.

Cumulative Statistic: Example 3


1. Type in the command window:
show @cummean(y,"1980Q3 1981Q4")
2. Press Enter. Original series @cummean(y,s)

• The cumulative mean series is calculated


from the start of the sample (1980Q3)
until the current observation.

40
Cumulative Statistic Functions:
Example 4
• Show the cumulative standard deviation of series y over a pre-defined sample.

Cumulative Statistic: Example 4


1. Type in the command window:
show @cumstdev(y,"1980Q3 1981Q4")
2. Press Enter. Original series @cumstdev(y,s)

• The cumulative standard deviation is


calculated from the start of the sample
(1980Q3) until the current observation.

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.

Moving Statistic: Example 1


1. Type in the command window:
show @movav(y,4)
2. Press Enter. Original series @movav(y,n)

• Note that the 4-period backwards


moving average is computed as:
(X+X(-1)+X(-2)+X(-3))/4.

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.

Moving Statistic: Example 2


1. Type in the command window:
Original series @movav(y(-1),n)
show @movav(y(-1),4)
2. Press Enter.

• The 4-period backwards moving


average is computed as:
(X+X(-1)+X(-2)+X(-3))/4. This is
then lagged by one period as we
specified.

45
Moving Statistic Functions:
Example 3
• Show a 5-period backward moving sum for series y.

Moving Statistic: Example 3


1. Type in the command window:
show @movsum(y,5)
Original series @movsum(y,5)
2. Press Enter.

• Note that the 5-period backward


moving sum is generated by
adding:
(X+X(-1)+X(-2)+X(-3)+X(-4)).

46
Moving Statistic Functions:
Example 4
• Show a centered 5-period backward moving sum for series y.

Moving Statistic: Example 4


1. Type in the command window:
show @movsum(y(2),5)
Original series @movsum(y(2),5)
2. Press Enter.

• Note that the 5-period backward


moving sum is now centered at
the observation which falls in the
middle of the interval.
• It is computed by adding:
(X(-1)+X(-2)+X+X(+1)+X(+2)).

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.

• Note that @trend creates a series that


begins at 0; while @trend+1 has initial
value of 1.

49

You might also like