99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁精彩閱讀一行R代碼來實(shí)現(xiàn)繁瑣的可視化
一行R代碼來實(shí)現(xiàn)繁瑣的可視化
2017-05-13
收藏

一行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

timeSeries::timSeries

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

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }