Download Always attach the data frame

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Euclidean vector wikipedia , lookup

Quadratic form wikipedia , lookup

Tensor operator wikipedia , lookup

System of linear equations wikipedia , lookup

Basis (linear algebra) wikipedia , lookup

Symmetry in quantum mechanics wikipedia , lookup

Eigenvalues and eigenvectors wikipedia , lookup

Bra–ket notation wikipedia , lookup

Linear algebra wikipedia , lookup

Jordan normal form wikipedia , lookup

Determinant wikipedia , lookup

Cartesian tensor wikipedia , lookup

Matrix (mathematics) wikipedia , lookup

Perron–Frobenius theorem wikipedia , lookup

Singular-value decomposition wikipedia , lookup

Non-negative matrix factorization wikipedia , lookup

Four-vector wikipedia , lookup

Cayley–Hamilton theorem wikipedia , lookup

Matrix calculus wikipedia , lookup

Matrix multiplication wikipedia , lookup

Transcript
STT 412/512
R#5
Spring, 2006
#Let's see how R handles matrices...
#First note that R treats all our vectors as column vectors.
i=rep(1,12) ; i
#gives the identity vector with 12 1's in it...
as.matrix(i)
#to create a matrix in R use the matrix function
x=matrix(c(1,2,3,4,5,6),nrow=2,ncol=3) ; x
#change to nrow=3 and ncol=2
y=matrix(c(1,2,3,4,5,6), nrow=3, ncol=2)
#So notice that the matrix is created by columns from the vector
#Try the examples given on page 68...notice that the matrix
#arithmetic of addition and subtraction require that the two matrices
#be of the same dimensions
A=matrix(c(2,1,1,5,3,2), nrow=3, ncol=2)
B=matrix(c(3,-1,5,3,-1,2,2,2),nrow=2,ncol=4)
BB=matrix(c(3,5,7,9,11,13),nrow=3,ncol=2)
A-BB
BB+A
A+B
#note error in the last operation since they are not the same size...
#To multiply matrices A and B they must have dimensions Ap x q and Bq x
t
q
#The product has Cp
x t
with elements
cij =
a b
r 1
ir rj
#Try A x B ... but notice that the symbol for matrix multiplication is not
#just the "*" but the "%*%" operator and the dimensions must be correct
#for that operator to work properly. Go over this operation by hand and
#check the result given in R...
#
#The transpose of the p x q matrix A =(aij) is a q x p matrix obtained by
#interchanging the rows and columns of A. It is written as A' = (aji)
#The command in R to do a transpose is t(A).
#Note that (AB)' = B'A' (or in R notation, t(A%*%B) will equal t(B)%*%t(A)
#A square matrix A is called symmetric if A' = A
#HW: Create a 4 x 4 symmetric matrix and check with R...
#We may also multiply two p x 1 (column) vectors x and y. It is called taking
#the inner product. The product x'y is a 1 x p vector by a p x 1 vector and
#so the result is a 1 x 1 vector, or a so-called scaler (a real number)
#Two vectors are said to be orthogonal if their inner product is 0.
#The square root of the inner product of a vector x with itself sqrt(x'x)
#is called the length of the vector x, or the Euclidean norm of x.
#Look at the example in the textbook on page 69:
y=matrix(c(3,0,-2,1)) ; x=matrix(c(3,2,5,1))
t(x)%*%y #this result = 0 so x and y are orthogonal.
sqrt(t(x)%*%x) #this result is the length of the vector x
#See page 69 for the definition of linearly dependent and linearly independent
#vectors. Essentially a set of vectors is dependent if you can find a set of
#constants, not all zero, such that the linear combination of the vectors
#with the constants is zero. Note that this means that if the vectors are
#linearly dependent, at least one of the vectors can be written as a linear
#combination of the others...
#Go over the example at the bottom of page 69...
#Define the rank of a p x q matrix A as the largest number of linearly
#independent columns in A. (or the largest number of lin. indep. rows of A)
#The p x p square matrix AA' has the same rank as the p x q matrix A.
#A m x m square matrix A is called nonsingular if its rank is m. Matrix A
#would be called singular if its rank is less than m.
STT 412/512
R#5
Spring, 2006
#Go over the Example on page 70 to see that the rank of AA' is 2 since the
#3rd column of AA' is the difference of the first two columns...
#
#Use the det function in R to compute the determinant of a square matrix
#The determinant of a square matrix is non-zero if and only if the matrix
#is nonsingular. If a matrix A is nonsingular, then its inverse is denoted
#by A-1 and it has the property that if you multiply A by its inverse, in
#either direction A A-1 or A-1A you will always get the identy matrix I.
#If A and B are both nonsingular m x m matrices then (AB)-1 = B-1 A-1 .
#Go over the example on page 71...
#In R, to compute the inverse of a nonsingular matrix A is found with the
#solve function.
A=matrix(c(4,1,2,3),nrow=2)
solve(A) #check by multiplying them together and show it's the 2x2 identy
A%*%(solve(A))
#Now let's apply this to our simple linear regression problem that we've been
#working on for several weeks... Set up the Hardness data in matrix format.
hardness=read.table(file=file.choose(),header=T)
hardness[1:5,]
attach(hardness)
#Note that the response (y) is named Hard, so
y=matrix(Hard) ; y
#Now create the X matrix by combining a vector of 1's in the first column
#with the values of the explanatory variable (Temp) in the second column.
#We do this so the matrix multiplication in 3.1 on p.74 can be done correctly.
X=cbind(rep(1,length(Temp)),Temp)
#Now when we solve the normal equations (in matrix format in 3.2 on page 75)
#and get 3.5 we'll have the X and y needed to get the l.s. estimates in the
#beta vector. Clearly the beauty of this is that we can very easily extend this
#to more than one explanatory variable and the matrix equation to be solved is
#the same; only the dimensions change...
#
#To get the l.s. estimates in beta, solve as in 3.3 on page 75:
beta=solve(t(X)%*%X)%*%t(X)%*%y ; beta #Look familiar??
#Notice that the fitted values and the residuals can also be expressed in matrix
#format:
fitted=X%*%beta ; fitted #This is equation 3.4 on page 75
res=y - fitted ; res
#This is equation 3.5 on page 75
#Finally, the l.s. estimator of 2 is given as
ssquared=(1/(length(Temp)-2)*(t(res)%*%res)) ; ssquared #This is 3.6, p. 75
#HOMEWORK: 1. Using matrices, solve 2.17 and 2.18 (p. 60 and 61) for the
#least squares estimates of slope and intercept (i.e., get beta-hat) and
#confirm your results with the usual lm function.
#2. Using matrices, do the same as in #1 above using data in Table 1.4,
#on p.13; predict gallons per mile using the three explanatory variables
#weight, displacement, and number of cylinders. Interpret the values of beta
#that you get in the beta-hat matrix. (SEND ME YOUR R CODE FOR #2)