SAS/IML Reference Card Creating Matrices M = {1 2, 3 4} creates

advertisement
SAS/IML Reference Card
Creating Matrices
M = {1 2, 3 4}
I(n)
diag(V ), diag({. . .})
vecdiag(M )
(n : p)
do(x, y, t)
J(r, c, v)
repeat(M, r, c)
shape(V, r, c)
M ‘ ou T (M )
A||B
A//B
1 2
creates matrix
3 4
identity matrix of size n
diagonal matrix whose elements are those of vector V or {. . .}
column vector made of M ’s diagonal elements
sequence vector ranging from n to p with an increment of 1
sequence vector ranging from x to y with an increment of t
matrix of size r × c filled with element v
matrix that repeats block M , r times in row and c times in column
matrix of size r × c filled with the elements of V
M transposed
horizontal concatenation of A and B
vertical concatenation of A and B
Operations on Matrices and Scalars
+
addition (matrices or scalars)
−
subtraction (matrices or scalars)
∗
matrix multiplication (matrices or scalars)
#
elementwise multiplication (matrices or scalars)
/
elementwise division (matrices or scalars)
∗∗
matrix power operator
##
elementwise power operator
sqrt(M )
elementwise square root operator
@
Kronecker product
Extractions / Reductions / Comparisons
M [Vr , Vc ]
extract of M with rows corresponding to Vr and columns to Vc
M [Vr , ]
extract of M with rows corresponding to Vr (all columns)
M [, Vc ]
extract of M with columns corresponding to Vc (all rows)
M [+]
scalar corresponding to the sum of M ’s elements
M [, :]
column vector of the means of M ’s rows
M [<>, ]
row vector made of the maximum element of each of M ’s columns
><
minimum operator
<:>
index of the maximum
>:<
index of the minimum
##
sum of squares
A>0
boolean matrix of the result of the test on each of A’s elements (same size as A)
Matrix fonctions
nrow(M )
ncol(M )
det(M )
inv(M )
trace(M )
eigval(M )
eigvec(M )
root(M )
number of rows in M
number of columns in M
determinant of M
inverse of M
trace of M
eigenvalues of M
eigenvectors of M
Cholesky decomposition of M (positive) such that U ‘U = M
General SAS fonctions
print A;
prints A in the output
print (ncol(A));
idem but use parenthesis if there is an operation
rannor(J(n, p, 0))
creates a n × p matrix with elements drawn in a normal distribution (mean 0 and variance 1)
ranuni(J(n, p, 0))
creates a n × p matrix with elements drawn in a uniform distribution on the interval [0, 1]
probnorm(x)
probability that a variable following a normal distribution (0, 1) is inferior to x
probt(x, df )
probability that a variable following a Student distribution with df degrees of freedom is inferior to x
probf (x, df1 , df2 )
probability that a variable following a Fischer distribution (df1 , df2 ) is inferior to x
cdf (0 T 0 , x, df )1
cumulative density function of a T distribution with df degrees of freedom at point x
pdf (0 N ORM AL0 , x)1
probability density function of a normal distribution (0, 1) at point x
1
list of available distributions: BERNOULLI, BETA, BINOMIAL, CAUCHY, CHISQUARE, EXPONENTIAL, F, GAMMA, GEOMETRIC, HYPERGEOMETRIC, LAPLACE,
LOGISTIC, LOGNORMAL, NEGBINOMIAL, NORMAL or GAUSS, NORMALMIX, PARETO, POISSON, T, UNIFORM, WALD or IGAUSS, WEIBULL
Reading data from a SAS dataset
use lib.toto;
read all var {. . .} into Y;
read point 2 var {. . .} into X;
/*
/*
Reads all observations (all).
Reads only line 2 (point 2).
*/
*/
Creating a SAS dataset in IML
create lib.toto from X [colname = {. . .}];
append;
/*
/*
Creates table lib.toto from matrix X.
Instruction ‘colname’ defines column names.
*/
*/
Creating a module
proc iml;
start transpose(M);
t_M=M‘;
return(t_M);
finish;
quit;
/*
/*
/*
/*
/*
Beginning of the ‘transpose’ module which takes M as argument.
Creates M transposed.
The module returns M transposed.
End of the module.
A module can also return nothing.
*/
*/
*/
*/
*/
Using an optimization routine1
proc iml;
use lib.td2;
read all var {depense} into Y;
read all var {age ind revenu} into X1;
e=J(nrow(Y),1,1);
X=e||X1;
start ols(beta) global(y,x);
residu=y-x*beta‘;
scr=residu[##];
return(scr);
finish;
beta0=J(1,ncol(X),0);
optn={0 4};
call nlpnra(rc,beta,’ols’,beta0,optn);
print beta;
quit;
1
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
/*
OLS by minimization of the residuals’ sum of squares
Declares SAS table.
Loads expenditure vector into Y.
Loads explanatory variables into X1.
Creates a constant vector.
Concatenates the constant vector with X1.
Starts module and declare
optimization parameters as local variables
and the rest as global variables.
Computes the residual which will
be minimized.
Computes the sum of squares.
Returns the value to optimize.
End of module.
Declares the vector which will be used
as initialization for the optimization routine.
Optimization options: 0 to minimize,
4 to control what will be printed.
Optimization routine:
rc is the return code vector,
beta must be a row vector.
Prints the result (vector).
available routines: NLPCG, NLPDD, NLPNMS, NLPNRA, NLPNRR, NLPQN, NLPQUA, NLPTR
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
*/
Download