This preview shows pages 1–13. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: S t a t i s t i c s Ucla Jan de Leeuw Computational Statistics with R Part II: Matrices in R 1. Introduction 2 In these notes we will give an Roriented overview of matrix algebra and matrix calculation. This means that we will think of a matrix as a certain type of R object. More precisely, for the purposes of this course a matrix is a vector of numbers with a dimension attribute . The dimension attribute is a vector with two integers, the number of rows and the number of columns . 3 So matrix calculus for us is calculating with these R objects. Of course packages like Matlab or SPSS or Stata have very similar objects  and thus their matrix calculus will be very similar to ours. Observe that in this part of the course we exclude matrices of logicals, characters or raw bytes. Only numerical matrices are used, and actually only matrices with doubles are considered. We will freely switch from matrix notation (formulas) to R notation (code). 4 Here is an example of a matrix. > options(digits=2) > x<matrix(rnorm(20),5,4) > x [,1] [,2] [,3] [,4] [1,] 2.42 0.0282 0.11 0.25 [2,] 0.78 0.1449 0.74 0.24 [3,] 1.24 0.0029 0.20 1.55 [4,] 0.65 0.5688 0.83 0.68 [5,] 0.24 0.2800 1.32 0.25 > nrow(x) [1] 5 > ncol(x) [1] 4 > dim(x) [1] 5 4 5 In order to get better acquainted: > str(x) num [1:5, 1:4] 2.419 0.777 1.238 0.651 0.241 ... > attributes(x) $dim [1] 5 4 6 It is important to understand the role of the dimension attribute. > dim(x)<c(10,2) > x [,1] [,2] [1,] 2.4193 0.11 [2,] 0.7770 0.74 [3,] 1.2378 0.20 [4,] 0.6509 0.83 [5,] 0.2414 1.32 [6,] 0.0282 0.25 [7,] 0.1449 0.24 [8,] 0.0029 1.55 [9,] 0.5688 0.68 [10,] 0.2800 0.25 7 > y<as.vector(x) > y [1] 2.4193 0.7770 1.2378 0.6509 0.2414 0.0282 0.1449 0.0029 0.5688 [10] 0.2800 0.1106 0.7381 0.1961 0.8328 1.3207 0.2515 0.2376 1.5456 [19] 0.6753 0.2453 > dim(y) NULL > dim(y)<c(5,4) > y [,1] [,2] [,3] [,4] [1,] 2.42 0.0282 0.11 0.25 [2,] 0.78 0.1449 0.74 0.24 [3,] 1.24 0.0029 0.20 1.55 [4,] 0.65 0.5688 0.83 0.68 [5,] 0.24 0.2800 1.32 0.25 8 It is also important to understand the difference between a vector of length n (which has no attributes, except maybe names) and a matrix of dimension c (n,1) or c (1,n) . > c [1] 1 2 3 > matrix(c,1,3) [,1] [,2] [,3] [1,] 1 2 3 > matrix(c,3,1) [,1] [1,] 1 [2,] 2 [3,] 3 9 If we format a vector to a matrix, the first column gets filled first, then the second column, and so on. This is known as columnmajororder . If we want rowmajor order , we have to ask. > matrix(1:6,3,2) [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 > matrix(1:6,3,2,byrow=TRUE) [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 10 Special Matrices > diag(3) [,1] [,2] [,3] [1,] 1 0 [2,] 0 1 [3,] 0 0 1 > matrix(1,2,3) [,1] [,2] [,3] [1,] 1 1 1 [2,] 1 1 1 > diag(1:2) [,1] [,2] [1,] 1 [2,] 0 2 11 In statistics, we are mostly interested in two types of matrices: positive semidefinite real symmetric matrices and rectangular matrices in which columns correspond to variables and row...
View
Full
Document
 Fall '10
 JandeLeeuw
 Statistics

Click to edit the document details