笔记 5 探索性数据分析
5.1 探索绘图原则
- 表示可比的对比
 - 表示因果 解释 机制 系统结构
 - 表示多元变量(超过2)
 - 证据整合 目的驱动非工具驱动
 - 证据描述要标注限定恰当
 - 内容为王
 
5.2 探索性绘图
- 个人理解用
 - 不用过分关注细节
 - 基于问题或假设出发
 
5.3 R绘图系统
5.3.1 基础包
- 艺术家绘画模式
 - graphics 包括基础包的绘图函数如plot, hist, boxplot
 - grDevices 包括执行调用绘图设备函数如X11, PDF, PostScript, PNG
 - 叠加函数 高度自由度
 - 初始化新图 然后标注
 - 以下命令熟记
 - pch: the plotting symbol (default is open circle)
 - lty: the line type (default is solid line), can be dashed, dotted, etc.
 - lwd: the line width, specified as an integer multiple
 - col: the plotting color, specified as a number, string, or hex code; the colors() function gives you a vector of colors by name
 - xlab: character string for the x-axis label
 - ylab: character string for the y-axis label
 par():查找做图的画布参数 具体如下- las: the orientation of the axis labels on the plot
 - bg: the background color
 - mar: the margin size
 - oma: the outer margin size (default is 0 for all sides)
 - mfrow: number of plots per row, column (plots are filled row-wise)
 - mfcol: number of plots per row, column (plots are filled column-wise)
 - plot: make a scatterplot, or other type of plot depending on the class of the object being plotted
 - lines: add lines to a plot, given a vector x values and a corresponding vector of y values (or a 2-column matrix); this function just connects the dots
 - points: add points to a plot
 - text: add text labels to a plot using specified x, y coordinates
 - title: add annotations to x, y axis labels, title, subtitle, outer margin
 - mtext: add arbitrary text to the margins (inner or outer) of the plot
 - axis: adding axis ticks/labels
 - 图形设备
 - 图像一定要有设备
 - 屏幕设备 Mac 
quartz()windowswindows()Unix/linuxx11() - 先调用后用
dev.off()关闭设备 - 矢量图设备 保真放大 元素过多体积庞大 
pdf()svg()winmetafile()postscript() - 位图设备 放大失真 基于像素 
png()jpeg()tiff()bmp() - 当前设备
dev.cur() - 设置设备
dev.set(<integer>) - 设备转移
dev.copydev.copy2pdf 
5.3.2 lattice
- 一站式解决
 - lattice 包括框架图函数如xyplot, bwplot, levelplot
 - grid 包括独立于基础绘图系统的网格绘图系统
 - 一个函数解决问题 默认自定义空间少
 - 返回
trellis类型对象 可单独存储 - 界面调整使用
panel选项 - 以下为常见函数
 - xyplot: this is the main function for creating scatterplots
 - bwplot: box-and-whiskers plots (“boxplots”)
 - histogram: histograms
 - stripplot: like a boxplot but with actual points
 - dotplot: plot dots on “violin strings”
 - splom: scatterplot matrix; like pairs in base plotting system
 - levelplot, contourplot: for plotting “image” data
 - 基本格式
 xyplot(y ~ x | f * g, data)- 可同时展示分组信息及交互作用
 
5.3.3 ggplot2
- 基于图形语法理念
 - 图形属性映射数据问题
 - 自动处理界面 允许后期添加 结合base与lattice
 - 默认友好
 - 基础绘图
qplot() ggplot()通过叠加元素出图- 细节调整
xlab(),ylab(),labs(),ggtitle() - 主题调整
theme() - 做图需求
 - 数据框 data.frame
 - 属性映射 asethetic mappling
 - 几何对象 geoms
 - 条件 facets
 - 统计转换 stats
 - 范围量表 scales
 - 坐标轴系统 coordinate system
 
5.3.4 数学绘图
- Tex语法
 - 使用
expression() ?plotmath
5.3.5 色彩管理
colorRamp返回01间数值 表示颜色过度colorRampPalette返回8位颜色代码调色盘colors返回可用颜色- RColorBrewer包 含有预先配色信息 序列 无序 两级
 rgb产生三原色颜色alpha控制透明度- 绘图时用
col调用调色盘颜色 
pal <- colorRamp(c("red", "blue"))
pal(0)##      [,1] [,2] [,3]
## [1,]  255    0    0
pal(1)##      [,1] [,2] [,3]
## [1,]    0    0  255
pal(0.5)##       [,1] [,2]  [,3]
## [1,] 127.5    0 127.5
#####
pal <- colorRampPalette(c("red", "yellow"))
pal(2)## [1] "#FF0000" "#FFFF00"
pal(10)##  [1] "#FF0000" "#FF1C00" "#FF3800" "#FF5500" "#FF7100" "#FF8D00" "#FFAA00"
##  [8] "#FFC600" "#FFE200" "#FFFF00"
#####
library(RColorBrewer)
cols <- brewer.pal(3, "BuGn")5.4 分层聚类
- 找到最近的 聚到一起 找下个最近的
 - 给出距离范围与距离计算方法
 - 欧氏距离 多维空间点距 开平方
 - manhattan距离 出租车距离 绝对值
 - 给出变量间或样本间的关系
 - 图形可能不稳定 多少样本多少类
 - 结果是确定的
 - 选定cut点并不明显
 - 应该首先用来探索
 
5.5 k-means聚类
- 固定聚类数 给出聚类中心 寻找最近的点 循环
 - 需要聚类数与聚类距离范围
 - 需要大量聚类 通过眼睛 交叉检验
 - k的经验数值\(\sqrt{n/2}\) 或者根据解释的变量变化多少来选取
 - 结果不确定 根据聚类数与迭代次数而变化
 
5.6 维度还原
- 找到最不相关的数来解释整体方差(统计)在这些数中选取个数最少的来解释原始数据(压缩)
 - 不一定是真实向量的叠加
 - SVD是PCA的一种解法 UDV三个向量 其中U表示行变化模式 D表示方差 V表示列变换模式 这样有助于解释主成分变化
 - 标准化与否影响结果
 - 计算量大
 - 类似探索分析还有因子分析 独立成分分析 潜在语义分析
 - impute包可补充缺失值