跳到內容

統計分析:使用 R/R/矩陣的介紹

來自華夏公益教科書,開放的書籍,開放的世界
許多統計理論使用矩陣代數。雖然本書不需要詳細瞭解矩陣,但瞭解 R 如何處理矩陣非常有用。

本質上,矩陣(複數:矩陣)是向量的二維等價物。換句話說,它是一個數字的矩形網格,按行和列排列。在 R 中,可以使用matrix()函式建立矩陣物件,該函式將一個數字向量(用於填充矩陣)作為第一個引數,並將行數和列數分別作為第二個和第三個引數。

R 也可以使用陣列物件,它們類似於矩陣,但可以有超過 2 個維度。這些對於表格特別有用:一種包含根據各種標準分類的資料計數的陣列。這些“列聯表”的示例如下所示的HairEyeColorTitanic表。

與向量一樣,索引運算子[]可用於訪問矩陣或陣列中的單個元素或元素集。這是透過用逗號分隔括號內的數字來完成的。例如,對於矩陣,需要指定行索引,然後是一個逗號,然後是列索引。如果行索引為空,則假定需要所有行,列索引也是如此。
輸入
m <- matrix(1:12, 3, 4)      #Create a 3x4 matrix filled with numbers 1 to 12
m                            #Display it!
m*2                          #Arithmetic, just like with vectors
m[2,3]                       #Pick out a single element (2nd row, 3rd column)
m[1:2, 2:4]                  #Or a range (rows 1 and 2, columns 2, 3, and 4.)
m[,1]                        #If the row index is missing, assume all rows
m[1,]                        #Same for columns
m[,2] <- 99                  #You can assign values to one or more elements
m                            #See!
###Some real data, stored as "arrays"
HairEyeColor                 #A 3D array
HairEyeColor[,,1]            #Select only the males to make it a 2D matrix
Titanic                      #A 4D array
Titanic[1:3,"Male","Adult",] #A matrix of only the adult male passengers
結果
> m <- matrix(1:12, 3, 4)      #Create a 3x4 matrix filled with numbers 1 to 12
> m                            #Display it!
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> m*2                          #Arithmetic, just like with vectors
     [,1] [,2] [,3] [,4]
[1,]    2    8   14   20
[2,]    4   10   16   22
[3,]    6   12   18   24
> m[2,3]                       #Pick out a single element (2nd row, 3rd column)
[1] 8
> m[1:2, 2:4]                  #Or a range (rows 1 and 2, columns 2, 3, and 4.)
     [,1] [,2] [,3]
[1,]    4    7   10
[2,]    5    8   11
> m[,1]                        #If the row index is missing, assume all rows
[1] 1 2 3
> m[1,]                        #Same for columns
[1]  1  4  7 10
> m[,2] <- 99                  #You can assign values to one or more elements
> m                            #See!
     [,1] [,2] [,3] [,4]
[1,]    1   99    7   10
[2,]    2   99    8   11
[3,]    3   99    9   12
> ###Some real data, stored as "arrays"
> HairEyeColor                 #A 3D array
, , Sex = Male

       Eye
Hair    Brown Blue Hazel Green
  Black    32   11    10     3
  Brown    53   50    25    15
  Red      10   10     7     7
  Blond     3   30     5     8

, , Sex = Female

       Eye
Hair    Brown Blue Hazel Green
  Black    36    9     5     2
  Brown    66   34    29    14
  Red      16    7     7     7
  Blond     4   64     5     8

> HairEyeColor[,,1]            #Select only the males to make it a 2D matrix
       Eye
Hair    Brown Blue Hazel Green
  Black    32   11    10     3
  Brown    53   50    25    15
  Red      10   10     7     7
  Blond     3   30     5     8
> Titanic                      #A 4D array
, , Age = Child, Survived = No

      Sex
Class  Male Female
  1st     0      0
  2nd     0      0
  3rd    35     17
  Crew    0      0

, , Age = Adult, Survived = No

      Sex
Class  Male Female
  1st   118      4
  2nd   154     13
  3rd   387     89
  Crew  670      3

, , Age = Child, Survived = Yes

      Sex
Class  Male Female
  1st     5      1
  2nd    11     13
  3rd    13     14
  Crew    0      0

, , Age = Adult, Survived = Yes

      Sex
Class  Male Female
  1st    57    140
  2nd    14     80
  3rd    75     76
  Crew  192     20

> Titanic[1:3,"Male","Adult",] #A matrix of only the adult male passengers
     Survived
Class  No Yes
  1st 118  57
  2nd 154  14
  3rd 387  75


華夏公益教科書