-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathpreface.Rmd
195 lines (153 loc) · 12 KB
/
preface.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# 前言 {#chap-preface}
> 荃者所以在鱼,得鱼而忘荃;蹄者所以在兔;得兔而忘蹄;言者所以在意,得意而忘言。吾安得夫忘言之人而与之言哉!
>
> --- 摘自 《庄子·杂篇·物》
庄子谈学习,余深以为然,故引之。
> The fish trap exists because of the fish; once you've gotten the fish, you can forget the trap. The rabbit snare exists because of the rabbit; once you've gotten the rabbit, you can forget the snare. Words exist because of meaning; once you've gotten the meaning, you can forget the words. Where can I find a man who has forgotten words so I can have a word with him ? [^notes]
>
> --- Chuang Tzu
[^notes]: 译文摘自 [Eric D. Kolaczyk](http://math.bu.edu/people/kolaczyk/teach.html)
## 语言抉择 {#sec-r-or-python}
\index{Octave}
\index{Python}
行业内可以做统计分析和建模的软件汗牛充栋,比较顶级的收费产品有 SAS 和 SPSS,在科学计算领域的 Matlab 和 Mathematica 也有相当强的统计功能,而用户基数最大的是微软 Excel,抛开微软公司的商业手段不说,Excel 的市场份额却是既成事实。 Brian D. Ripley 20 多年前的一句话很有意思,放在当下也是适用的。
> Let's not kid ourselves: the most widely used piece of software for statistics is Excel.
>
> --- Brian D. Ripley [@Ripley_2002_SS]
有鉴于 Excel 在人文、社会、经济和管理等领域的影响力,熟悉 R 语言的人把它看作超级收费版的 Excel,这实在是一点也不过分。事实上,我司就是一个很好的明证,一个在线教育类的互联网公司,各大业务部门都在使用 Excel 作为主要的数据分析工具。然而,Excel 的不足也十分突出,工作过程无法保存和重复利用,Excel 也不是数据库,数据集稍大,操作起来愈发困难,对于复杂的展示,需要借助内嵌的 VBA,由于缺乏版本控制,随着时间的推移,几乎不可维护。所以,我们还是放弃 Excel 吧,Jenny Bryan 更在 2016 年国际 R 语言大会上的直截了当地喊出了这句话[^bye-excel]。Nathan Stephens 对 Excel 的缺陷不足做了全面的总结[^without-excel]。
> Some people familiar with R describe it as a supercharged version of Microsoft's Excel spreadsheet software.
>
> --- Ashlee Vance [^AV-NYT-2009]
[^AV-NYT-2009]: <https://www.nytimes.com/2009/01/07/technology/business-computing/07program.html>
[^bye-excel]: <https://channel9.msdn.com/Events/useR-international-R-User-conference/useR2016/jailbreakr-Get-out-of-Excel-free>
[^without-excel]: <https://resources.rstudio.com/wistia-rstudio-essentials-2/how-to-excel-without-using-excel>
另一方面,我们谈谈开源领域的佼佼者 --- R (<https://cran.r-project.org/>),Python (<https://www.python.org/>) 和 Octave (<http://www.gnu.org/software/octave/>)。Python 号称万能的胶水语言,从系统运维到深度学习都有它的广泛存在,它被各大主流 Linux 系统内置,语言风格上更接近于基数庞大的开发人员,形成了强大的生态平台。 Octave 号称是可以替代 Matlab 的科学计算软件,在兼容 Matlab 的方面确实做的很不错,然而,根据 Julia 官网给出的各大编程语言的测试 <https://julialang.org/benchmarks/>,性能上不能相提并论。
```{r r-eco-system, engine="tikz", echo=FALSE, cache=TRUE, fig.cap="R 语言扩展包生态系统", out.width="85%", engine.opts=list(extra.preamble=c("\\usepackage[default]{sourcesanspro}","\\usetikzlibrary{mindmap}"))}
\tikzset{level 1 concept/.append style={font=\sf}}
\tikzset{level 2 concept/.append style={font=\sf}}
\begin{tikzpicture}[scale=0.88]
\path[mindmap, concept color=gray!90, text=black]
node[concept] {\textcolor{black} {\Huge\sf Interface}}[clockwise from=60]
child[concept color=gray!70] {
node[concept] {\textcolor{black}{JavaScript}}[clockwise from=145]
child[concept color=gray!50,text=black] { node[concept] {highcharter} }
child[concept color=gray!50,text=black] { node[concept] {plotly} }
child[concept color=gray!50,text=black] { node[concept] {leaflet} }
}
child[concept color=gray!70] {
node[concept] {\textcolor{black}{ DBI }}[clockwise from=100]
child[concept color=gray!50,text=black] { node[concept] {sparklyr} }
child[concept color=gray!50,text=black] { node[concept] {odbc} }
child[concept color=gray!50,text=black] { node[concept] {rJava} }
child[concept color=gray!50,text=black] { node[concept] {RJDBC} }
child[concept color=gray!50,text=black] { node[concept] {RcppRedis} }
}
child[concept color=gray!70] {
node[concept] {\textcolor{black}{Application}} % 在线分析
[clockwise from=-25]
child[concept color=gray!50,text=black] { node[concept] {shiny} }
child[concept color=gray!50,text=black] { node[concept] {RestRserve} }
child[concept color=gray!50,text=black] { node[concept] {plumber} }
}
child[concept color=gray!70] {
node[concept] {\textcolor{black}{Language}}
[clockwise from=-90]
child[concept color=gray!50,text=black] { node[concept] {reticulate} }
child[concept color=gray!50,text=black] { node[concept] {rstan} }
child[concept color=gray!50,text=black] { node[concept] {Rcpp} }
}
child[concept color=gray!70] {
node[concept] {\textcolor{black}{Software}}[clockwise from=-145]
child[concept color=gray!50,text=black] { node[concept] {RWeka } }
child[concept color=gray!50,text=black] { node[concept] {Ryacas} }
child[concept color=gray!50,text=black] { node[concept] {RcppOctave} }
}
child[concept color=gray!70] {
node[concept] {\textcolor{black}{Framework}}[clockwise from=145]
child[concept color=gray!50,text=black] { node[concept] {mxnet} }
child[concept color=gray!50,text=black] { node[concept] {tensorflow} }
};
\end{tikzpicture}
```
R 提供了丰富的图形接口,包括 Tcl/Tk , Gtk, Shiny 等,以及基于它们的衍生品 rattle([RGtk2](http://www.ggobi.org/rgtk2/))、[Rcmdr]( https://CRAN.R-project.org/package=Rcmdr)(tcl/tk)、[radiant](https://github.com/radiant-rstats/radiant)(shiny)。更多底层介绍,见 John Chamber 的著作《Extending R》。
[Eviews](https://www.eviews.com) 时间序列和计量经济模型,相比于 Eviews, [Stata](https://www.stata.com/) 是综合型的统计软件,提供丰富的统计模型,
[SPSS](https://www.ibm.com/cn-zh/products/spss-statistics) 同 Stata 类似,[Minitab](https://www.minitab.com/zh-cn/),
[JASP](https://jasp-stats.org/) 是开源的软件, [Octave](https://www.gnu.org/software/octave/) 是对标 Matlab 的工程计算软件,
有丰富的优化功能,是一门编程语言兼软件,为求解统计模型的参数提供了广泛的基础能力。[Tableau](https://www.tableau.com/zh-cn) 提供强大的分析和打造数据产品的能力。TikZ 在绘制示意图方面有很大优势,特别是示意图里包含数学公式,这更是 LaTeX 所擅长的方面。
JASP <https://jasp-stats.org> 是一款免费的统计软件,源代码托管在 Github 上 <https://github.com/jasp-stats/jasp-desktop>,主要由阿姆斯特丹大学 E. J. Wagenmakers 教授 <https://www.ejwagenmakers.com/> 领导的团队维护开发,实现了很多贝叶斯和频率统计方法,相似的图形用户界面使得 JASP 可以作为 SPSS 的替代,目前实现的功能见 <https://jasp-stats.org/current-functionality/>,统计方法见博客 <https://www.bayesianspectacles.org/>。
国内可视化分析平台,比如 [hiplot](https://github.com/hiplot) 基于 R 语言实现可视化分析,各类图形的介绍见[文档](https://hiplot.com.cn/docs/zh/),极大地降低数据分析人员探索分析的门槛,节省了时间,同时非专业内的人也可借助其完成分析探索的过程,只需明白各类图形的含义即可。美团也建设了自己的可视化分析平台帮助运营人员,详见[文档](https://tech.meituan.com/2018/08/02/mt-r-practice.html)
[Patrick Burns](https://www.burns-stat.com/) 收集整理了 R 语言中奇葩的现象,写成 [The R Inferno](https://www.burns-stat.com/pages/Tutor/R_inferno.pdf) 直译过来就是《R 之炼狱》。这些奇葩的怪现象可以看做是 R 风格的一部分,对于编程人员来说就是一些建议和技巧,参考之可以避开某些坑。 Paul E. Johnson 整理了一份真正的 R 语言建议,记录了他自己从 SAS 转换到 R 的过程中遇到的各种问题 <http://pj.freefaculty.org/R/Rtips.html>。Michail Tsagris 和 Manos Papadakis 也收集了 70 多条 R 编程的技巧和建议,力求以更加 R 范地将语言特性发挥到极致 [@Rfast_2018_Tsagris],Martin Mächler 提供了一份 [Good Practices in R Programming](https://stat.ethz.ch/Teaching/maechler/R/useR_2014/)。
Python 社区广泛流传着 Tim Peters 的 《Python 之禅》,它已经整合进每一版 Python 软件中,只需在 Python 控制台里执行 `import this` 可以获得。
> 1. Beautiful is better than ugly.
1. Explicit is better than implicit.
1. Simple is better than complex.
1. Complex is better than complicated.
1. Flat is better than nested.
1. Sparse is better than dense.
1. Readability counts.
1. Special cases aren't special enough to break the rules.
1. Although practicality beats purity.
1. Errors should never pass silently.
1. Unless explicitly silenced.
1. In the face of ambiguity, refuse the temptation to guess.
1. There should be one-- and preferably only one --obvious way to do it.
1. Although that way may not be obvious at first unless you're Dutch.
1. Now is better than never.
1. Although never is often better than *right* now.
1. If the implementation is hard to explain, it's a bad idea.
1. If the implementation is easy to explain, it may be a good idea.
1. Namespaces are one honking great idea -- let's do more of those!
>
> --- The Zen of Python
总之,编程语言到一定境界都是殊途同归的,对美的认识也是趋同的,道理更是相通的,Python 社区的 Pandas <https://github.com/pandas-dev/pandas> 和 Matplotlib <https://github.com/matplotlib/matplotlib> 也有数据框和图形语法的影子。Pandas <https://github.com/pandas-dev/pandas> 明确说了要提供与 data.frame 类似的数据结构和对应统计函数等,而 Matplotlib 偷了 ggplot2 绘图样式 <https://matplotlib.org/3.2.2/gallery/style_sheets/ggplot.html>。
## 数据科学 {#sec-data-science}
John M. Chambers 谈了数据科学的源起以及和 S、R 语言的渊源 [@RS_2020_John]。
## 写作环境 {#sec-writing-details}
\index{bookdown}
\index{Pandoc}
```{r book-workflow, engine = "dot", echo=FALSE, fig.cap="书籍项目", cache=TRUE}
digraph {
graph [layout = dot, rankdir = TB];
node [shape = rectangle];
rec1 [label = "RStudio IDE / VS Code"];
rec2 [label = "编辑 R Markdown"];
rec3 [label = "远程推送 (Git)"];
rec4 [label = "本地编译 (Makefile)"];
rec9 [label = "knitr + rmarkdown + bookdown"];
rec10 [label = "GitBook / EPUB"];
rec11 [label = "PDF"];
rec19 [label = "GhostScript"];
rec20 [label = "GraphicsMagick / ImageMagick"]; # resize image
rec21 [label = "PNG"];
rec22 [label = "OptiPNG"];
rec23 [label = "Graphviz"];
rec24 [label = "GIF"];
rec25 [label = "Gifski"];
rec26 [label = "PCRE 2.0"];
rec12 [label = "LaTeX"];
rec13 [label = "Markdown"];
rec14 [label = "Pandoc + Lua"];
rec15 [label = "CSS + JavaScript + HTML"];
rec16 [label = "R + Python + TinyTeX"];
rec17 [label = "编译成功"];
rec18 [label = "自动部署 (bookdown.org)"];
rec5 [label = "自动编译 (Travis CI)"];
rec6 [label = "自动部署 (Github Pages)"];
rec8 [label = "自动编译 (Github Action)"];
rec7 [label = "自动部署 (Netlify)"];
edge [style = solid, color = black];
rec1 -> rec2 -> rec3 -> rec5 -> rec9
rec2 -> rec4 -> rec16 -> rec9
rec3 -> rec8 -> rec9 -> rec13 -> rec14
rec14 -> rec12 -> rec11 -> rec17 -> rec6
rec14 -> rec15 -> rec10 -> rec17 -> rec7
rec17 -> rec18
rec19 -> rec11
rec20 -> rec11
rec22 -> rec21 -> rec15
rec23 -> rec21
rec24 -> rec15
rec25 -> rec24
rec26 -> rec12
}
```