Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Eviews5 / EViews5 / Docs / EViews 5 Command Ref.pdf
Скачиваний:
75
Добавлен:
23.03.2015
Размер:
5.23 Mб
Скачать

Copying Data Between Objects—27

Same object type on right-hand side

If the source object on the right is a matrix or vector, and the target or destination object on the left is of the same type, the target will be resized to have the same dimension as the source, and every source element will be copied. For example:

matrix(10,2) zdata = 5

matrix ydata = zdata

matrix(10,10) xdata = ydata

declares that ZDATA is a 10 × 2 matrix filled with 5’s. YDATA is automatically resized to be a 10 × 2 matrix and is filled with the contents of ZDATA.

Note that even though the declaration of XDATA calls for a 10 × 10 matrix, XDATA is a 10 × 2 matrix of 5’s. This behavior occurs because the declaration statement above is equivalent to issuing the two commands:

matrix(10,10) xdata xdata = ydata

which will first declare the 10 × 10 matrix XDATA, and then automatically resize it to 10 × 2 when you fill it with the values for YDATA.

The next section discusses assignment statements in the more general case, where you are converting between object types. In some cases, the conversion is automatic; in other cases, EViews provides you with additional tools to perform the conversion.

Copying Data Between Objects

In addition to the basic assignment statements described in the previous section, EViews provides you with a large set of tools for copying data to and from matrix objects.

At times, you may wish to move data between different types of matrix objects. For example, you may wish to take the data from a vector and put it in a matrix. EViews has a number of built-in rules which make these conversions automatically.

At other times, you may wish to move data between a matrix object and an EViews series or group object. There are a separate set of tools which allow you to convert data across a variety of object types.

Copying data from matrix objects

Data may be moved between different types of matrix objects using assignments. If possible, EViews will resize the target object so that it contains the same information as the object on the right side of the equation.

The basic rules governing expressions of the form “Y=X” may be summarized as follows:

28—Chapter 3. Matrix Language

Object type of Y does not change.

The target object Y will, if possible, be resized to match the object X; otherwise, EViews will issue an error. Thus, assigning a vector to a matrix will resize the matrix, but assigning a matrix to a vector will generate an error if the matrix has more than one column.

The data in X will be copied to Y.

Specific exceptions to the rules given above are:

If X is a scalar, Y will keep its original size and will be filled with the value of X.

If X and Y are both vector or rowvector objects, Y will be changed to the same type as X.

“Summary of Automatic Resizing of Matrix Objects” on page 42 contains a complete summary of the conversion rules for matrix objects.

Here are some simple examples illustrating the rules for matrix assignment:

vector(3) x x(1) = 1 x(2) = 2 x(3) = 3 vector y = x matrix z = x

Y is now a 3 element vector because it has the same dimension and values as X. EViews automatically resizes the Z Matrix to conform to the dimensions of X so that Z is now a 3 × 1 matrix containing the contents of X: Z(1,1)=1, Z(2,1)=2, Z(3,1)=3.

Here are some further examples where automatic resizing is allowed:

vector(7) y = 2

scalar value = 4

matrix(10,10) w = value

w = y

matrix(2,3) x = 1

rowvector(10) t = 100 x = t

W is declared as a 10 × 10 matrix of 4’s, but it is then reset to be a 7 × 1 matrix of 2’s. X is a 1 × 10 matrix of 100’s.

Lastly, consider the commands:

Copying Data Between Objects—29

vector(7) y = 2

rowvector(12) z = 3

coef(20) beta

y = z

z = beta

Y will be a rowvector of length 3, containing the original contents of Z, and Z will be a column vector of length 20 containing the contents of BETA.

There are some cases where EViews will be unable to perform the specified assignment because the resize operation is not defined. For example, suppose that X is a 2 × 2 matrix. Then the assignment statement:

vector(7) y = x

will result in an error. EViews cannot change Y from a vector to a matrix and there is no way to assign the 4 elements of the matrix X to the vector Y. Other examples of invalid assignment statements involve assigning matrix objects to scalars or sym objects to vector objects.

