Basic matrix math in R

Similar documents
Interlude: Solving systems of Equations

II.Matrix. Creates matrix, takes a vector argument and turns it into a matrix matrix(data, nrow, ncol, byrow = F)

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

EN 001-4: Introduction to Computational Design. Matrices & vectors. Why do we care about vectors? What is a matrix and a vector?

Computational Methods CMSC/AMSC/MAPL 460. Vectors, Matrices, Linear Systems, LU Decomposition, Ramani Duraiswami, Dept. of Computer Science

Solve the matrix equation AX B for X by using A.(1-3) Use the Inverse Matrix Calculator Link to check your work

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Matrices 4: use of MATLAB

3. Replace any row by the sum of that row and a constant multiple of any other row.

Computational Methods CMSC/AMSC/MAPL 460. Linear Systems, LU Decomposition, Ramani Duraiswami, Dept. of Computer Science

Table of Laplace Transforms

Finite Math - J-term Homework. Section Inverse of a Square Matrix

Teaching Manual Math 2131

x = 12 x = 12 1x = 16

Introduction to Matlab

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Computational Methods CMSC/AMSC/MAPL 460. Vectors, Matrices, Linear Systems, LU Decomposition, Ramani Duraiswami, Dept. of Computer Science

Matlab Introduction. Scalar Variables and Arithmetic Operators

The Singular Value Decomposition: Let A be any m n matrix. orthogonal matrices U, V and a diagonal matrix Σ such that A = UΣV T.

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Grade 6 Math Circles November 6 & Relations, Functions, and Morphisms

Introduction to Matlab

The inverse of a matrix

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

For example, the system. 22 may be represented by the augmented matrix

Standard Chapter/Unit Notes

Basic Matrix Manipulation with a TI-89/TI-92/Voyage 200

Math 308 Autumn 2016 MIDTERM /18/2016

Maths for Signals and Systems Linear Algebra in Engineering. Some problems by Gilbert Strang

0_PreCNotes17 18.notebook May 16, Chapter 12

Performing Matrix Operations on the TI-83/84

Homework 1 Description CmpE 362 Spring Instructor : Fatih Alagoz Teaching Assistant : Yekta Said Can Due: 3 March, 23:59, sharp

Robot Mapping. Least Squares Approach to SLAM. Cyrill Stachniss

Graphbased. Kalman filter. Particle filter. Three Main SLAM Paradigms. Robot Mapping. Least Squares Approach to SLAM. Least Squares in General

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

Image Manipulation in MATLAB Due Monday, July 17 at 5:00 PM

MATH 5520 Basics of MATLAB

MATH 3511 Basics of MATLAB

CSC-140 Assignment 5

University of Alberta

AH Matrices.notebook November 28, 2016

Derek Bridge School of Computer Science and Information Technology University College Cork

Finding the Volume of Solids

Matlab and Octave: Quick Introduction and Examples 1 Basics

Lab #10 Multi-dimensional Arrays

LAB #2: SAMPLING, SAMPLING DISTRIBUTIONS, AND THE CLT

Solving Systems of Equations Using Matrices With the TI-83 or TI-84

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

MATLAB GUIDE UMD PHYS401 SPRING 2012

Lecture 2: Variables, Vectors and Matrices in MATLAB

1 Introduction to Matlab

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Geometric Transformations

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Therefore, after becoming familiar with the Matrix Method, you will be able to solve a system of two linear equations in four different ways.

MATLAB GUIDE UMD PHYS401 SPRING 2011

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

1. (0 points) Write your name! READ cipher and english notes. P (5) =.05 P (6) =.1 P (7) =.15 P (9) =.25

John's Tutorial on Everyday Mathcad (Version 9/2/09) Mathcad is not the specialist's ultimate mathematical simulator

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

APPM 2360 Lab #2: Facial Recognition

Teachers Teaching with Technology (Scotland) Teachers Teaching with Technology. Scotland T 3. Matrices. Teachers Teaching with Technology (Scotland)

PetShop (BYU Students, SIGGRAPH 2006)

CS1114 Assignment 5, Part 1

Homework #2: Introduction to Images Due 4 th Week of Spring 2018 at the start of lab CSE 7, Spring 2018

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Addition/Subtraction flops. ... k k + 1, n (n k)(n k) (n k)(n + 1 k) n 1 n, n (1)(1) (1)(2)

Logical operators: R provides an extensive list of logical operators. These include

MITOCW ocw f99-lec12_300k

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

MATLAB: The greatest thing ever. Why is MATLAB so great? Nobody s perfect, not even MATLAB. Prof. Dionne Aleman. Excellent matrix/vector handling

