读取多变量数据

R 中的多元概率分布

Surajit Ray

Professor, University of Glasgow

课程主题

  • 读取并分析多变量数据
  • 探索绘图方法
  • 使用常见统计分布

    • 高斯分布与t分布
  • 高维数据技术

    • 主成分分析(PCA)
R 中的多元概率分布

多变量数据的结构

  • 矩形结构:按行和列组织
    • 行表示观测
    • 列表示变量
  • 可能包括或不包括:
    • 行名或行号
    • 列标题
  • 可能存在缺失值
R 中的多元概率分布

多变量数据示例

来自 剑桥大学网站 的 Iris 数据

5.1  3.5  1.4  0.2  1
4.9  3.0  1.4  0.2  1
4.7  3.2  1.3  0.2  1

出生体重数据(带列名的CSV)

"","case","bwt","gestation","parity","age","height","weight","smoke"
"1",1,120,284,0,27,62,100,0
"2",2,113,282,0,33,64,135,0
R 中的多元概率分布

读取数据

URL 读取

iris_url <- "https://mlg.eng.cam.ac.uk/teaching/3f3/1011/iris.data"
iris_raw <- read.table(iris_url,  sep = "", header = FALSE)

本地读取

iris_raw <- read.table("iris.txt", sep = "", header = FALSE)
R 中的多元概率分布

查看数据集

head(iris_raw, n = 4)
     V1  V2  V3  V4 V5
1   5.1 3.5 1.4 0.2  1  
2   4.9 3.0 1.4 0.2  1  
3   4.7 3.2 1.3 0.2  1  
4   4.6 3.1 1.5 0.2  1
R 中的多元概率分布

设置列名

colnames(iris_raw) <- c("Sepal.Length", "Sepal.Width", "Petal.Length","Petal.Width", "Species" )
head(iris_raw)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2       1
2          4.9         3.0          1.4         0.2       1
3          4.7         3.2          1.3         0.2       1
4          4.6         3.1          1.5         0.2       1
5          5.0         3.6          1.4         0.2       1
6          5.4         3.9          1.7         0.4       1
R 中的多元概率分布

访问指定列

检查当前列名

names(iris_raw)
"Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

$$

访问花萼长度和花萼宽度列

iris_raw[, 1:2] 
iris[, c('Sepal.Length', 'Sepal.Width')]
R 中的多元概率分布

更改数据类型

将最后一列 Species 改为因子

iris_raw$species <- as.factor(iris_raw$species)
str(iris_raw)
'data.frame':    150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
R 中的多元概率分布

设置因子标签

将物种标签从 123 重编码为 setosaversicolorvirginica

  • 指定因子标签
  • 将第一列改为因子
library(car) 
iris_raw$Species <- recode(iris_raw$Species,
                          " 1 ='setosa'; 2 = 'versicolor'; 3 = 'virginica'")
str(iris_raw)
'data.frame':    150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
R 中的多元概率分布

读取带列名的CSV数据

出生体重数据(带列名的CSV)

"","case","bwt","gestation","parity","age","height","weight","smoke"
"1",1,120,284,0,27,62,100,0
"2",2,113,282,0,33,64,135,0
"3",3,128,279,0,28,64,115,1

读取出生体重数据

bwt <- read.csv("birthweight.csv", row.names = 1)
head(bwt, n = 3)
  case bwt gestation parity age height weight smoke
1    1 120       284      0  27     62    100     0
2    2 113       282      0  33     64    135     0
3    3 128       279      0  28     64    115     1
R 中的多元概率分布

让我们读取一些多变量数据!

R 中的多元概率分布

Preparing Video For Download...