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

Matrix Views and Procs—39

NA Handling

As noted above, most of the methods of moving data from series and groups into matrix objects will automatically drop observations containing missing values. It is still possible, however, to encounter matrices which contain missing values.

For example, the automatic NA removal may be overridden using the stomna command (p. 489). Additionally, some of the element operators may generate missing values as a result of standard matrix operations. For example, taking element-by-element logarithms of a matrix using @log will generate NAs for all cells containing nonpositive values.

EViews follows two simple rules for handling matrices that contain NAs. For all operators, commands, and functions, except the descriptive statistics function, EViews works with the full matrix object, processing NAs as required. For descriptive statistic functions, EViews automatically drops NAs when performing the calculation. These rules imply the following:

Matrix operators will generate NAs where appropriate. Adding together two matrices that contain NAs will yield a matrix containing NAs in the corresponding cells. Multiplying two matrices will result in a matrix containing NAs in the appropriate rows and columns.

All matrix algebra functions and commands will generate NAs, since these operations are undefined. For example, the Cholesky factorization of a matrix that contains NAs will contain NAs.

All utility functions and commands will work as before, with NAs treated like any other value. Copying the contents of a vector into a matrix using colplace will place the contents, including NAs, into the target matrix.

All of the matrix element functions will propagate NAs when appropriate. Taking the absolute value of a matrix will yield a matrix containing absolute values for nonmissing cells and NAs for cells that contain NAs.

The descriptive statistics functions are based upon the non-missing subset of the elements in the matrix. You can always find out how many values were used in the computations by using the @OBS function.

Matrix Views and Procs

The object listing in Appendix A, “Object, View and Procedure Reference”, on page 153 lists the various views and procs for all of the matrix objects.

40—Chapter 3. Matrix Language

Matrix Graph and Statistics Views

All of the matrix objects, with the exception of the scalar object, have windows and views. For example, you may display line and bar graphs for each column of the 10 × 5 matrix Z:

z.line

z.bar(p)

Each column will be plotted against the row number of the matrix.

Additionally, you can compute descriptive statistics for each column of a matrix, as well as the correlation and covariance matrix between the columns of the matrix:

z.stats

z.cor

z.cov

EViews performs listwise deletion by column, so that each group of column statistics is computed using the largest possible set of observations.

The full syntax for the commands to display and print these views is listed in the object reference.

Matrix input and output

EViews provides you with the ability to read and write files directly from matrix objects using the read and write procedures.

You must supply the name of the source file. If you do not include the optional path specification, EViews will look for the file in the default directory. The input specification follows the source file name. Path specifications may point to local or network drives. If the path specification contains a space, you must enclose the entire expression in double quotes “”.

In reading from a file, EViews first fills the matrix with NAs, places the first data element in the “(1,1)” element of the matrix, then continues to read the data by row or by column, depending upon the options set.

The following command reads data into MAT1 from an Excel file CPS88 in the network drive specified in the path directory. The data are read by column, and the upper left data cell is A2.

mat1.read(a2,s=sheet3) "\\net1\dr 1\cps88.xls"

To read the same file by row, you should use the “t” option:

Matrix Operations versus Loop Operations—41

mat1.read(a2,t,s=sheet3) "\\net1\dr 1\cps88.xls"

To write data from a matrix, use the write keyword, enter the desired options, then the name of the output file. For example:

mat1.write mydt.txt

writes the data in MAT1 into the ASCII file MYDT.TXT located in the default directory.

There are many more options for controlling reading and writing of data; Chapter 5, “Basic Data Handling”, on page 85 of the User’s Guide provides extensive discussion. See also read (p. 414) and write (p. 545).

Matrix Operations versus Loop Operations

You can perform matrix operations using element operations and loops instead of the builtin functions and commands. For example, the inner product of two vectors may be computed by evaluating the vectors element-by-element:

scalar inprod1 = 0

for !i = 1 to @rows(vec1)

inprod1 = inprod1 + vec1(!i)*vec2(!i)

next

This approach will, however, generally be much slower than using the built-in function:

scalar inprod2 = @inner(vec1,vec2)

You should use the built-in matrix operators rather than loop operators whenever you can. The matrix operators are always much faster than the equivalent loop operations.

