|
|
Output from imlxmp1.sas |
Source
0 Graphs |
|---|
NOTE: Capture of log output started.
NOTE: %INCLUDE (level 1) file n:\psy6140\examples\iml\imlxmp1.sas is file
n:\psy6140\examples\iml\imlxmp1.sas.
1309 +title 'Basic matrix operations in Proc IML'; 1310 +options nodate ls=79 pageno=1; 1311 +/*----------------------------------------------------* 1312 + | Basic operations in Proc IML | 1313 + *----------------------------------------------------*/ 1314 + 1315 +proc iml;
IML Ready
1316 + 1317 + reset print log; 1318 + 1319 + *-- Scalars; 1320 + x = 12.3;
X 1 row 1 col (numeric)
12.3
1321 + y = {57};
Y 1 row 1 col (numeric)
57
1322 + 1323 + name = 'Bob';
NAME 1 row 1 col (character, size 3)
Bob
1324 + msg = "Hi there,";
MSG 1 row 1 col (character, size 9)
Hi there,
1325 + 1326 + print msg name x y;
MSG NAME X Y
Hi there, Bob 12.3 57
1327 +
1328 + *-- Creating matrices and vectors;
1329 +
1330 + a = { 2 4,
1331 + 3 1};
A 2 rows 2 cols (numeric)
2 4
3 1
1332 + b = { 4 5, 0 1};
B 2 rows 2 cols (numeric)
4 5
0 1
1333 +
1334 + c = { 1 2 3 };
C 1 row 3 cols (numeric)
1 2 3
1335 + d = { 1, 2, 3};
D 3 rows 1 col (numeric)
1
2
3
1336 +
1337 + e={'a' 'b' 'c', 'd' 'e' 'f'};
E 2 rows 3 cols (character, size 1)
a b c
d e f
1338 + 1339 + *-- Simple matrix operations; 1340 + sum = a + b;
SUM 2 rows 2 cols (numeric)
6 9
3 2
1341 + diff = a - b;
DIFF 2 rows 2 cols (numeric)
-2 -1
3 0
1342 + 1343 + *-- Elementwise product; 1344 + times = a # b;
TIMES 2 rows 2 cols (numeric)
8 20
0 1
1345 + *-- Matrix product; 1346 + prod = a * b;
PROD 2 rows 2 cols (numeric)
8 14
12 16
1347 + sqr = a##2;
SQR 2 rows 2 cols (numeric)
4 16
9 1
1348 + asq = a * a;
ASQ 2 rows 2 cols (numeric)
16 12
9 13
1349 + 1350 + *-- Comparison operations; 1351 + max = a<>b;
MAX 2 rows 2 cols (numeric)
4 5
3 1
1352 + min = a><b;
MIN 2 rows 2 cols (numeric)
2 4
0 1
1353 + less = a < b;
LESS 2 rows 2 cols (numeric)
1 1
0 0
1354 + more = a > b;
MORE 2 rows 2 cols (numeric)
0 0
1 0
1355 +*page; 1356 + *--------------------------------; 1357 + * Matrix-generating functions ; 1358 + *--------------------------------; 1359 + 1360 + ident = I(3);
IDENT 3 rows 3 cols (numeric)
1 0 0
0 1 0
0 0 1
1361 + ones = j(1,5);
ONES 1 row 5 cols (numeric)
1 1 1 1 1
1362 + zero = j(3,2,0);
ZERO 3 rows 2 cols (numeric)
0 0
0 0
0 0
1363 + 1364 + a = j(2,2,2);
A 2 rows 2 cols (numeric)
2 2
2 2
1365 + b = j(2,2,1);
B 2 rows 2 cols (numeric)
1 1
1 1
1366 + ab = block(a,b);
AB 4 rows 4 cols (numeric)
2 2 0 0
2 2 0 0
0 0 1 1
0 0 1 1
1367 +
1368 + a = shape({5 12},3,3);
A 3 rows 3 cols (numeric)
5 12 5
12 5 12
5 12 5
1369 + b = shape({5 12},3,3,-1);
B 3 rows 3 cols (numeric)
5 12 -1
-1 -1 -1
-1 -1 -1
1370 +
1371 + c = repeat({5 12},3,3);
C 3 rows 6 cols (numeric)
5 12 5 12 5 12
5 12 5 12 5 12
5 12 5 12 5 12
1372 + 1373 + index = 1:5;
INDEX 1 row 5 cols (numeric)
1 2 3 4 5
1374 + col = (4:6)`;
COL 3 rows 1 col (numeric)
4
5
6
1375 + rindex= 5:1;
RINDEX 1 row 5 cols (numeric)
5 4 3 2 1
1376 + 1377 + series= do(12,72,12);
SERIES 1 row 6 cols (numeric)
12 24 36 48 60 72
1378 + 1379 + vars = 'XX1': 'XX7';
VARS 1 row 7 cols (character, size 3)
XX1 XX2 XX3 XX4 XX5 XX6 XX7
1380 +
1381 + *-- Diagonal matrices;
1382 + d = diag( {1 2 4} );
D 3 rows 3 cols (numeric)
1 0 0
0 2 0
0 0 4
1383 + s = diag( {1 2, 3 4} );
S 2 rows 2 cols (numeric)
1 0
0 4
1384 + v = vecdiag( a );
V 3 rows 1 col (numeric)
5
5
5
1385 + 1386 + *-----------------; 1387 + * Subscripts ; 1388 + *-----------------; 1389 + 1390 + *-- Selecting rows or columns; 1391 + col1 = d[,1];
COL1 3 rows 1 col (numeric)
1
0
0
1392 + row2 = d[2,];
ROW2 1 row 3 cols (numeric)
0 2 0
1393 + row21 = d[{2 1},];
ROW21 2 rows 3 cols (numeric)
0 2 0
1 0 0
1394 + 1395 + *-- Selecting a submatrix; 1396 + print ab;
AB
2 2 0 0
2 2 0 0
0 0 1 1
0 0 1 1
1397 + row12 = ab[{1 2},];
ROW12 2 rows 4 cols (numeric)
2 2 0 0
2 2 0 0
1398 + a = ab[1:2, 1:2];
A 2 rows 2 cols (numeric)
2 2
2 2
1399 +
1400 + *-- Assigning a row, column or element;
1401 + a[1,] = {6 7};
A 2 rows 2 cols (numeric)
6 7
2 2
1402 + a[,2] = { 0,
1403 + 10};
A 2 rows 2 cols (numeric)
6 0
2 10
1404 + a[2,2]= 0;
A 2 rows 2 cols (numeric)
6 0
2 0
1405 +*page;
1406 + *----------------------------------;
1407 + * Subscript reduction operators ;
1408 + *----------------------------------;
1409 +
1410 + a = { 0 1 2, 5 4 3, 7 6 8 };
A 3 rows 3 cols (numeric)
0 1 2
5 4 3
7 6 8
1411 + 1412 + colsum = a[+,];
COLSUM 1 row 3 cols (numeric)
12 11 13
1413 + rowsum = a[,+];
ROWSUM 3 rows 1 col (numeric)
3
12
21
1414 + colmax = a[<>,];
COLMAX 1 row 3 cols (numeric)
7 6 8
1415 + rowmin = a[,><];
ROWMIN 3 rows 1 col (numeric)
0
3
6
1416 + colmean= a[:,];
COLMEAN 1 row 3 cols (numeric)
4 3.6666667 4.3333333
1417 + colprod= a[#,];
COLPROD 1 row 3 cols (numeric)
0 24 48
1418 + colssq = a[##,];
COLSSQ 1 row 3 cols (numeric)
74 53 77
1419 +*page;
1420 + *-----------------;
1421 + * Missing data ;
1422 + *-----------------;
1423 +
1424 + x = { 1 2 . ,
1425 + . 5 6 ,
1426 + 7 . 9 };
X 3 rows 3 cols (numeric)
1 2 .
. 5 6
7 . 9
1427 + y = { 4 . 2 ,
1428 + 2 1 3 ,
1429 + 6 . 5 };
Y 3 rows 3 cols (numeric)
4 . 2
2 1 3
6 . 5
1430 + 1431 + sum = x + y;
SUM 3 rows 3 cols (numeric)
5 . .
. 6 9
13 . 14
1432 + times = x # y;
TIMES 3 rows 3 cols (numeric)
4 . .
. 5 18
42 . 45
1433 + max = x <> y;
MAX 3 rows 3 cols (numeric)
4 2 2
2 5 6
7 . 9
1434 + *-- NB: missing treated as zero; 1435 + colsum= x[+,];
COLSUM 1 row 3 cols (numeric)
8 7 15
1436 +*page;
1437 + *--------------------------------------;
1438 + * Working with vectors and matrices ;
1439 + *--------------------------------------;
1440 +
1441 + coffee = { 4 2 2 3 2,
1442 + 3 3 1 2 2,
1443 + 2 1 0 4 5 };
COFFEE 3 rows 5 cols (numeric)
4 2 2 3 2
3 3 1 2 2
2 1 0 4 5
1444 + days = { Mon Tue Wed Thu Fri };
DAYS 1 row 5 cols (character, size 3)
MON TUE WED THU FRI
1445 + names = { 'Lenny', 'Linda', 'Sue'};
NAMES 3 rows 1 col (character, size 5)
Lenny
Linda
Sue
1446 + 1447 + print coffee;
COFFEE
4 2 2 3 2
3 3 1 2 2
2 1 0 4 5
1448 + print coffee[r=names c=days];
COFFEE MON TUE WED THU FRI
Lenny 4 2 2 3 2
Linda 3 3 1 2 2
Sue 2 1 0 4 5
1449 + 1450 + *-- Calculate daily and weekly cost at $.50/cup; 1451 + daycost = .50 # coffee;
DAYCOST 3 rows 5 cols (numeric)
2 1 1 1.5 1
1.5 1.5 0.5 1 1
1 0.5 0 2 2.5
1452 + 1453 + ones = j(5,1);
ONES 5 rows 1 col (numeric)
1
1
1
1
1
1454 + weektot = daycost * ones;
WEEKTOT 3 rows 1 col (numeric)
6.5
5.5
6
1455 + weektot = daycost[,+];
WEEKTOT 3 rows 1 col (numeric)
6.5
5.5
6
1456 + daytot = daycost[+,];
DAYTOT 1 row 5 cols (numeric)
4.5 3 1.5 4.5 4.5
1457 + total = daycost[+,+];
TOTAL 1 row 1 col (numeric)
18
1458 + 1459 + print coffee[r=names c=days] weektot[format=dollar7.2] , 1460 + daytot[c=days f=dollar8.2] ' ' total[f=dollar7.2];
COFFEE MON TUE WED THU FRI WEEKTOT
Lenny 4 2 2 3 2 $6.50
Linda 3 3 1 2 2 $5.50
Sue 2 1 0 4 5 $6.00
DAYTOT MON TUE WED THU FRI TOTAL
$4.50 $3.00 $1.50 $4.50 $4.50 $18.00
1461 + 1462 + rowmean = coffee[,:];
ROWMEAN 3 rows 1 col (numeric)
2.6
2.2
2.4
1463 + colmean = coffee[:,];
COLMEAN 1 row 5 cols (numeric)
3 2 1 3 3
1464 + 1465 + most = weektot[<>];
MOST 1 row 1 col (numeric)
6.5
1466 + index= weektot[<:>];
INDEX 1 row 1 col (numeric)
1
1467 + who = names[index];
WHO 1 row 1 col (character, size 5)
Lenny
1468 + 1469 + *-- Sum of squares and cross-products matrix; 1470 + n = nrow(coffee);
N 1 row 1 col (numeric)
3
1471 + sscp = coffee` * coffee;
SSCP 5 rows 5 cols (numeric)
29 19 11 26 24
19 14 7 16 15
11 7 5 8 6
26 16 8 29 30
24 15 6 30 33
1472 + 1473 + *-- Deviations from column means; 1474 + dev = coffee - J(n,1) * colmean;
DEV 3 rows 5 cols (numeric)
1 0 1 0 -1
0 1 0 -1 -1
-1 -1 -1 1 2
1475 + *-- Corrected SSCP matrix; 1476 + sscp= dev` * dev;
SSCP 5 rows 5 cols (numeric)
2 1 2 -1 -3
1 2 1 -2 -3
2 1 2 -1 -3
-1 -2 -1 2 3
-3 -3 -3 3 6
1477 + *-- Variance covariance matrix; 1478 + cov = sscp / (n-1);
COV 5 rows 5 cols (numeric)
1 0.5 1 -0.5 -1.5
0.5 1 0.5 -1 -1.5
1 0.5 1 -0.5 -1.5
-0.5 -1 -0.5 1 1.5
-1.5 -1.5 -1.5 1.5 3
1479 + *-- Standard deviations; 1480 + std = sqrt( diag(cov) );
STD 5 rows 5 cols (numeric)
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1.7320508
1481 + *-- Correlation matrix; 1482 + corr= inv(std) * cov * inv(std);
CORR 5 rows 5 cols (numeric)
1 0.5 1 -0.5 -0.866025
0.5 1 0.5 -1 -0.866025
1 0.5 1 -0.5 -0.866025
-0.5 -1 -0.5 1 0.8660254
-0.866025 -0.866025 -0.866025 0.8660254 1
1483 + print corr[r=days c=days f=7.2];
CORR MON TUE WED THU FRI
MON 1.00 0.50 1.00 -0.50 -0.87
TUE 0.50 1.00 0.50 -1.00 -0.87
WED 1.00 0.50 1.00 -0.50 -0.87
THU -0.50 -1.00 -0.50 1.00 0.87
FRI -0.87 -0.87 -0.87 0.87 1.00
1484 + quit;
Exiting IML.
NOTE: The PROCEDURE IML used 0.38 seconds. NOTE: %INCLUDE (level 1) ending.