2.3 探索和理解数据
2.3.1 数据的结构
read.scv()
str()
数据导入到R后,就要开始对数据做一个基本的检查;数据的特征和案例;理解数据的独特,以便后面让模型匹配具体的学习问题;
## data exploration example using used car data
usedcars <- read.csv("usedcars.csv", stringsAsFactors = FALSE)
# get structure of used car data
str(usedcars)
2.3.2 数值型变量
summary()
汇总统计量分两类:数据的中心测度 和 分散程度测度
# summarize numeric variables
summary(usedcars$year)
summary(usedcars[c("price", "mileage")])
2.3.2.1 测量中心趋势--平均数和中位数
mean()
median()
一类统计量:标识一组数据的中间值;
平均数:按照值的加和,得出平均数;如果平均值偏低,表名数据中小的数偏多;均值不一定最合适;
中位数:按照次序,位于中间的那个数;中位数反映中等水平,不能全面反映数据。
均值对异常值非常敏感,容易受到少数几个极端值的影响;
(实例:均值比中位数大很多,暗示着存在几个极大值)
# calculate the mean income
(36000 + 44000 + 56000) / 3
mean(c(36000, 44000, 56000))
# the median income
median(c(36000, 44000, 56000))
2.3.2.2 测量数据分散程度--四分位数和五分位数
min()
max()
range()
diff()
IQR()
quantile()
seq()
测量数值的大小是否具有多样性;与数据的分散程序有关;
五数汇总:最小值、第一四分位数、中位数、第三四分位数、最大值;
第一四分位数(Q1)、第三四分位数(Q3)和中位数(Q2)一起讲数据集分为了4个元素数量均等的部分。
Q1和Q3之间的50%意义重大,为四分位距(IQR),它就是数据分散程度的一个测度。
通过比较差值大小,可以判断哪个区域之间聚集的更加紧密。
# the min/max of used car prices
range(usedcars$price)
# the difference of the range
diff(range(usedcars$price))
# IQR for used car prices
IQR(usedcars$price)
# use quantile to calculate five-number summary
quantile(usedcars$price)
# the 99th percentile
quantile(usedcars$price, probs = c(0.01, 0.99))
# quintiles
quantile(usedcars$price, seq(from = 0, to = 1, by = 0.20))
2.3.2.3 箱图
boxplot()
箱图是对五数汇总的常用的可视化方式;可以显示数值型变量的中心和分散程度;可以很快了解变量的值域和偏度;
用水平线来表示五数汇总的值;盒子上的三条线分别是Q1、Q2、Q3;
最大值和最小值用细线表示;细线通常会有所保留;向内聚合;Q1-1.5*IQR 和 Q3+1.5*IQR;代替了min 和 max;
任何坐落在min到Q1-1.5*IQR 和 Q3+1.5*IQR到max之间的值都是异常值,用圆圈或者点来表示。
# boxplot of used car prices and mileage
boxplot(usedcars$price, main="Boxplot of Used Car Prices",
ylab="Price ($)")
boxplot(usedcars$mileage, main="Boxplot of Used Car Mileage",
ylab="Odometer (mi.)")
2.3.2.4 直方图
hist()
直方图是形象化描述数值型变量间差异的方式。高度代表频率,横坐标为分段内的起点和终点。
# histograms of used car prices and mileage
hist(usedcars$price, main = "Histogram of Used Car Prices",
xlab = "Price ($)")
hist(usedcars$mileage, main = "Histogram of Used Car Mileage",
xlab = "Odometer (mi.)")
2.3.2.5 均匀分布和正态分布
2.3.2.6 方差和标准差
var()
sd()
正态分布可以用两个参数来定义:中心 和 分散程度。
中心用均值来定义;分散程度用标准差来测量;
# variance and standard deviation of the used car data
var(usedcars$price)
sd(usedcars$price)
var(usedcars$mileage)
sd(usedcars$mileage)
2.3.3 分类变量
table()
prop.table()
round()
不能用summary()函数,而是用table()函数;
# one-way tables for the used car data
table(usedcars$year)
table(usedcars$model)
table(usedcars$color)
# compute table proportions
model_table <- table(usedcars$model)
prop.table(model_table)
# round the data
color_table <- table(usedcars$color)
color_pct <- prop.table(color_table) * 100
round(color_pct, digits = 1)
2.3.4 变量之间关系
之前都是考察单变量统计量,现在我们开始考虑多变量之间的关系;
2.3.4.1 散点图
plot()
两个变量之间线性关系的强弱是通过统计量相关系数来测量的。
# scatterplot of price vs. mileage
plot(x = usedcars$mileage, y = usedcars$price,
main = "Scatterplot of Price vs. Mileage",
xlab = "Used Car Odometer (mi.)",
ylab = "Used Car Price ($)")
2.3.4.2 双向交叉表
%in%
CrossTable()
用于检查两个名义变量(分类变量)之间的关系;
# new variable indicating conservative colors
usedcars$conservative <-
usedcars$color %in% c("Black", "Gray", "Silver", "White")
# checking our variable
table(usedcars$conservative)
# Crosstab of conservative by model
library(gmodels)
CrossTable(x = usedcars$model, y = usedcars$conservative)