To add or subtract, just add or subtract the numbers in the same column and row and place answer accordingly.

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

MATLAB TUTORIAL WORKSHEET

CHAPTER 5 SYSTEMS OF EQUATIONS. x y

Introduction to R. UCLA Statistical Consulting Center R Bootcamp. Irina Kukuyeva September 20, 2010

Matrix algebra. Basics

Matrix Inverse 2 ( 2) 1 = 2 1 2

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

MITOCW ocw f99-lec07_300k

Using Microsoft Excel

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

Exercise Set Decide whether each matrix below is an elementary matrix. (a) (b) (c) (d) Answer:

Introduction to Matlab

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Linear Equation Systems Iterative Methods

SPRITES Moving Two At the Same Using Game State

Chapter 1: Number and Operations

How to learn MATLAB? Some predefined variables

GLY Geostatistics Fall Lecture 2 Introduction to the Basics of MATLAB. Command Window & Environment

Get to Know Your Calculator!

Paul's Online Math Notes. Online Notes / Algebra (Notes) / Systems of Equations / Augmented Matricies

Introduction to MATLAB

Howto Use Octave Functions in C/C++ Programs

POL 345: Quantitative Analysis and Politics

Lecture 5: Matrices. Dheeraj Kumar Singh 07CS1004 Teacher: Prof. Niloy Ganguly Department of Computer Science and Engineering IIT Kharagpur

Transcription:

1 Basic matrix math in R This chapter reviews the basic matrix math operations that you will need to understand the course material and how to do these operations in R. 1.1 Creating matrices in R Create a 3 4 matrix, meaning 3 row and 4 columns, that is all 1s: matrix(1, 3, 4) [,4] [1,] 1 1 1 1 [2,] 1 1 1 1 [3,] 1 1 1 1 Create a 3 4 matrix filled in with the numbers 1 to 12 by column (default) and by row: matrix(1:12, 3, 4) [,4] [1,] 1 4 7 10 [2,] 2 5 8 11 [3,] 3 6 9 12 matrix(1:12, 3, 4, byrow=true) [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 Create a matrix with one column: matrix(1:4, ncol=1)

2 1 Matrix math [,1] [1,] 1 [2,] 2 [3,] 3 [4,] 4 Create a matrix with one row: matrix(1:4, nrow=1) [,4] [1,] 1 2 3 4 Check the dimensions of a matrix =matrix(1:6, 2,3) [1,] 1 3 5 [2,] 2 4 6 dim() [1] 2 3 Get the number of rows in a matrix: dim()[1] [1] 2 nrow() [1] 2 Create a 3D matrix (called array): =array(1:6, dim=c(2,3,2)),, 1 [1,] 1 3 5 [2,] 2 4 6,, 2 [1,] 1 3 5 [2,] 2 4 6

1.2 Matrix multiplication, addition and transpose 3 dim() [1] 2 3 2 Check if an object is a matrix. dataframe is not a matrix. vector is not a matrix. =matrix(1:4, 1, 4) [,4] [1,] 1 2 3 4 class() [1] "matrix" B=data.frame() B X1 X2 X3 X4 1 1 2 3 4 class(b) [1] "data.frame" C=1:4 C [1] 1 2 3 4 class(c) [1] "integer" 1.2 Matrix multiplication, addition and transpose You will need to be very solid in matrix multiplication for the course. If you haven t done it in awhile, google matrix multiplication youtube and you find lots of 5min videos to remind you. In R, you use the %*% operation to do matrix multiplication. When you do matrix multiplication, the columns of the matrix on the left must equal the rows of the matrix on the right. The result is a matrix that has the number of rows of the matrix on the left and number of columns of the matrix on the right. (n m)(m p) = (n p)

4 1 Matrix math =matrix(1:6, 2, 3) #2 rows, 3 columns B=matrix(1:6, 3, 2) #3 rows, 2 columns %*%B #this works [,1] [,2] [1,] 22 49 [2,] 28 64 B%*% #this works [1,] 9 19 29 [2,] 12 26 40 [3,] 15 33 51 try(b%*%b) #this doesn't To add two matrices use +. The matrices have to have the same dimensions. + #works [1,] 2 6 10 [2,] 4 8 12 +t(b) #works [1,] 2 5 8 [2,] 6 9 12 try(+b) #does not work since has 2 rows and B has 3 The transpose of a matrix is denoted or. To transpose a matrix in R, you use t(). =matrix(1:6, 2, 3) #2 rows, 3 columns t() #is the transpose of [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 try(%*%) #this won't work %*%t() #this will [,1] [,2] [1,] 35 44 [2,] 44 56

1.3 Subsetting a matrix 5 1.3 Subsetting a matrix To subset a matrix, we use [ ]: =matrix(1:9, 3, 3) #3 rows, 3 columns #get the first and second rows of #it's a 2x3 matrix [1:2,] [1,] 1 4 7 [2,] 2 5 8 #get the top 2 rows and left 2 columns [1:2,1:2] [,1] [,2] [1,] 1 4 [2,] 2 5 #What does this do? [c(1,3),c(1,3)] [,1] [,2] [1,] 1 7 [2,] 3 9 #This? [c(1,2,1),c(2,3)] [,1] [,2] [1,] 4 7 [2,] 5 8 [3,] 4 7 If you have used matlab, you know you can say something like [1,end] to denote the element of a matrix in row 1 and the last column. R does not have end. To do, the same in R you do something like: =matrix(1:9, 3, 3) [1,ncol()] [1] 7 #or [1,dim()[2]] [1] 7

6 1 Matrix math Warning R will create vectors from subsetting matrices! One of the really bad things that R does with matrices is create a vector if you happen to subset a matrix to create a matrix with 1 row or 1 column. Look at this: =matrix(1:9, 3, 3) #take the first 2 rows B=[1:2,] #everything is ok dim(b) [1] 2 3 class(b) [1] "matrix" #take the first row B=[1,] #oh no! It should be a 1x3 matrix but it is not. dim(b) NULL #It is not even a matrix any more class(b) [1] "integer" #and what happens if we take the transpose? #Oh no, it's a 1x3 matrix not a 3x1 (transpose of 1x3) t(b) [1,] 1 4 7 #%*%B should fail because is (3x3) and B is (1x3) %*%B [,1] [1,] 66 [2,] 78 [3,] 90 #It works? That is horrible! This will create hard to find bugs in your code because you will look at B=[1,] and everything looks fine. Why is R saying it is not a matrix! To stop R from doing this use drop=flse.

B=[1,,drop=FLSE] #Now it is a matrix as it should be dim(b) [1] 1 3 class(b) [1] "matrix" 1.4 Replacing elements in a matrix 7 #this fails as it should (alerting you to a problem!) try(%*%b) 1.4 Replacing elements in a matrix Replace 1 element. =matrix(1, 3, 3) [1,1]=2 [1,] 2 1 1 [2,] 1 1 1 [3,] 1 1 1 Replace a row with all 1s or a string of values =matrix(1, 3, 3) [1,]=2 [1,] 2 2 2 [2,] 1 1 1 [3,] 1 1 1 [1,]=1:3 [1,] 1 2 3 [2,] 1 1 1 [3,] 1 1 1 Replace group of elements. This often does not work as one expects so be sure look at your matrix after trying something like this. Here I want to replace elements (1,3) and (3,1) with 2, but it didn t work as I wanted.

8 1 Matrix math =matrix(1, 3, 3) [c(1,3),c(3,1)]=2 [1,] 2 1 2 [2,] 1 1 1 [3,] 2 1 2 How do I replace elements (1,1) and (3,3) with 2 then? It s tedious. If you have a lot of elements to replace, you might want to use a for loop. =matrix(1, 3, 3) [1,3]=2 [3,1]=2 [1,] 1 1 2 [2,] 1 1 1 [3,] 2 1 1 1.5 Diagonal matrices and identity matrices diagonal matrix is one that is square, meaning number of rows equals number of columns, and it has 0s on the off-diagonal and non-zeros on the diagonal. In R, you form a diagonal matrix with the diag() function: diag(1,3) #put 1 on diagonal of 3x3 matrix [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1 diag(2, 3) #put 2 on diagonal of 3x3 matrix [1,] 2 0 0 [2,] 0 2 0 [3,] 0 0 2 diag(1:4) #put 1 to 4 on diagonal of 4x4 matrix [,4] [1,] 1 0 0 0 [2,] 0 2 0 0 [3,] 0 0 3 0 [4,] 0 0 0 4

1.5 Diagonal matrices and identity matrices 9 The diag() function can also be used to replace elements on the diagonal of a matrix: =matrix(3, 3, 3) diag()=1 [1,] 1 3 3 [2,] 3 1 3 [3,] 3 3 1 =matrix(3, 3, 3) diag()=1:3 [1,] 1 3 3 [2,] 3 2 3 [3,] 3 3 3 =matrix(3, 3, 4) diag([1:3,2:4])=1 [,4] [1,] 3 1 3 3 [2,] 3 3 1 3 [3,] 3 3 3 1 The diag function is also used to get the diagonal of a matrix. =matrix(1:9, 3, 3) diag() [1] 1 5 9 The identity matrix is a special kind of diagonal matrix with 1s on the diagonal. It is denoted I. I 3 would mean a 3 3 diagonal matrix. identity matrix has the property that I = and I = so it is like a 1. =matrix(1:9, 3, 3) I=diag(3) #shortcut for 3x3 identity matrix %*%I [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9

10 1 Matrix math 1.6 Taking the inverse of a square matrix The inverse of a matrix is denoted 1. You can think of the inverse of a matrix like 1/a. 1/a a = 1. 1 = 1 = I. The inverse of a matrix does not always exist; for one it has to be square. We ll be using inverses for variance-covariance matrices and by definition (of a variance-covariance matrix), the inverse of those exist. In R, there are a couple way common ways to take the inverse of a variance-covariance matrix (or something with the same properties). solve is the most common probably: =diag(3,3)+matrix(1,3,3) inv=solve() inv%*% [1,] 1.000000e+00-6.938894e-18 0 [2,] 2.081668e-17 1.000000e+00 0 [3,] 0.000000e+00 0.000000e+00 1 %*%inv [1,] 1.000000e+00-6.938894e-18 0 [2,] 2.081668e-17 1.000000e+00 0 [3,] 0.000000e+00 0.000000e+00 1 nother option is to use chol2inv which uses a Cholesky decomposition 1 : =diag(3,3)+matrix(1,3,3) inv=chol2inv(chol()) inv%*% [1,] 1.000000e+00 6.938894e-17 0.000000e+00 [2,] 2.081668e-17 1.000000e+00-2.775558e-17 [3,] -5.551115e-17 0.000000e+00 1.000000e+00 %*%inv [1,] 1.000000e+00 2.081668e-17-5.551115e-17 [2,] 6.938894e-17 1.000000e+00 0.000000e+00 [3,] 0.000000e+00-2.775558e-17 1.000000e+00 For the purpose of this course, solve is fine. 1 The Cholesky decomposition is a handy way to keep your variance-covariance matrices valid when doing a parameter search. Don t search over the raw variancecovariance matrix. Search over a matrix where the lower triangle is 0, that is what a Cholesky decomposition looks like. Let s call it B. Your variance-covariance matrix is t(b)%*%b.

11 Problems 1.1 Build a 4 3 matrix with the numbers 1 through 4 in each row. 1.2 Extract the elements in the 1st and 2nd rows and 1st and 2nd columns (you ll have a 2 2 matrix). Show the R code that will do this. 1.3 Build a 4 3 matrix with the numbers 1 through 12 by row (meaning the first row will have the numbers 1 through 4 in it). 1.4 Extract the 3rd row of the above. Show R code to do this where you end up with a vector and how to do this where you end up with a 1 3 matrix. 1.5 Build a 4 3 matrix that is all 1s except a 2 in the (2,3) element (2nd row, 3rd column). 1.6 Take the transpose of the above. 1.7 Build a 4 4 diagonal matrix with 1 through 4 on the diagonal. 1.8 Build a 5 5 identity matrix. 1.9 Replace the diagonal in the above matrix with 2 (the number 2). 1.10 Build a matrix with 2 on the diagonal and 1s on the offdiagonals. 1.11 Take the inverse of the above. 1.12 Build a 3 3 matrix with the first 9 letters of the alphabet. First column should be a, b, c. letters[1:9] gives you these letters. 1.13 Replace the diagonal of this matrix with the word cat. 1.14 Build a 4 3 matrix with all 1s. Multiply by a 3 4 matrix with all 2s. 1.15 If is a 4 3 matrix, is possible? Is possible? Show how to write in R. ] 1.16 In the equation, B = C, let =. Build a B matrix with only 1s [ 1 4 7 2 5 8 3 6 9 and 0s such that the values on the diagonal of C are 1, 8, 6 (in that order). Show your R code for, B and B. 1.17 Same matrix as above and same equation ] B = C. Build a 3 3 B matrix such that C = 2. So C =. Hint, B is diagonal. [ 2 8 14 4 10 16 6 12 18 1.18 Same and B = C equation. Build a B matrix to compute the row sums of. So the first row sum would be 1 + 4 + 7, the sum of all elements in

12 1 Matrix math row 1 of. C will be (1 column). [ 12 ] 15, the row sums of. Hint, B is a column matrix 18 1.19 Same matrix as above but now equation B = C. Build a B matrix to compute the column sums of. So the first column sum would be 1 + 2 + 3. C will be a 1 3 matrix. [ 2 1 1 ] 1.20 Let B = C equation but = 1 2 1 (so =diag(3)+1). Build a B matrix [ 1 1 2 33 ] such that C =. Hint, you need to use the inverse of. 3