Copying data from parts of matrix objects

In addition to the standard rules for conversion of data between objects, EViews provides functions for extracting and assigning parts of matrix objects. Matrix functions are described in greater detail later in this chapter. For now, note that some functions take a matrix object and perhaps other parameters as arguments and return a matrix object.

A comprehensive list of the EViews commands and functions that may be used for matrix object conversion appears in “Utility Functions and Commands” on page 44. However, a few examples will provide you with a sense of the type of operations that may be performed.

Suppose first that you are interested in copying data from a matrix into a vector. The following commands will copy data from M1 and SYM1 into the vectors V1, V2, V3, and V4.

matrix(10, 10) m1 sym(10) sym1

vector v1 = @vec(m1)

vector v2 = @columnextract(m1,3) vector v3 = @rowextract(m1,4) vector v4 = @columnextract(sym1,5)

The @vec function creates a 100 element vector, V1, from the columns of M1 stacked one on top of another. V2 will be a 10 element vector containing the contents of the third col-

30—Chapter 3. Matrix Language

umn of M1 while V3 will be a 10 element vector containing the fourth row of M1. The @vec, @rowextract, and @columnextract functions also work with sym objects. V4 is a 10 element vector containing the fifth column of SYM1.

You can also copy data from one matrix into a smaller matrix using @subextract. For example:

matrix(20,20) m1=1

matrix m2 = @subextract(m1,5,5,10,7)

matrix m3 = @subextract(m1,5,10)

matrix m4 = m1

M2 is a 6 × 3 matrix containing a submatrix of M1 defined by taking the part of the matrix M1 beginning at row 5 and column 5, and ending at row 10 and column 7. M3 is the

16 × 11 matrix taken from M1 at row 5 and column 10 to the last element of the matrix (row 20 and column 20). In contrast, M4 is defined to be an exact copy of the full 20 × 20 matrix.

Data from a matrix may be copied into another matrix object using the commands colplace, rowplace, and matplace. Consider the commands:

matrix(100,5) m1 = 0 matrix(100,2) m2 = 1 vector(100) v1 = 3 rowvector(100) v2 = 4 matplace(m1,m2,1,3) colplace(m1,v1,3) rowplace(m1,v2,80)

The matplace command places M2 in M1 beginning at row 1 and column 3. V1 is placed in column 3 of M1, while V2 is placed in row 80 of M1.

Copying data between matrix objects and other objects

The previous sections described techniques for copying data between matrix objects such as vectors, matrices and scalars. In this section, we describe techniques for copying data between matrix objects and other EViews objects such as series and groups.

Keep in mind that there are two primary differences between the ordinary series or group objects and the matrix objects. First, operations involving series and groups use information about the current workfile sample, while matrix objects do not. Second, there are important differences in the handling of missing values (NAs) between the two types of objects.

Copying Data Between Objects—31

Direct Assignment

The easiest method to copy data from series or group objects to a matrix object is to use direct assignment. Place the destination matrix object on the left side of an equal sign, and place the series or group to be converted on the right.

If you use a series object on the right, EViews will only include the observations from the current sample to make the vector. If you place a group object on the right, EViews will create a rectangular matrix, again only using observations from the current sample.

While very convenient, there are two principal limitations of this approach. First, EViews will only include observations in the current sample when copying the data. Second, observations containing missing data (NAs) for a series, or for any series in the group, are not placed in the matrix. Thus, if the current sample contains 20 observations, but the series or group contains missing data, the dimension of the vector or matrix will be less than 20. Below, we provide you with methods which allow you to override the current sample and to retain missing values.

Examples:

smpl 1963:3 1993:6 fetch hsf gmpyq

group mygrp hsf gmpyq vector xvec = gmpyq matrix xmat = mygrp

These statements create the vector XVEC and the two column matrix XMAT containing the non-missing series and group data from 1963:3 to 1993:6. Note that if GMPYQ has a missing value in 1970:01, and HSF contains a missing value in 1980:01, both observations for both series will be excluded from XMAT.

