R Lecture 4 Naomi Altman Department of Statistics Department of Statistics (Based on notes by J. Lee)

Slides:



Advertisements
Similar presentations
Identity and Inverse Matrices
Advertisements

Generalised Inverses Modal Analysis and Modal Testing S. Ziaei Rad.
Matrices A matrix is a rectangular array of quantities (numbers, expressions or function), arranged in m rows and n columns x 3y.
Refresher: Vector and Matrix Algebra Mike Kirkpatrick Department of Chemical Engineering FAMU-FSU College of Engineering.
Maths for Computer Graphics
Examples of Two- Dimensional Systolic Arrays. Obvious Matrix Multiply Rows of a distributed to each PE in row. Columns of b distributed to each PE in.
The Definition of a Group!!! Come to the talk today!!!!! Statistics Val 103 3:30 – 4:15.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
數位控制(九).
What is entry A in the matrix multiplication: ( a) 1 (b) -2(c) 5 (d) 11(e) 13 (f) 0.
Pam Perlich Urban Planning 5/6020
Matrices MSU CSE 260.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
Section 10.3 – The Inverse of a Matrix No Calculator.
1 Chapter 2 Matrices Matrices provide an orderly way of arranging values or functions to enhance the analysis of systems in a systematic manner. Their.
Chapter 7 Matrix Mathematics Matrix Operations Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Using Inverse Matrices Solving Systems. You can use the inverse of the coefficient matrix to find the solution. 3x + 2y = 7 4x - 5y = 11 Solve the system.
1 Statistical Analysis Professor Lynne Stokes Department of Statistical Science Lecture 5QF Introduction to Vector and Matrix Operations Needed for the.
How to find the inverse of a matrix
Matrix Equations Step 1: Write the system as a matrix equation. A three-equation system is shown below.
Computer Vision Group Prof. Daniel Cremers Autonomous Navigation for Flying Robots Lecture 2.1: Recap on Linear Algebra Daniel Cremers Technische Universität.
Algebra 2: Lesson 5 Using Matrices to Organize Data and Solve Problems.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
Matrices Addition & Subtraction Scalar Multiplication & Multiplication Determinants Inverses Solving Systems – 2x2 & 3x3 Cramer’s Rule.
Do Now: Evaluate: 3AB. Algebra II 3.7: Evaluate Determinants HW: p.207 (4-14 even) Test : Friday, 12/6.
Lecture 28: Mathematical Insight and Engineering.
4.7 Identity and Inverse Matrices and Solving Systems of Equations Objectives: 1.Determine whether two matrices are inverses. 2.Find the inverse of a 2x2.
Advanced Trig Exam Review Day Three: Matrices. Solving Systems of Equations.
Inverse and Identity Matrices Can only be used for square matrices. (2x2, 3x3, etc.)
Linear algebra: matrix Eigen-value Problems Eng. Hassan S. Migdadi Part 1.
Section 10.3 and Section 9.3 Systems of Equations and Inverses of Matrices.
ME 142 Engineering Computation I Matrix Operations in Excel.
3.6 Solving Systems Using Matrices You can use a matrix to represent and solve a system of equations without writing the variables. A matrix is a rectangular.
Sec 4.1 Matrices.
2.2 The Inverse of a Matrix. Example: REVIEW Invertible (Nonsingular)
Matrices and Matrix Operations. Matrices An m×n matrix A is a rectangular array of mn real numbers arranged in m horizontal rows and n vertical columns.
2.5 DETERMINANTS AND MULTIPLICATIVE INVERSES OF MATRICES By the end of the section students will evaluate determinants, find inverses of matrices, and.
Use Inverse Matrices to Solve Linear Systems Objectives 1.To find the inverse of a square matrix 2.To solve a matrix equation using inverses 3.To solve.
LEARNING OUTCOMES At the end of this topic, student should be able to :  D efination of matrix  Identify the different types of matrices such as rectangular,
Linear System of Simultaneous Equations Warm UP First precinct: 6 arrests last week equally divided between felonies and misdemeanors. Second precinct:
Matrix Algebra Basics Chapter 3 Section 5. Algebra.
Matrices. Variety of engineering problems lead to the need to solve systems of linear equations matrixcolumn vectors.
1 Matrix Math ©Anthony Steed Overview n To revise Vectors Matrices.
MTH108 Business Math I Lecture 20.
Introduction to Vectors and Matrices
Use Inverse Matrices to Solve Linear Systems
12-1 Organizing Data Using Matrices
Linear Algebra review (optional)
Chapter 7 Matrix Mathematics
Review Problems Matrices
Naomi Altman Department of Statistics (Based on notes by J. Lee)
An Introduction to Maple
الوحدة السابعة : المصفوفات . تنظيم البيانات فى مصفوفات . الوحدة السابعة : المصفوفات . تنظيم البيانات فى مصفوفات . 1 جمع المصفوفات وطرحها.
RECORD. RECORD Gaussian Elimination: derived system back-substitution.
Section 6.4 Multiplicative Inverses of Matices and Matrix Equations
Introduction to MATLAB Lecture 02
Solving Linear Systems Using Inverse Matrices
CSCI N207 Data Analysis Using Spreadsheet
Multiplicative Inverses of Matrices and Matrix Equations
Inverse & Identity MATRICES Last Updated: October 12, 2005.
Section 3.3 – The Inverse of a Matrix
Section 9.4 Multiplicative Inverses of Matices and Matrix Equations
Multidimensional array
ARRAY DIVISION Identity matrix Islamic University of Gaza
Introduction to Vectors and Matrices
All we need in Game Programming Course Reference
Linear Algebra review (optional)
1.8 Matrices.
1.8 Matrices.
The Elements of Linear Algebra
Presentation transcript:

R Lecture 4 Naomi Altman Department of Statistics Department of Statistics (Based on notes by J. Lee)

Matrix Computations matrix creates matrices array creates higher dimensional arrays arrays binary operations such as +, -, *, / >,,< are done element-wise, and create a new matrix or array the same size as the larger array are done element-wise, and create a new matrix or array the same size as the larger array

More Matrix Operations more matrix operations %*% matrix multiplication t transpose Also: various matrix decompositions matrix types (e.g. diag) matrix types (e.g. diag) matrix functions such as determinant, matrix functions such as determinant, eigenvalues eigenvalues inverse via the "solve" function inverse via the "solve" function

Session: Arrays + Matrices z <- 1:150 z <- 1:150 a <- array(z,dim=c(3,5,10)) a <- array(z,dim=c(3,5,10)) dim(z) <- c(3,5,10) dim(z) <- c(3,5,10)azmatrix(1:6,nrow=2,ncol=3) matrix(1:6,nrow=2, ncol=3, byrow=T)

x <- matrix(0,nc=5,nr=5); i <- matrix(1:4,5,2); x[i] =1 matrix(1:6, 3, 2)*matrix(1:6*2, 3, 2) X = matrix(1:6,3,2) y = 1:3 t(X) %*% y A <- matrix(c(1,1,2,3),2,2) b <- c(2,5) solve(A,b)diag(rep(1,2))solve(A,diag(rep(1,2)))

Control Structures for (x in set){operations} while (x in condition){operations} repeat {operations, test, break} if (condition) {operations} else {more operations} else {more operations}switch(flag,dolist)

Control Structures: Session 1 x <- 1:9 if (length(x) <= 10) { x <- c(x,10:20);print(x) } x <- c(x,10:20);print(x) } if (length(x) < 5) print(x) else print(x[5:20]) else print(x[5:20]) for (i in x) i for(i in x) {print(i)} y=c("a","b","hi there") for (i in y) i for (i in y) print(i)

j <- 1 while( j < 10) { print(j) print(j) j <- j + 2 } j <- j + 2 } j < -1 repeat { print(j) print(j) j <- j + j/3 j <- j + j/3 if (j > 30) break if (j > 30) break}

citizen="uk" switch(citizen,au="Aussie",uk="Brit",us="Y ankee",ca="Canuck")

