# Welcome back

## Find a book, put up your feet, stay awhile

Sign in with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more

Download

Standard view

Full view

of .

Look up keyword

Like this

Share on social networks

45Activity

×

0 of .

Results for: No results containing your search query

P. 1

R Matrix TutorRatings:

4.0

(1)|Views: 28,493|Likes: 46Published by Fanny Sylvia C.

See more

See less

https://www.scribd.com/doc/6187359/R-Matrix-Tutor

06/06/2013

text

original

The Statistical Language

R

- Matrices

This document is designed to introduce matrix calculations useful for statistics. It assumes somefamiliarity with

R

. Those meeting

R

for the ﬁrst time should go through guides such as “

Using R in Windows XP on the ISS network

” and “

The

R

Language - Basics

”.

A. Variables - vectors and matrices

1. Vectors are the key variables within

R

. They are most easily constructed using the “com-bine”

c(

·

)

function; e.g.

x = c(1,2,4,5)

creates a vector

x

of length 4 with elements1,2,4,5. Matrices are variables represented as 2-way arrays of numbers. They are most eas-ily created from vectors using the

matrix()

function:

x = matrix(c(1,2,3,4,5,6), nrow = 3)

yields

1 42 53 6

x = matrix(c(1,2,3,4,5,6), nrow = 3, byrow = T)

yields

1 23 45 6

In each case a vector of length 6 is turned into a 3

×

2 matrix. You can specify

nrow=3

or

ncol=2

or both; all three choices are equivalent. The vector ﬁlls up the matrix in successivecolumns unless “byrow = T” is speciﬁed, in which case the matrix is ﬁlled up by successiverows.2.

rbind

and

cbind

are functions which can combine matrices columnwise or rowwise. If

x

and

y

contain the matrices

1 23 4

and

5 67 8

,

then

cbind(x,y)

and

rbind(x,y)

contain the matrices

1 2 5 63 4 7 8

and

1 23 45 67 8

,

respectively. If

x

and/or

y

is a vector, it is interpreted as a column in

cbind

and as a rowin

rbind

.

September 2002

1

The Statistical Language

R

- Matrices3. Subsets. Elements or blocks of elements of vectors and matrices can be picked out usingsquare brackets. If

x

is a vector and

y

is a matrix, then

x[3]

element 3 of

xx[1:3]

a subvector of

x

containing theﬁrst 3 elements

y[5,6]

the element of the matrix

y

from row 5, column 6

y[c(1,2), c(2,3)]

a 2

×

2 submatrix of

y

containing rows 1,2 and columns 2,3

z = y[c(1,2), c(2,3)]

save this submatrix as variable

zy[c(1,2), c(2,3)] = matrix(c(5, 9, 2.5, 3), nrow=2)

Assign new values to a submatrix of y

B. Basic matrix calculations

1. In general, operations on vectors and matrices act elementwise. If

x

and

y

are

n

×

m

matrices,then

x+y

and

x

∗

y

are

n

×

m

matrices with elements

x[i,j] + y[i,j]

, and

x[i,j]

∗

y[i,j]

,respectively. See below for “proper” matrix multiplication. If

a

is a scalar,

a

∗

x

has compo-nents

a x[i,j]

.2. Matrix addition. If

x

and

y

are

n

×

m

matrices, then the matrices

x+y

and

x-y

make senseand are evaluated elementwise. If

a

is a scalar,

a+x

and

a

∗

x

are

n

×

m

matrices with elements

a+x[i,j]

and

a

∗

x[i,j]

.3. Matrix multiplication. If

x

is an

n

×

m

matrix and

y

is

m

×

p

, then

x%

∗

%y

representsthe matrix product with elements

m j

=1

x

[

i,j

]

∗

y

[

j,k

]. There are also some special casesinvolving vectors. In an expression involving a matrix and a vector, the vector is interpretedas either a row vector or a column vector, whichever makes the multiplication legitimate.

•

If

x

is a vector of length

m

and

y

is an

m

×

p

matrix,

x%

∗

%y

is a vector of length

p

.

•

If

x

is an

n

×

m

matrix and

y

is a vector of length

m

,

x%

∗

%y

is a vector of length

m

.

September 2002

2

The Statistical Language

R

- Matrices

•

If

x

and

y

are vectors of length

m

,

x%

∗

%y

is a scalar (i.e. vector of length 1)representing the inner product

mi

=1

x[i]

∗

y[i]

.4. Matrix inverse. If

a

is a square non-singular

n

×

n

matrix,

solve(a)

is an

n

×

n

matrixgiving the inverse of

a

. If

b

is a vector of length

n

,

solve(a,b)

gives the matrix product of the inverse of

a

times

b

.

Note

. In

R

,

a

∧

(-1)

or

1/a

is an

n

×

n

matrix with elements

1/a[i,j]

−

the element-wise inverse of

a

. This is very diﬀerent from the matrix inverse of

a

!5.

t(a)

gives the transpose of a rectangular matrix

a

.6. Diagonals. The function

diag(a)

has 3 distinct interpretations

•

If

a

is a positive integer scalar (e.g. 5) then

diag(a)

is a 5

×

5 matrix with ones downthe diagonal and zeros elsewhere.

•

If

a

is a vector of length

n

,

diag(a)

is an

n

×

n

diagonal matrix with (

i,i

)thelement

a[i]

.

•

If

a

is an

n

×

n

matrix,

diag(a)

is a vector containing the diagonal elements

a[i,i]

,

i

= 1

,... ,n

.

C. Plotting

If

x

is an

n

×

p

data matrix, then

matplot(x, col=1)

plots each variable in an

n

×

p

data matrix vs 1:n. The

i

th variable in the plot is labelled

i

,

i

= 1

,... ,p

. Remove the option

col=1

to see each variable in a diﬀerent colour.When the matrix represents image or spatial data then

image(x,col=gray((0:32)/32))

# draws a grayscale image

contour(x, add = TRUE, drawlabels = FALSE)

# overlays a contour plot

September 2002

3

You've already reviewed this. Edit your review.

1 hundred reads

1 thousand reads

Reply

Maria Aparecida P Franco added this note

I used the instructions- thanks!! 2012-july-25

johnvoyce liked this

johnvoyce liked this

johnvoyce liked this

Aleksandra Raicevic liked this

Jonas Wiklund liked this

Abhishek Kumar liked this

- Read and print without ads
- Download to keep your version
- Edit, email or read offline

© Copyright 2015 Scribd Inc.

Language

Choose the language in which you want to experience Scribd:

Sign in with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

Password Reset Email Sent

Join with Facebook

Sorry, we are unable to log you in via Facebook at this time. Please try again later.

or

By joining, you agree to our

read free for two weeks

Personalized recommendationsbased on books you love

Syncing across all your devices

Join with Facebook

or Join with EmailSorry, we are unable to log you in via Facebook at this time. Please try again later.

Already a member? Sign in.

By joining, you agree to our

to download

Personalized recommendationsbased on books you love

Syncing across all your devices

Continue with Facebook

Sign inJoin with emailSorry, we are unable to log you in via Facebook at this time. Please try again later.

By joining, you agree to our

Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

CANCEL

OK

You've been reading!

NO, THANKS

OK

scribd