When performing matrix assignment, you may refer to an element of a series, just as you would refer to an element of a vector, by placing an index value in parentheses after the name. An index value i refers to the i-th element of the series from the beginning of the workfile range. For example, if the range of the current annual workfile is 1961 to 1980, the expression GNP(6) refers to the 1966 value of GNP. These series element expressions may be used in assigning specific series values to matrix elements, or to assign matrix values to a specific series element. For example:

matrix(5,10) x

series yser = nrnd

x(1,1) = yser(4)

yser(5) = x(2,3)

yser(6) = 4000.2

32—Chapter 3. Matrix Language

assigns the fourth value of the series YSER to X(1,1), and assigns to the fifth and sixth values of YSER, the X(2,3) value and the scalar value “4000.2”, respectively.

While matrix assignments allow you to refer to elements of series as though they were elements of vectors, you cannot generally use series in place of vectors. Most vector and matrix operations will error if you use a series in place of a vector. For example, you cannot perform a rowplace command using a series name.

Furthermore, note that when you are not performing matrix assignment, a series name followed by a number in parentheses will indicate that the lag/lead operator be applied to the entire series. Thus, when used in generating series or in an equation, system, or model specification, GNP(6) refers to the sixth lead of the GNP series. To refer to specific elements of the GNP series in these settings, you should use the @elem function.

Copy using @convert

The @convert function takes a series or group object and, optionally, a sample object, and returns a vector or rectangular matrix. If no sample is provided, @convert will use the workfile sample. The sample determines which series elements are included in the matrix. Example:

smpl 61 90

group groupx inv gdp m1

vector v = @convert(gdp)

matrix x = @convert(groupx)

X is a 30 × 3 matrix with the first column containing data from INV, the second column from GDP, and the third column from M1.

As with direct assignment, the @convert function excludes observations for which the series or any of the series in the group contain missing data. If, in the example above, INV contains missing observations in 1970 and 1980, V would be a 29 element vector while X would be a 28 × 3 matrix. This will cause errors in subsequent operations that require V and X to have a common row dimension.

There are two primary advantages of using @convert over direct assignment. First, since @convert is a function, it may be used in the middle of a matrix expression. Second, an optional second argument allows you to specify a sample to be used in conversion. For example:

sample s1.set 1950 1990

matrix x = @convert(grp,s1)

sym y = @inverse(@inner(@convert(grp,s1)))

performs the conversion using the sample defined in S1.

Copying Data Between Objects—33

Copy data between Series and Matrices

EViews also provides three useful commands that perform explicit conversions between series and matrices with control over both the sample, and the handling of NAs.

stom (Series TO Matrix) takes a series or group object and copies its data to a vector or matrix using either the current workfile sample, or the optionally specified sample. As with direct assignment, the stom command excludes observations for which the series or any of the series in the group contain missing data.

Example:

sample smpl_cnvrt.set 1950 1995 smpl 1961 1990

group group1 gnp gdp money vector(46) vec1 matrix(3,30) mat1 stom(gdp,vec1,smpl_cnvrt) stom(group1,mat1)

While the operation of stom is similar to @convert, stom is a command and cannot be included in a matrix expression. Furthermore, unlike @convert, the destination matrix or vector must already exist and have the proper dimension.

stomna (Series TO Matrix with NAs) works identically to stom, but does not exclude observations for which there are missing values. The elements of the series for the relevant sample will map directly into the target vector or matrix. Thus,

smpl 1951 2000

vector(50) gvector

stom(gdp,gvector)

will always create a 50 element vector GVECTOR that contains the values of GDP from 1951 to 2000, including observations with NAs.

mtos (Matrix TO Series) takes a matrix or vector and copies its data into an existing series or group, using the current workfile sample or a sample that you provide.

Examples:

mtos(mat1,group1)

mtos(vec1,resid)

mtos(mat2,group1,smpl1)

As with stom the destination series or group must already exist and the destination dimension given by the sample must match that of the source vector or matrix.

Соседние файлы в папке Docs