R for data science ||探索性數(shù)據(jù)分析

什么是探索性數(shù)據(jù)分析

參看之前的文章:
數(shù)量生態(tài)學(xué)筆記||數(shù)據(jù)探索
環(huán)境與生態(tài)統(tǒng)計(jì)||探索性數(shù)據(jù)分析
環(huán)境與生態(tài)統(tǒng)計(jì)||探索性數(shù)據(jù)可視化

探索性數(shù)據(jù)分析的作用
  • 對(duì)數(shù)據(jù)提出問(wèn)題
  • 對(duì)數(shù)據(jù)進(jìn)行可視化锅移、轉(zhuǎn)換、建模晴埂,進(jìn)而找出問(wèn)題的答案
  • 使用上一步的結(jié)果來(lái)精煉問(wèn)題蛛碌,并提出新問(wèn)題
對(duì)分布進(jìn)行可視化
head(diamonds)
# A tibble: 6 x 10
  carat cut       color clarity depth table price     x     y     z
  <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48

ggplot(data = diamonds) +
  geom_bar(mapping = aes(x = cut))
> diamonds %>%
+   count(cut)
# A tibble: 5 x 2
  cut           n
  <ord>     <int>
1 Fair       1610
2 Good       4906
3 Very Good 12082
4 Premium   13791
5 Ideal     21551
ggplot(data = diamonds) +
  geom_histogram(mapping = aes(x = carat), binwidth = 0.5)

> diamonds %>% 
+   count(cut_width(carat, 0.5))
# A tibble: 11 x 2
   `cut_width(carat, 0.5)`     n
   <fct>                   <int>
 1 [-0.25,0.25]              785
 2 (0.25,0.75]             29498
 3 (0.75,1.25]             15977
 4 (1.25,1.75]              5313
 5 (1.75,2.25]              2002
 6 (2.25,2.75]               322
 7 (2.75,3.25]                32
 8 (3.25,3.75]                 5
 9 (3.75,4.25]                 4
10 (4.25,4.75]                 1
11 (4.75,5.25]                 1
diamonds %>% 
  filter(carat < 3)  %>% 
   ggplot( mapping = aes(x = carat)) +
  geom_histogram(binwidth = 0.1)

diamonds %>% 
   filter(carat < 3)  %>% 
 ggplot( mapping = aes(x = carat, colour = cut)) +
   geom_freqpoly(binwidth = 0.1)

典型值
diamonds %>% 
   filter(carat < 3)  %>% 
   ggplot( mapping = aes(x = carat)) +
   geom_histogram(binwidth = 0.01)
 
異常值
p1<- ggplot(diamonds) + 
   geom_histogram(mapping = aes(x = y), binwidth = 0.5)
p2<-ggplot(diamonds) + 
  geom_histogram(mapping = aes(x = y), binwidth = 0.5) +
  coord_cartesian(ylim = c(0, 50))
library(gridExtra) 
grid.arrange(p1,p2,ncol = 2, nrow = 1)
unusual <- diamonds %>% 
  filter(y < 3 | y > 20) %>% 
  select(price, x, y, z) %>%
  arrange(y)
unusual


# A tibble: 9 x 4
  price     x     y     z
  <int> <dbl> <dbl> <dbl>
1  5139  0      0    0   
2  6381  0      0    0   
3 12800  0      0    0   
4 15686  0      0    0   
5 18034  0      0    0   
6  2130  0      0    0   
7  2130  0      0    0   
8  2075  5.15  31.8  5.12
9 12210  8.09  58.9  8.06
缺失值
  • 去丟棄異常值
diamonds2 <- diamonds %>% 
  filter(between(y, 3, 20))

建議用缺失值代替異常值

diamonds2 <- diamonds %>% 
  mutate(y = ifelse(y < 3 | y > 20, NA, y))
p1<- ggplot(data = diamonds2, mapping = aes(x = x, y = y)) + 
  geom_point()

Warning message:
Removed 9 rows containing missing values (geom_point).
p2<-ggplot(data = diamonds2, mapping = aes(x = x, y = y)) + 
  geom_point(na.rm = TRUE)
grid.arrange(p1,p2,ncol = 2, nrow = 1)

nycflights13::flights %>% 
  mutate(
    cancelled = is.na(dep_time),
    sched_hour = sched_dep_time %/% 100,
    sched_min = sched_dep_time %% 100,
    sched_dep_time = sched_hour + sched_min / 60
  ) %>% 
  ggplot(mapping = aes(sched_dep_time)) + 
    geom_freqpoly(mapping = aes(colour = cancelled), binwidth = 1/4)
相關(guān)變動(dòng)
p1<-ggplot(data = diamonds, mapping = aes(x = price)) + 
  geom_freqpoly(mapping = aes(colour = cut), binwidth = 500)

p2<-ggplot(diamonds) + 
  geom_bar(mapping = aes(x = cut))

p3<-ggplot(data = diamonds, mapping = aes(x = price, y = ..density..)) + 
  geom_freqpoly(mapping = aes(colour = cut), binwidth = 500)

