
一行R代碼來實(shí)現(xiàn)繁瑣的可視化
ggfortify 是一個(gè)簡單易用的R軟件包,它可以僅僅使用一行代碼來對許多受歡迎的R軟件包結(jié)果進(jìn)行二維可視化,這讓統(tǒng)計(jì)學(xué)家以及數(shù)據(jù)科學(xué)家省去了許多繁瑣和重復(fù)的過程,不用對結(jié)果進(jìn)行任何處理就能以ggplot的風(fēng)格畫出好看的圖,大大地提高了工作的效率。
ggfortify 已經(jīng)可以在 CRAN 上下載得到,但是由于最近很多的功能都還在快速增加,因此還是推薦大家從 Github 上下載和安裝。
library(devtools) install_github('sinhrks/ggfortify') library(ggfortify)
接下來我將簡單介紹一下怎么用ggplot2和ggfortify來很快地對PCA、聚類以及LFDA的結(jié)果進(jìn)行可視化,然后將簡單介紹用ggfortify來對時(shí)間序列進(jìn)行快速可視化的方法。
PCA (主成分分析)
ggfortify使ggplot2知道怎么詮釋PCA對象。加載好ggfortify包之后, 你可以對stats::prcomp和stats::princomp對象使用ggplot2::autoplot。
library(ggfortify) df <- iris[c(1, 2, 3, 4)] autoplot(prcomp(df))
你還可以選擇數(shù)據(jù)中的一列來給畫出的點(diǎn)按類別自動(dòng)分顏色。輸入help(autoplot.prcomp)可以了解到更多的其他選擇。
autoplot(prcomp(df), data = iris, colour = 'Species')
比如說給定label = TRUE可以給每個(gè)點(diǎn)加上標(biāo)識(以rownames為標(biāo)準(zhǔn)),也可以調(diào)整標(biāo)識的大小。
autoplot(prcomp(df), data = iris, colour = 'Species', label = TRUE, label.size = 3)
給定shape = FALSE可以讓所有的點(diǎn)消失,只留下標(biāo)識,這樣可以讓圖更清晰,辨識度更大。
autoplot(prcomp(df), data = iris, colour = 'Species', shape = FALSE, label.size = 3)
給定loadings = TRUE可以很快地畫出特征向量。
autoplot(prcomp(df), data = iris, colour = 'Species', loadings = TRUE)
同樣的,你也可以顯示特征向量的標(biāo)識以及調(diào)整他們的大小,更多選擇請參考幫助文件。
autoplot(prcomp(df), data = iris, colour = 'Species', loadings = TRUE, loadings.colour = 'blue', loadings.label = TRUE, loadings.label.size = 3)
和PCA類似,ggfortify也支持stats::factanal對象。可調(diào)的選擇也很廣泛。以下給出了簡單的例子:
注意當(dāng)你使用factanal來計(jì)算分?jǐn)?shù)的話,你必須給定scores的值。
d.factanal <- factanal(state.x77, factors = 3, scores = 'regression') autoplot(d.factanal, data = state.x77, colour = 'Income')
autoplot(d.factanal, label = TRUE, label.size = 3, loadings = TRUE, loadings.label = TRUE, loadings.label.size = 3)
K-均值聚類
autoplot(kmeans(USArrests, 3), data = USArrests)
autoplot(kmeans(USArrests, 3), data = USArrests, label = TRUE, label.size = 3)
其他聚類
ggfortify也支持cluster::clara,cluster::fanny,cluster::pam。
library(cluster) autoplot(clara(iris[-5], 3))
給定frame = TRUE,可以把stats::kmeans和cluster::*中的每個(gè)類圈出來。
autoplot(fanny(iris[-5], 3), frame = TRUE)
你也可以通過frame.type來選擇圈的類型。更多選擇請參照ggplot2::stat_ellipse里面的frame.type的type關(guān)鍵詞。
autoplot(pam(iris[-5], 3), frame = TRUE, frame.type = 'norm')
更多關(guān)于聚類方面的可視化請參考 Github 上的 Vignette 或者 Rpubs 上的例子。
lfda(Fisher局部判別分析)
lfda包支持一系列的 Fisher 局部判別分析方法,包括半監(jiān)督 lfda,非線性 lfda。你也可以使用ggfortify來對他們的結(jié)果進(jìn)行可視化。
library(lfda) # Fisher局部判別分析 (LFDA) model <- lfda(iris[-5], iris[, 5], 4, metric="plain") autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
# 非線性核Fisher局部判別分析 (KLFDA) model <- klfda(kmatrixGauss(iris[-5]), iris[, 5], 4, metric="plain") autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
注意對iris數(shù)據(jù)來說,不同的類之間的關(guān)系很顯然不是簡單的線性,這種情況下非線性的klfda 影響可能太強(qiáng)大而影響了可視化的效果,在使用前請充分理解每個(gè)算法的意義以及效果。
# 半監(jiān)督Fisher局部判別分析 (SELF) model <- self(iris[-5], iris[, 5], beta = 0.1, r = 3, metric="plain") autoplot(model, data = iris, frame = TRUE, frame.colour = 'Species')
時(shí)間序列的可視化
用ggfortify可以使時(shí)間序列的可視化變得極其簡單。接下來我將給出一些簡單的例子。
ts對象
library(ggfortify) autoplot(AirPassengers)
可以使用ts.colour和ts.linetype來改變線的顏色和形狀。更多的選擇請參考help(autoplot.ts)。
autoplot(AirPassengers, ts.colour = 'red', ts.linetype = 'dashed')
多變量時(shí)間序列
library(vars) data(Canada) autoplot(Canada)
使用facets = FALSE可以把所有變量畫在一條軸上。
autoplot(Canada, facets = FALSE)
autoplot也可以理解其他的時(shí)間序列類別。可支持的R包有:
zoo::zooreg
xts::xts
tseries::irts
一些例子:
library(xts) autoplot(as.xts(AirPassengers), ts.colour = 'green')
library(timeSeries) autoplot(as.timeSeries(AirPassengers), ts.colour = ('dodgerblue3'))
你也可以通過ts.geom來改變幾何形狀,目前支持的有l(wèi)ine,bar和point。
autoplot(AirPassengers, ts.geom = 'bar', fill = 'blue')
autoplot(AirPassengers, ts.geom = 'point', shape = 3)
forecast包
library(forecast) d.arima <- auto.arima(AirPassengers) d.forecast <- forecast(d.arima, level = c(95), h = 50) autoplot(d.forecast)
有很多設(shè)置可供調(diào)整:
autoplot(d.forecast, ts.colour = 'firebrick1', predict.colour = 'red', predict.linetype = 'dashed', conf.int = FALSE)
vars包
library(vars) data(Canada) d.vselect <- VARselect(Canada, lag.max = 5, type = 'const')$selection[1] d.var <- VAR(Canada, p = d.vselect, type = 'const') autoplot(predict(d.var, n.ahead = 50), ts.colour = 'dodgerblue4', predict.colour = 'blue', predict.linetype = 'dashed')
changepoint包
library(changepoint) autoplot(cpt.meanvar(AirPassengers))
autoplot(cpt.meanvar(AirPassengers), cpt.colour = 'blue', cpt.linetype = 'solid')
strucchange包
library(strucchange) autoplot(breakpoints(Nile ~ 1), ts.colour = 'blue', ts.linetype = 'dashed', cpt.colour = 'dodgerblue3', cpt.linetype = 'solid')
dlm包
library(dlm) form <- function(theta){ dlmModPoly(order = 1, dV = exp(theta[1]), dW = exp(theta[2])) } model <- form(dlmMLE(Nile, parm = c(1, 1), form)$par) filtered <- dlmFilter(Nile, model) autoplot(filtered)
autoplot(filtered, ts.linetype = 'dashed', fitted.colour = 'blue')
smoothed <- dlmSmooth(filtered) autoplot(smoothed)
p <- autoplot(filtered) autoplot(smoothed, ts.colour = 'blue', p = p)
KFAS包
library(KFAS) model <- SSModel( Nile ~ SSMtrend(degree=1, Q=matrix(NA)), H=matrix(NA) ) fit <- fitSSM(model=model, inits=c(log(var(Nile)),log(var(Nile))), method="BFGS") smoothed <- KFS(fit$model) autoplot(smoothed)
使用smoothing='none'可以畫出過濾后的結(jié)果。
filtered <- KFS(fit$model, filtering="mean", smoothing='none') autoplot(filtered)
trend <- signal(smoothed, states="trend") p <- autoplot(filtered) autoplot(trend, ts.colour = 'blue', p = p)
stats包
可支持的stats包里的對象有:
stl,decomposed.ts
acf,pacf,ccf
spec.ar,spec.pgram
cpgramautoplot(stl(AirPassengers, s.window = 'periodic'), ts.colour = 'blue')
autoplot(acf(AirPassengers, plot = FALSE))
autoplot(acf(AirPassengers, plot = FALSE), conf.int.fill = '#0000FF', conf.int.value = 0.8, conf.int.type = 'ma')
autoplot(spec.ar(AirPassengers, plot = FALSE))
ggcpgram(arima.sim(list(ar = c(0.7, -0.5)), n = 50))
library(forecast) ggtsdiag(auto.arima(AirPassengers))
gglagplot(AirPassengers, lags = 4)
數(shù)據(jù)分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
LSTM 模型輸入長度選擇技巧:提升序列建模效能的關(guān)鍵? 在循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)家族中,長短期記憶網(wǎng)絡(luò)(LSTM)憑借其解決長序列 ...
2025-07-11CDA 數(shù)據(jù)分析師報(bào)考條件詳解與準(zhǔn)備指南? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代浪潮下,CDA 數(shù)據(jù)分析師認(rèn)證愈發(fā)受到矚目,成為眾多有志投身數(shù) ...
2025-07-11數(shù)據(jù)透視表中兩列相乘合計(jì)的實(shí)用指南? 在數(shù)據(jù)分析的日常工作中,數(shù)據(jù)透視表憑借其強(qiáng)大的數(shù)據(jù)匯總和分析功能,成為了 Excel 用戶 ...
2025-07-11尊敬的考生: 您好! 我們誠摯通知您,CDA Level I和 Level II考試大綱將于 2025年7月25日 實(shí)施重大更新。 此次更新旨在確保認(rèn) ...
2025-07-10BI 大數(shù)據(jù)分析師:連接數(shù)據(jù)與業(yè)務(wù)的價(jià)值轉(zhuǎn)化者? ? 在大數(shù)據(jù)與商業(yè)智能(Business Intelligence,簡稱 BI)深度融合的時(shí)代,BI ...
2025-07-10SQL 在預(yù)測分析中的應(yīng)用:從數(shù)據(jù)查詢到趨勢預(yù)判? ? 在數(shù)據(jù)驅(qū)動(dòng)決策的時(shí)代,預(yù)測分析作為挖掘數(shù)據(jù)潛在價(jià)值的核心手段,正被廣泛 ...
2025-07-10數(shù)據(jù)查詢結(jié)束后:分析師的收尾工作與價(jià)值深化? ? 在數(shù)據(jù)分析的全流程中,“query end”(查詢結(jié)束)并非工作的終點(diǎn),而是將數(shù) ...
2025-07-10CDA 數(shù)據(jù)分析師考試:從報(bào)考到取證的全攻略? 在數(shù)字經(jīng)濟(jì)蓬勃發(fā)展的今天,數(shù)據(jù)分析師已成為各行業(yè)爭搶的核心人才,而 CDA(Certi ...
2025-07-09【CDA干貨】單樣本趨勢性檢驗(yàn):捕捉數(shù)據(jù)背后的時(shí)間軌跡? 在數(shù)據(jù)分析的版圖中,單樣本趨勢性檢驗(yàn)如同一位耐心的偵探,專注于從單 ...
2025-07-09year_month數(shù)據(jù)類型:時(shí)間維度的精準(zhǔn)切片? ? 在數(shù)據(jù)的世界里,時(shí)間是最不可或缺的維度之一,而year_month數(shù)據(jù)類型就像一把精準(zhǔn) ...
2025-07-09CDA 備考干貨:Python 在數(shù)據(jù)分析中的核心應(yīng)用與實(shí)戰(zhàn)技巧? ? 在 CDA 數(shù)據(jù)分析師認(rèn)證考試中,Python 作為數(shù)據(jù)處理與分析的核心 ...
2025-07-08SPSS 中的 Mann-Kendall 檢驗(yàn):數(shù)據(jù)趨勢與突變分析的有力工具? ? ? 在數(shù)據(jù)分析的廣袤領(lǐng)域中,準(zhǔn)確捕捉數(shù)據(jù)的趨勢變化以及識別 ...
2025-07-08備戰(zhàn) CDA 數(shù)據(jù)分析師考試:需要多久?如何規(guī)劃? CDA(Certified Data Analyst)數(shù)據(jù)分析師認(rèn)證作為國內(nèi)權(quán)威的數(shù)據(jù)分析能力認(rèn)證 ...
2025-07-08LSTM 輸出不確定的成因、影響與應(yīng)對策略? 長短期記憶網(wǎng)絡(luò)(LSTM)作為循環(huán)神經(jīng)網(wǎng)絡(luò)(RNN)的一種變體,憑借獨(dú)特的門控機(jī)制,在 ...
2025-07-07統(tǒng)計(jì)學(xué)方法在市場調(diào)研數(shù)據(jù)中的深度應(yīng)用? 市場調(diào)研是企業(yè)洞察市場動(dòng)態(tài)、了解消費(fèi)者需求的重要途徑,而統(tǒng)計(jì)學(xué)方法則是市場調(diào)研數(shù) ...
2025-07-07CDA數(shù)據(jù)分析師證書考試全攻略? 在數(shù)字化浪潮席卷全球的當(dāng)下,數(shù)據(jù)已成為企業(yè)決策、行業(yè)發(fā)展的核心驅(qū)動(dòng)力,數(shù)據(jù)分析師也因此成為 ...
2025-07-07剖析 CDA 數(shù)據(jù)分析師考試題型:解鎖高效備考與答題策略? CDA(Certified Data Analyst)數(shù)據(jù)分析師考試作為衡量數(shù)據(jù)專業(yè)能力的 ...
2025-07-04SQL Server 字符串截取轉(zhuǎn)日期:解鎖數(shù)據(jù)處理的關(guān)鍵技能? 在數(shù)據(jù)處理與分析工作中,數(shù)據(jù)格式的規(guī)范性是保證后續(xù)分析準(zhǔn)確性的基礎(chǔ) ...
2025-07-04CDA 數(shù)據(jù)分析師視角:從數(shù)據(jù)迷霧中探尋商業(yè)真相? 在數(shù)字化浪潮席卷全球的今天,數(shù)據(jù)已成為企業(yè)決策的核心驅(qū)動(dòng)力,CDA(Certifie ...
2025-07-04CDA 數(shù)據(jù)分析師:開啟數(shù)據(jù)職業(yè)發(fā)展新征程? ? 在數(shù)據(jù)成為核心生產(chǎn)要素的今天,數(shù)據(jù)分析師的職業(yè)價(jià)值愈發(fā)凸顯。CDA(Certified D ...
2025-07-03