Matrix Algebra in R A Minimal Introduction



Similar documents
Here are some examples of combining elements and the operations used:

Typical Linear Equation Set and Corresponding Matrices

Lecture 2 Matrix Operations

Data Mining: Algorithms and Applications Matrix Math Review

1 Introduction to Matrices

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS. + + x 2. x n. a 11 a 12 a 1n b 1 a 21 a 22 a 2n b 2 a 31 a 32 a 3n b 3. a m1 a m2 a mn b m

MATHEMATICS FOR ENGINEERS BASIC MATRIX THEORY TUTORIAL 2

Introduction to Matrix Algebra

6. Cholesky factorization

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Linear Algebra Review. Vectors

Review Jeopardy. Blue vs. Orange. Review Jeopardy

3.2 Matrix Multiplication

18.06 Problem Set 4 Solution Due Wednesday, 11 March 2009 at 4 pm in Total: 175 points.

AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

Similar matrices and Jordan form

Chapter 7. Matrices. Definition. An m n matrix is an array of numbers set out in m rows and n columns. Examples. (

December 4, 2013 MATH 171 BASIC LINEAR ALGEBRA B. KITCHENS

Question 2: How do you solve a matrix equation using the matrix inverse?

MAT188H1S Lec0101 Burbulla

13 MATH FACTS a = The elements of a vector have a graphical interpretation, which is particularly easy to see in two or three dimensions.

Abstract: We describe the beautiful LU factorization of a square matrix (or how to write Gaussian elimination in terms of matrix multiplication).

Multiple regression - Matrices

Excel supplement: Chapter 7 Matrix and vector algebra

Brief Introduction to Vectors and Matrices

Using row reduction to calculate the inverse and the determinant of a square matrix

Introduction to Matrices for Engineers

[1] Diagonal factorization

Section 5.3. Section 5.3. u m ] l jj. = l jj u j + + l mj u m. v j = [ u 1 u j. l mj

7. LU factorization. factor-solve method. LU factorization. solving Ax = b with A nonsingular. the inverse of a nonsingular matrix

Matrices 2. Solving Square Systems of Linear Equations; Inverse Matrices

Notes on Determinant

CHAPTER 8 FACTOR EXTRACTION BY MATRIX FACTORING TECHNIQUES. From Exploratory Factor Analysis Ledyard R Tucker and Robert C.

Chapter 6. Orthogonality

Math 1050 Khan Academy Extra Credit Algebra Assignment

Matrix Differentiation

Math 312 Homework 1 Solutions

Orthogonal Projections

Linear Algebra Notes for Marsden and Tromba Vector Calculus

A =

Math 115A HW4 Solutions University of California, Los Angeles. 5 2i 6 + 4i. (5 2i)7i (6 + 4i)( 3 + i) = 35i + 14 ( 22 6i) = i.

Beginner s Matlab Tutorial

7.4. The Inverse of a Matrix. Introduction. Prerequisites. Learning Style. Learning Outcomes

(!' ) "' # "*# "!(!' +,

T ( a i x i ) = a i T (x i ).

a 11 x 1 + a 12 x a 1n x n = b 1 a 21 x 1 + a 22 x a 2n x n = b 2.

Operation Count; Numerical Linear Algebra

5. Orthogonal matrices

Elementary Matrices and The LU Factorization

Lecture L3 - Vectors, Matrices and Coordinate Transformations

( ) which must be a vector

Mathematics Course 111: Algebra I Part IV: Vector Spaces

Linear Algebra: Vectors

Recall the basic property of the transpose (for any A): v A t Aw = v w, v, w R n.

LINEAR ALGEBRA. September 23, 2010

26. Determinants I. 1. Prehistory

Excel Guide for Finite Mathematics and Applied Calculus

Preface of Excel Guide

ISOMETRIES OF R n KEITH CONRAD

9 MATRICES AND TRANSFORMATIONS

Introduction to Matrix Algebra

University of Lille I PC first year list of exercises n 7. Review

Using Microsoft Excel Built-in Functions and Matrix Operations. EGN 1006 Introduction to the Engineering Profession

Eigenvalues, Eigenvectors, Matrix Factoring, and Principal Components

Linear Algebraic Equations, SVD, and the Pseudo-Inverse

Solving Linear Systems, Continued and The Inverse of a Matrix

Lecture 5: Singular Value Decomposition SVD (1)

x = + x 2 + x

v w is orthogonal to both v and w. the three vectors v, w and v w form a right-handed set of vectors.

Linearly Independent Sets and Linearly Dependent Sets

Solution to Homework 2

A linear combination is a sum of scalars times quantities. Such expressions arise quite frequently and have the form

SYSTEMS OF EQUATIONS AND MATRICES WITH THE TI-89. by Joseph Collison

Linear Algebra and TI 89

α = u v. In other words, Orthogonal Projection

Matrix Algebra. Some Basic Matrix Laws. Before reading the text or the following notes glance at the following list of basic matrix algebra laws.

Eigenvalues and Eigenvectors

Data Analysis Tools. Tools for Summarizing Data

Electrical Engineering 103 Applied Numerical Computing

Linear Algebra: Determinants, Inverses, Rank

F Matrix Calculus F 1

R: A self-learn tutorial

PTC Mathcad Prime 3.0 Keyboard Shortcuts

ROUND(cell or formula, 2)

Excel Basics By Tom Peters & Laura Spielman

Systems of Linear Equations

8 Square matrices continued: Determinants

Similarity and Diagonalization. Similar Matrices

Quick Tour of Mathcad and Examples

MAT 200, Midterm Exam Solution. a. (5 points) Compute the determinant of the matrix A =

Chapter 19. General Matrices. An n m matrix is an array. a 11 a 12 a 1m a 21 a 22 a 2m A = a n1 a n2 a nm. The matrix A has n row vectors

Torgerson s Classical MDS derivation: 1: Determining Coordinates from Euclidean Distances

Notes on Matrix Calculus

Notes on Symmetric Matrices

CS3220 Lecture Notes: QR factorization and orthogonal transformations

Abstract Algebra Cheat Sheet

Lecture 3: Finding integer solutions to systems of linear equations

Transcription:

A Minimal Introduction James H. Steiger Department of Psychology and Human Development Vanderbilt University Regression Modeling, 2009

1 Defining a Matrix in R Entering by Columns Entering by Rows Entering a Column or Row Vector 2 Extracting Individual Elements Extracting a Row of a Matrix Extracting a Column of a Matrix Extracting Several Rows and/or Columns 3 Joining Rows Joining Columns 4 Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion

Entering by Columns Entering by Rows Entering a Column or Row Vector Preliminary Comments This is a very basic introduction For some more challenging basics, you might examine Chapter 5 of An Introduction to R, the manual available from the Help->PDF Manuals menu selection in the R program

Entering by Columns Entering by Rows Entering a Column or Row Vector Preliminary Comments This is a very basic introduction For some more challenging basics, you might examine Chapter 5 of An Introduction to R, the manual available from the Help->PDF Manuals menu selection in the R program

Entering by Columns Entering by Rows Entering a Column or Row Vector Preliminary Comments This is a very basic introduction For some more challenging basics, you might examine Chapter 5 of An Introduction to R, the manual available from the Help->PDF Manuals menu selection in the R program

Defining a Matrix in R Entering by Columns Entering by Rows Entering a Column or Row Vector Entering a Matrix Suppose you wish to enter, then view the following matrix A in R ( ) 1 2 A = 3 4 You would use the R commands: > A matrix(c (1,3,2,4),2,2) > A [1,] 1 2 [2,] 3 4 Note that the numbers are, by default, entered into the matrix columnwise, i.e., by column

Entering by Columns Entering by Rows Entering a Column or Row Vector Defining a Matrix in R Entering a Matrix By Rows You can enter the numbers by row, simply by adding an optional input variable Here are the R commands: > A matrix(c (1,2,3,4),2,2, byrow = TRUE ) > A [1,] 1 2 [2,] 3 4

Entering by Columns Entering by Rows Entering a Column or Row Vector Entering a Column Vector Entering a Column Vector To enter a p 1 column vector, simply enter a p 1 matrix > a matrix(c (1,2,3,4),4,1) > a [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 Row vectors are, likewise, entered as 1 q matrices

Extracting Individual Elements Extracting Individual Elements Extracting a Row of a Matrix Extracting a Column of a Matrix Extracting Several Rows and/or Columns Extracting Individual Elements Individual elements of a matrix are referred to by their subscripts For example, consider a matrix correlation matrix R given below To extract element R 3,1, we simply request R[3,1] 1 2 3 4 1 1.00 0.40 0.30 0.30 2 0.40 1.00 0.20 0.20 3 0.30 0.20 1.00 0.30 4 0.30 0.20 0.30 1.00 > R[3,1] [1] 0.3

Extracting a Row of a Matrix Extracting Individual Elements Extracting a Row of a Matrix Extracting a Column of a Matrix Extracting Several Rows and/or Columns Extracting a Row of a Matrix To get an entire row of a matrix, you name the row and leave out the column For example, in the matrix R below, to get the first row, just enter R[1,] 1 2 3 4 1 1.00 0.40 0.30 0.30 2 0.40 1.00 0.20 0.20 3 0.30 0.20 1.00 0.30 4 0.30 0.20 0.30 1.00 > R[1,] [1] 1.0 0.4 0.3 0.3

Extracting a Column of a Matrix Extracting Individual Elements Extracting a Row of a Matrix Extracting a Column of a Matrix Extracting Several Rows and/or Columns Extracting a Column of a Matrix To get an entire column of a matrix, you name the column and leave out the row For example, in the matrix R below, to get the first column, just enter R[,1] 1 2 3 4 1 1.00 0.40 0.30 0.30 2 0.40 1.00 0.20 0.20 3 0.30 0.20 1.00 0.30 4 0.30 0.20 0.30 1.00 > R[,1] [1] 1.0 0.4 0.3 0.3

Extracting Individual Elements Extracting a Row of a Matrix Extracting a Column of a Matrix Extracting Several Rows and/or Columns Extracting Several Rows and/or Columns Example (Extracting Several Rows and/or Columns) Examine the following examples to see how we can extract any specified range of rows and/or columns 1 2 3 4 1 1.00 0.40 0.30 0.30 2 0.40 1.00 0.20 0.20 3 0.30 0.20 1.00 0.30 4 0.30 0.20 0.30 1.00 > R[1:3,] [,3] [,4] [1,] 1.0 0.4 0.3 0.3 [2,] 0.4 1.0 0.2 0.2 [3,] 0.3 0.2 1.0 0.3 > R[1:3,2:4] [,3] [1,] 0.4 0.3 0.3 [2,] 1.0 0.2 0.2 [3,] 0.2 1.0 0.3

Joining Rows Joining Columns Joining Rows Joining Rows On occasion, we need to build up matrices from smaller parts You can combine several matrices with the same number of columns by joining them as rows, using the rbind() command Here is an example

Joining Rows Joining Columns Joining Rows Example (Joining Rows) > A matrix(c (1,3,3,9,6,5),2,3) > B matrix(c (9,8,8,2,9,0),2,3) > A [,3] [1,] 1 3 6 [2,] 3 9 5 > B [,3] [1,] 9 8 9 [2,] 8 2 0 > rbind (A,B) [,3] [1,] 1 3 6 [2,] 3 9 5 [3,] 9 8 9 [4,] 8 2 0 > rbind (B,A) [,3] [1,] 9 8 9 [2,] 8 2 0 [3,] 1 3 6 [4,] 3 9 5

Joining Rows Joining Columns Joining Columns Joining Columns In similar fashion, you can combine several matrices with the same number of rows by joining them as columnss, using the cbind() command Here is an example

Joining Columns Defining a Matrix in R Joining Rows Joining Columns Example (Joining Columns) > A matrix(c (1,3,3,9,6,5),2,3) > B matrix(c (9,8,8,2,9,0),2,3) > A [,3] [1,] 1 3 6 [2,] 3 9 5 > B [,3] [1,] 9 8 9 [2,] 8 2 0 > cbind(a,b) [,3] [,4] [,5] [,6] [1,] 1 3 6 9 8 9 [2,] 3 9 5 8 2 0 > cbind(b,a) [,3] [,4] [,5] [,6] [1,] 9 8 9 1 3 6 [2,] 8 2 0 3 9 5

Matrix Addition and Subtraction Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion Adding or subtracting matrices is natural and straightforward, as the example below shows Example > A matrix(c (1,3,3,9),2,2) > B matrix(c (9,8,8,2),2,2) > A [1,] 1 3 [2,] 3 9 > B [1,] 9 8 [2,] 8 2 > A+B [1,] 10 11 [2,] 11 11 > A-B [1,] -8-5 [2,] -5 7

Scalar Multiplication Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion To multiply a matrix by a scalar, simply use the multiplication symbol * For example, Example (Scalar Multiplication) > A [1,] 1 3 [2,] 3 9 > 3 *A [1,] 3 9 [2,] 9 27

Matrix Multiplication Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion Matrix multiplication uses the %*% command Example (Matrix Multiplication) > A [1,] 1 3 [2,] 3 9 > B [1,] 9 8 [2,] 8 2 > A % *% B [1,] 33 14 [2,] 99 42 > B % *% A [1,] 33 99 [2,] 14 42

Matrix Transposition Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion To transpose a matrix, use the t() command Example (Transposing a matrix) > A [,3] [1,] 1 3 6 [2,] 3 9 5 > B [,3] [1,] 9 8 9 [2,] 8 2 0 > t (A) [1,] 1 3 [2,] 3 9 [3,] 6 5 > t (B) [1,] 9 8 [2,] 8 2 [3,] 9 0

Matrix Inversion Defining a Matrix in R Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion Matrix Inversion To invert a square matrix, use the solve() command In the example below, we illustrate a common problem numbers that are really zero are only very close to zero due to rounding error When we compute the product AA 1, we should get the identity matrix I, but instead we see that the off-diagonal elements are not quite zero. To cure this problem, you can use the zapsmall() function

Matrix Inversion Defining a Matrix in R Matrix Addition and Subtraction Scalar Multiplication Matrix Multiplication Matrix Transposition Matrix Inversion Example (Inverting a matrix) > A [,3] [1,] 1 9 9 [2,] 3 6 1 [3,] 3 5 8 > solve (A) [,3] [1,] -0.24855491 0.1560694 0.2601156 [2,] 0.12138728 0.1098266-0.1502890 [3,] 0.01734104-0.1271676 0.1213873 > A % *% solve (A) [,3] [1,] 1.000000e+00 2.775558e-17-9.714451e-17 [2,] -4.510281e-17 1.000000e+00-4.163336e-17 [3,] -2.775558e-17-2.220446e-16 1.000000e+00 > zapsmall ( A % *% solve (A) ) [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1