grid.arrange(p1,p2,p3,ncol = 3, nrow = 1)
p1<-ggplot(data = diamonds, mapping = aes(x = cut, y = price)) +
  geom_boxplot()
p2<-ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
  geom_boxplot()
p3<-ggplot(data = mpg) +
  geom_boxplot(mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy))
p4<-ggplot(data = mpg) +
  geom_boxplot(mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy)) +
  coord_flip()

p5<-ggplot(data = mpg) +
  geom_violin(mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy)) +
  coord_flip()
grid.arrange(p1,p2,p3,p4,p5,ncol = 5, nrow = 1)

兩個(gè)分類(lèi)變量
p1<-ggplot(data = diamonds) +
  geom_count(mapping = aes(x = cut, y = color))
p2<- diamonds %>% 
  count(color, cut) %>%  
  ggplot(mapping = aes(x = color, y = cut)) +
  geom_tile(mapping = aes(fill = n))

  
  diamonds %>% 
  count(color, cut)
#> # A tibble: 35 x 3
#>   color cut           n
#>   <ord> <ord>     <int>
#> 1 D     Fair        163
#> 2 D     Good        662
#> 3 D     Very Good  1513
#> 4 D     Premium    1603
#> 5 D     Ideal      2834
#> 6 E     Fair        224
#> # … with 29 more rows
  grid.arrange(p1,p2,ncol = 2, nrow = 1)
兩個(gè)連續(xù)變量
p1<- ggplot(data = diamonds) +
  geom_point(mapping = aes(x = carat, y = price))

p2<-ggplot(data = diamonds) + 
  geom_point(mapping = aes(x = carat, y = price), alpha = 1 / 100)

smaller <- diamonds %>% 
  filter(carat < 3)

p3<-ggplot(data = smaller) +
  geom_bin2d(mapping = aes(x = carat, y = price))

# install.packages("hexbin")
p4<-ggplot(data = smaller) +
  geom_hex(mapping = aes(x = carat, y = price))

p5<-ggplot(data = smaller, mapping = aes(x = carat, y = price)) + 
  geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)))

grid.arrange(p1,p2,p3,p4,p5,ncol = 5, nrow = 1)
模式和模型
  • 模式是不是巧合
  • 如何描述隱含關(guān)系
  • 隱含關(guān)系有多強(qiáng)
  • 其他變量如何影響這種關(guān)系
  • 獨(dú)立分組會(huì)有變化么
library(modelr)

mod <- lm(log(price) ~ log(carat), data = diamonds)

diamonds2 <- diamonds %>% 
  add_residuals(mod) %>% 
  mutate(resid = exp(resid))

p1<-ggplot(data = diamonds2) + 
  geom_point(mapping = aes(x = carat, y = resid))

p2<-ggplot(data = diamonds2) + 
  geom_boxplot(mapping = aes(x = cut, y = resid))

grid.arrange(p1,p2,ncol = 2, nrow = 1)

ggplot2 調(diào)用
diamonds %>% 
  count(cut, clarity) %>% 
  ggplot(aes(clarity, cut, fill = n)) + 
  geom_tile()


r4ds

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末聂喇,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子蔚携,更是在濱河造成了極大的恐慌授帕,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評(píng)論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浮梢,死亡現(xiàn)場(chǎng)離奇詭異跛十,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)秕硝,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)芥映,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人远豺,你說(shuō)我怎么就攤上這事奈偏。” “怎么了躯护?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,057評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵惊来,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我棺滞,道長(zhǎng)裁蚁,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,509評(píng)論 1 293
  • 正文 為了忘掉前任继准,我火速辦了婚禮枉证,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘移必。我一直安慰自己室谚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著秒赤,像睡著了一般猪瞬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上入篮,一...
    開(kāi)封第一講書(shū)人閱讀 51,443評(píng)論 1 302
  • 那天陈瘦,我揣著相機(jī)與錄音,去河邊找鬼崎弃。 笑死,一個(gè)胖子當(dāng)著我的面吹牛含潘,可吹牛的內(nèi)容都是我干的饲做。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼遏弱,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼盆均!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起漱逸,我...
    開(kāi)封第一講書(shū)人閱讀 39,129評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤泪姨,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后饰抒,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體肮砾,經(jīng)...
    沈念sama閱讀 45,561評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評(píng)論 3 335
  • 正文 我和宋清朗相戀三年袋坑,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了仗处。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,902評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡枣宫,死狀恐怖婆誓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情也颤,我是刑警寧澤洋幻,帶...
    沈念sama閱讀 35,621評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站翅娶,受9級(jí)特大地震影響文留,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜竭沫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評(píng)論 3 328
  • 文/蒙蒙 一厂庇、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧输吏,春花似錦权旷、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,838評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)躲查。三九已至,卻和暖如春译柏,著一層夾襖步出監(jiān)牢的瞬間镣煮,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,971評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工鄙麦, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留典唇,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,025評(píng)論 2 370
  • 正文 我出身青樓胯府,卻偏偏與公主長(zhǎng)得像介衔,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子骂因,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容