Functions Functions are stored like data. my.function=function(arguments){operations} I write functions for just about anything I need to do more than once - I run through the commands once interactively, and then use the history() feature and an editor to create the function. It is wise to include a comment at the start of each function to say what it does and to document functions of more than a few lines.

e.g. makeList=function(mat,i=2){ #change a matrix into a list of rows or columns mylist=list() mylist=list() m=switch(i,t(mat),mat) m=switch(i,t(mat),mat) for (i in 1:ncol(m)){ for (i in 1:ncol(m)){ mylist[[i]]=m[,i] mylist[[i]]=m[,i] } mylist mylist }

Calling Conventions for Functions  Arguments may be specified in the same order in which they occur in function definition, in which case the values are supplied in order.  Arguments may be specified as name=value, when the order in which the arguments appear is irrelevant.  Above two rules can be mixed. t.test(x1, y1, var.equal=F, conf.level=.99) t.test(x1, y1, var.equal=F, conf.level=.99) t.test(var.equal=F, conf.level=.99, x1, y1) t.test(var.equal=F, conf.level=.99, x1, y1)

Default values When creating a function, the programmer can supply default values. makeList=function(mat,i=2){} means that by default, i=2. The user can change the value when calling the function myRowList=makeList(mymat,1)

Missing Arguments R functions can handle missing arguments two ways either by providing a default expression in the argument list of definition, or by testing explicitly for missing arguments. R functions can handle missing arguments two ways either by providing a default expression in the argument list of definition, or by testing explicitly for missing arguments.

Session: Missing Arguments add <- function(x,y=0){#adds 2 numbers x + y} x + y}add(4) add <- function(x,y){#adds 2 numbers if(missing(y)) x add <- function(x,y){#adds 2 numbers if(missing(y)) x else x+y else x+y} add(4) add(4)

Variable Number of Arguments  The special argument name “…” in the function definition will match any number of arguments in the call.  nargs() returns the number of arguments in the current call.

Variable Number of Arguments mean.of.all <- function(…) mean(c(…)) mean.of.all(1:10,20:100,12:14) mean.of.means <- function(…){ means <- numeric() means <- numeric() for(x in list(…)) means <- c(means,mean(x)) for(x in list(…)) means <- c(means,mean(x)) mean(means) mean(means)}

Session 2: Variable Number of Arguments mean.of.means <- function(…){ #computes the mean of the means of the # arguments n <- nargs() n <- nargs() means <- numeric(n) means <- numeric(n) all.x <- list(…) all.x <- list(…) for(j in 1:n) means[j] <- mean(all.x[[j]]) for(j in 1:n) means[j] <- mean(all.x[[j]]) mean(means) mean(means)}mean.of.means(1:10,10:100)

Variables are local Note that any ordinary assignments done within the function are local and temporary and lost after exit from the function. fun1 <- function(){ a <- 1:10 a <- 1:10 rnorm(10) rnorm(10)}a

Output from a function The last line of a function should be the output. myfun=function(x){ y=3*x y=3*x y} y}myfun=function(x){ y=3*x y=3*x list(x=x,y=y) list(x=x,y=y) }

Editfunname=edit(myfun) brings up an edit window. If funname is the same as myfun, you will save the edited function, overwriting myfun. If you make a syntax error, when editing, R will send you an error message upon closing the edit window and will not save the changes. funname=edit() will bring up the most recent edit window

Session 3: Editing a function ?appendappendx=(1:10)*4append(x,3,after=4)myappend=edit(append)myappend(x,3,after=4)

Functions calling Functions When a function calls another function, you need to be careful to understand which variables are local to which functions. This is called "scope" and is discussed in the tutorial. (10.7) One of the few differences between Splus and R pertain to "scope" and so it is important to understand this if you are trying to port functions between them.

Documenting your Functions You will quickly lose track of all your functions unless you document them. Comments should be added to all functions. If you plan to share the functions, the comments should at least include a list of the function arguments and outputs. You can also create help documents that will respond to ?

Creating a Help File R has a mark-up language for creating help files for your functions and other objects. We will look at this for a homework.