-
Notifications
You must be signed in to change notification settings - Fork 15
/
05-DSexploreanalysis.Rmd
executable file
·153 lines (132 loc) · 5.26 KB
/
05-DSexploreanalysis.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# 探索性数据分析
## 探索绘图原则
- 表示可比的对比
- 表示因果 解释 机制 系统结构
- 表示多元变量(超过2)
- 证据整合 目的驱动非工具驱动
- 证据描述要标注限定恰当
- 内容为王
## 探索性绘图
- 个人理解用
- 不用过分关注细节
- 基于问题或假设出发
## R绘图系统
### 基础包
- 艺术家绘画模式
- 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()` windows `windows()` Unix/linux `x11()`
- 先调用后用`dev.off()`关闭设备
- 矢量图设备 保真放大 元素过多体积庞大 `pdf()` `svg()` `winmetafile()` `postscript()`
- 位图设备 放大失真 基于像素 `png()` `jpeg()` `tiff()` `bmp()`
- 当前设备`dev.cur()`
- 设置设备`dev.set(<integer>)`
- 设备转移`dev.copy` `dev.copy2pdf`
### 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)`
- 可同时展示分组信息及交互作用
### ggplot2
- 基于图形语法理念
- 图形属性映射数据问题
- 自动处理界面 允许后期添加 结合base与lattice
- 默认友好
- 基础绘图`qplot()`
- `ggplot()` 通过叠加元素出图
- 细节调整`xlab()`, `ylab()`, `labs()`, `ggtitle()`
- 主题调整`theme()`
- 做图需求
- 数据框 data.frame
- 属性映射 asethetic mappling
- 几何对象 geoms
- 条件 facets
- 统计转换 stats
- 范围量表 scales
- 坐标轴系统 coordinate system
### 数学绘图
- Tex语法
- 使用`expression()`
- `?plotmath`
### 色彩管理
- `colorRamp` 返回01间数值 表示颜色过度
- `colorRampPalette` 返回8位颜色代码调色盘
- `colors` 返回可用颜色
- RColorBrewer包 含有预先配色信息 序列 无序 两级
- `rgb`产生三原色颜色 `alpha` 控制透明度
- 绘图时用`col`调用调色盘颜色
```{r}
pal <- colorRamp(c("red", "blue"))
pal(0)
pal(1)
pal(0.5)
#####
pal <- colorRampPalette(c("red", "yellow"))
pal(2)
pal(10)
#####
library(RColorBrewer)
cols <- brewer.pal(3, "BuGn")
```
## 分层聚类
- 找到最近的 聚到一起 找下个最近的
- 给出距离范围与距离计算方法
- 欧氏距离 多维空间点距 开平方
- manhattan距离 出租车距离 绝对值
- 给出变量间或样本间的关系
- 图形可能不稳定 多少样本多少类
- 结果是确定的
- 选定cut点并不明显
- 应该首先用来探索
## k-means聚类
- 固定聚类数 给出聚类中心 寻找最近的点 循环
- 需要聚类数与聚类距离范围
- 需要大量聚类 通过眼睛 交叉检验
- k的经验数值$\sqrt{n/2}$ 或者根据解释的变量变化多少来选取
- 结果不确定 根据聚类数与迭代次数而变化
## 维度还原
- 找到最不相关的数来解释整体方差(统计)在这些数中选取个数最少的来解释原始数据(压缩)
- 不一定是真实向量的叠加
- SVD是PCA的一种解法 UDV三个向量 其中U表示行变化模式 D表示方差 V表示列变换模式 这样有助于解释主成分变化
- 标准化与否影响结果
- 计算量大
- 类似探索分析还有因子分析 独立成分分析 潜在语义分析
- impute包可补充缺失值