There will be cases when you cannot avoid using loop operations. For example, suppose you wish to subtract the column mean from each element of a matrix. Such a calculation might be useful in constructing a fixed effects regression estimator. First, consider a slow method involving only loops and element operations:

matrix(2000,10) x = @convert(mygrp1) scalar xsum

for !i = 1 to @columns(x) xsum = 0

for !j = 1 to @rows(x) xsum = xsum+x(!j,!i)

next

xsum = xsum/@rows(x) for !j = 1 to @rows(x)

42—Chapter 3. Matrix Language

x(!j,!i) = x(!j,!i)-xsum

next

next

The loops are used to compute a mean for each column of data in X, and then to subtract the value of the mean from each element of the column. A better and much faster method for subtracting column means uses the built-in operators:

matrix x = @convert(mygrp1)

vector(@rows(x)) xmean

for !i = 1 to @columns(x)

xmean = @mean(@columnextract(x,!i))

colplace(x,@columnextract(x,!i)-xmean,!i)

next

This command extracts each column of X, computes the mean, and fills the vector XMEAN with the column mean. You then subtract the mean from the column and place the result back into the appropriate column of X. While you still need to loop over the control variable !i, you avoid the need to loop over the elements of the columns.

Summary of Automatic Resizing of Matrix Objects

When you perform a matrix object assignment, EViews will resize, where possible, the destination object to accommodate the contents of the source matrix. This resizing will occur if the destination object type can be modified and sized appropriately and if the values of the destination may be assigned without ambiguity. You can, for example, assign a matrix to a vector and vice versa, you can assign a scalar to a matrix, but you cannot assign a matrix to a scalar since the EViews does not allow scalar resizing.

The following table summarizes the rules for resizing of matrix objects as a result of declarations of the form

object_type y = x

where object_type is an EViews object type, or as the result of an assignment statement for Y after an initial declaration, as in:

object_type y

y = x

Each row of the table corresponds to the specified type of the destination object, Y. Each column represents the type and size of the source object, X. Each cell of the table shows the type and size of object that results from the declaration or assignment.

Summary of Automatic Resizing of Matrix Objects—43

 

 

 

 

 

 

Object type and size for source X

 

 

 

 

Object type for Y

 

coef(p)

matrix(p,q)

 

 

 

 

 

 

 

 

coef(k)

 

 

 

 

coef(p)

invalid

 

 

 

 

matrix(n,k)

 

matrix(p,1)

matrix(p,q)

 

 

 

 

rowvector(k)

 

rowvector(p)

invalid

 

 

 

 

scalar

 

invalid

invalid

 

 

 

 

sym(k)

 

invalid

sym(p) if p=q

 

 

 

 

vector(n)

 

vector(p)

invalid

 

 

 

 

 

 

 

 

 

 

 

 

Object type and size for source X

 

 

 

 

Object type for Y

 

rowvector(q)

scalar

 

 

 

 

 

 

 

 

coef(k)

 

 

 

 

coef(q)

coef(k)

 

 

 

 

matrix(n,k)

 

matrix(1,q)

matrix(n,k)

 

 

 

 

rowvector(k)

 

rowvector(q)

rowvector(k)

 

 

 

 

scalar

 

invalid

scalar

 

 

 

 

sym(k)

 

invalid

invalid

 

 

 

 

vector(n)

 

rowvector(q)

vector(n)

 

 

 

 

 

 

 

 

 

Object type and size for source X

 

 

 

 

Object type for Y

 

sym(p)

vector(p)

 

 

 

 

 

 

 

 

coef(k)

 

 

 

 

invalid

coef(p)

 

 

 

 

matrix(n,k)

 

matrix(p,p)

matrix(p,1)

 

 

 

 

rowvector(k)

 

invalid

vector(p)

 

 

 

 

scalar

 

invalid

invalid

 

 

 

 

sym(k)

 

sym(p)

invalid

 

 

 

 

vector(n)

 

invalid

vector(p)

 

 

 

 

For example, consider the command matrix(500,4) y = x

where X is a coef of size 50. The object type is given by examining the table entry corresponding to row “matrix Y” (n = 500, k = 4 ), and column “coef X” (p = 50 ). The entry reads “matrix(p, 1 )”, so that the result Y is a 50 × 1 matrix.

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