506
gooseeker集搜客
一行R代码实现数据分析可视化
作者简介: 唐源,目前就职于芝加哥一家创业公司,曾参与和创作过多个被广泛使用的 R 和 Python 开源项目,是 ggfortify,lfda,metric-learn 等包的作者,也是 xgboost,caret,pandas 等包的贡献者。(喜欢爬山和烧烤 )
ggfortify 是一个简单易用的R软件包,它可以仅仅使用一行代码来对许多受欢迎的R软件包结果进行二维可视化,这让统计学家以及数据科学家省去了许多繁琐和重复的过程,不用对结果进行任何处理就能以 ggplot 的风格画出好看的图,大大地提高了工作的效率。
ggfortify 已经可以在 CRAN 上下载得到,但是由于最近很多的功能都还在快速增加,因此还是推荐大家从 Github 上下载和安装。
1 library(devtools)2 install_github('sinhrks/ggfortify')3 library(ggfortify)
接下来我将简单介绍一下怎么用 ggplot2 和 ggfortify 来很快地对PCA、聚类以及LFDA的结果进行可视化,然后将简单介绍用 ggfortify 来对时间序列进行快速可视化的方法。
1,PCA (主成分分析) ggfortify 使 ggplot2 知道怎么诠释PCA对象。加载好 ggfortify 包之后, 你可以对stats::prcomp 和 stats::princomp 对象使用 ggplot2::autoplot。
1 library(ggfortify)2 df <- iris[c(1, 2, 3, 4)]3 autoplot(prcomp(df))
4 autoplot(prcomp(df), data = iris, colour = 'Species')
5 autoplot(prcomp(df), data = iris, colour = 'Species', label = TRUE,label.size = 3)
6 autoplot(prcomp(df), data = iris, colour = 'Species', shape = FALSE, label.size = 3)
7 autoplot(prcomp(df), data = iris, colour = 'Species', loadings = TRUE)
8 autoplot(prcomp(df), data = iris, colour = 'Species',loadings = TRUE, loadings.colour = 'blue',loadings.label = TRUE, loadings.label.size = 3)
1 d.factanal <- factanal(state.x77, factors = 3, scores = 'regression')2 autoplot(d.factanal, data = state.x77, colour = 'Income')
3,K-均值聚类
1 autoplot(kmeans(USArrests, 3), data = USArrests)
2 autoplot(kmeans(USArrests, 3), data = USArrests, label = TRUE, label.size = 3)
4,其他聚类 ggfortify 也支持 cluster::clara, cluster::fanny, cluster::pam。
1 library(cluster)2 autoplot(clara(iris[-5], 3))
3 autoplot(fanny(iris[-5], 3), frame = TRUE)
4 autoplot(pam(iris[-5], 3), frame = TRUE, frame.type = 'norm')
5,lfda(Fisher局部判别分析) lfda 包支持一系列的 Fisher 局部判别分析方法,包括半监督 lfda,非线性 lfda。你也可以使用 ggfortify 来对他们的结果进行可视化。
1 library(lfda)2 # Fisher局部判别分析 (LFDA)3 model <- lfda(iris[-5], iris[, 5], 4, metric="plain")4 autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
8 # 半监督Fisher局部判别分析 (SELF)9 model <- self(iris[-5], iris[, 5], beta = 0.1, r = 3, metric="plain")10 autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
6,时间序列的可视化用 ggfortify 可以使时间序列的可视化变得极其简单。接下来我将给出一些简单的例子。
6.1,ts对象
1 library(ggfortify)2 autoplot(AirPassengers)
3 autoplot(AirPassengers, ts.colour = 'red', ts.linetype = 'dashed')
6.2,多变量时间序列
4 library(vars)5 data(Canada)6 autoplot(Canada)
7 autoplot(Canada, facets = FALSE)
- zoo::zooreg
- xts::xts
- timeSeries::timSeries
- tseries::irts
一些例子:
8 library(xts)9 autoplot(as.xts(AirPassengers), ts.colour = 'green')
12 autoplot(AirPassengers, ts.geom = 'bar', fill = 'blue')
14 library(forecast)15 d.arima <- auto.arima(AirPassengers)16 d.forecast <- forecast(d.arima, level = c(95), h = 50)17 autoplot(d.forecast)
18 autoplot(d.forecast, ts.colour = 'firebrick1', predict.colour = 'red',predict.linetype = 'dashed', conf.int = FALSE)
6.5,vars包
19 library(vars)20 data(Canada)21 d.vselect <- VARselect(Canada, lag.max = 5, type = 'const')$selection[1]22 d.var <- VAR(Canada, p = d.vselect, type = 'const')23 autoplot(predict(d.var, n.ahead = 50), ts.colour = 'dodgerblue4', predict.colour = 'blue', predict.linetype = 'dashed')
24 library(changepoint)25 autoplot(cpt.meanvar(AirPassengers))
6.7,strucchange包
27 library(strucchange)28 autoplot(breakpoints(Nile ~ 1), ts.colour = 'blue', ts.linetype = 'dashed',cpt.colour = 'dodgerblue3', cpt.linetype = 'solid')
29 library(dlm)30 form <- function(theta){31 dlmModPoly(order = 1, dV = exp(theta[1]), dW = exp(theta[2]))32 }3334 model <- form(dlmMLE(Nile, parm = c(1, 1), form)$par)35 filtered <- dlmFilter(Nile, model)3637 autoplot(filtered)
6.9,KFAS包
43 library(KFAS)44 model <- SSModel(45 Nile ~ SSMtrend(degree=1, Q=matrix(NA)), H=matrix(NA)46 )47 48 fit <- fitSSM(model=model, inits=c(log(var(Nile)),log(var(Nile))), method="BFGS")49 smoothed <- KFS(fit$model)50 autoplot(smoothed)
51 filtered <- KFS(fit$model, filtering="mean", smoothing='none')52 autoplot(filtered)
6.10,stats包
可支持的stats包里的对象有:
- stl, decomposed.ts
- acf, pacf, ccf
- spec.ar, spec.pgram
- cpgram
56 autoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = 'blue')
58 autoplot(acf(AirPassengers, plot = FALSE), conf.int.fill = '#0000FF', conf.int.value = 0.8, conf.int.type = 'ma')
59 ggcpgram(arima.sim(list(ar = c(0.7, -0.5)), n = 50))
更多关于时间序列的例子,请参考 Rpubs 上的介绍。
摘自:大数据文摘
最后更新:2017-01-09 14:08:06