R語言-探索兩個變量


目的:

  通過探索文件pseudo_facebook.tsv數據來學會兩個變量的分析流程

 知識點:

  1.ggplot語法

       2.如何做散點圖

       3.如何優化散點圖

       4.條件均值

       5.變量的相關性

       6.子集散點圖

       7.平滑化

簡介:

  如果在探索單一變量時,使用直方圖來表示該值和整體的關系,那么在探索兩個變量的時候,使用散點圖會更適合來探索兩個變量之間的關系

案例分析:

 1.根據年齡和好友數作出散點圖

#導入ggplot2繪圖包
library(ggplot2) setwd(
'D:/Udacity/數據分析進階/R')
#加載數據文件 pf
<- read.csv('pseudo_facebook.tsv',sep='\t')
#使用qplot語法作出散點圖 qplot(x
=age,y=friend_count,data=pf)
#使用ggplot語法作出散點圖,此處使用ggplot作圖語法上更清晰
ggplot(aes(x
=age,y=friend_count),data=pf)+ geom_point()

                                                                          圖 2-1

2.過渡繪制,因為圖2-1有大部分的點都重疊,不太好區分哪個年齡和好友數的關系,所以使用alpha和geom_jitter來進行調整

#geom_jitter消除重合的點
#alpha=1/20表示20個值算1個點
#xlim(13,90)表示x軸的取值從13,90
ggplot(aes(x=age,y=friend_count),data=pf)+
  geom_jitter(alpha=1/20)+
  xlim(13,90)

                                                                     圖 2-2

   3.coord_trans函數的用法,可以給坐標軸上應用函數,使其的可視化效果更好

#給y軸的好友數開根號,使其可視化效果更好
ggplot(aes(x=age,y=friend_count),data=pf)+
  geom_point(alpha=1/20)+
  xlim(13,90)+
  coord_trans(y="sqrt")

                                                                   圖2-3

4.條件均值,根據字段進行分組然后分組進行統計出新的DataFrame

#1.導入dplyr包
#2.使用group_by對年齡字段進行分組
#3.使用summarise統計出平均值和中位數
#4.再使用arrange進行排序
library('dplyr')
pf.fc_by_age <- pf %>%
  group_by(age) %>%
  summarise(friend_count_mean=mean(friend_count),
            friend_count_media = median(friend_count),
            n=n()) %>%
  arrange(age)

5.將該數據和原始數據進行迭加,根據圖形,我們可以得出一個趨勢,從13歲-26歲好友數在增加,從26開始慢慢的好友數開始下降

#1.通過限制x,y的值,做出年齡和好友數的散點圖
#2.做出中位值的漸近線
#3.做出0.9的漸近線
#4.做出0.5的漸近線
#5.做出0.1的漸近線
ggplot(aes(x=age,y=friend_count),data=pf)+
  geom_point(alpha=1/10,
             position = position_jitter(h=0),
             color='orange')+
  coord_cartesian(xlim = c(13,90),ylim = c(0,1000))+
  geom_line(stat = 'summary',fun.y=mean)+
  geom_line(stat = 'summary',fun.y=quantile,fun.args=list(probs=.9),
            linetype=2,color='blue')+
  geom_line(stat='summary',fun.y=quantile,fun.args=list(probs=.5),
            color='green')+
  geom_line(stat = 'summary',fun.y=quantile,fun.args=list(probs=.1),
            color='blue',linetype=2)

                                                                     圖2-4

 

 6.計算相關性

#使用cor.test函數來進行計算,在實際中可以對數據集進行划分
#pearson表示兩個變量之間的關聯強度的參數,越接近1關聯性越強
with(pf,cor.test(age,friend_count,method = 'pearson'))
with(subset(pf,age<=70),cor.test(age,friend_count,method = 'pearson')

7.強相關參數,通過做出www_likes_received和likes_received的散點圖來判斷兩個變量的關聯程度,從圖中看出兩個值的關聯性很大

#使用quantile來過限定一些極端值
#通過xlim和ylim實現過濾
#同時增加一條漸近線來查看整體的值
ggplot(aes(x=www_likes_received,y=likes_received),data=pf)+
  geom_point()+
  xlim(0,quantile(pf$www_likes_received,0.95))+
  ylim(0,quantile(pf$likes_received,0.95))+
  geom_smooth(method = 'lm',color='red')

                                                              圖2-5

8.通過計算月平均年齡,平均年齡和年齡分布來做出三個有關年齡和好友數關系的折線圖

   從該圖中我們可以發現p1的細節最多,p2展現的是每個年齡段不同的好友數量,p3展示的是年齡和好友數的大體趨勢

#
library(gridExtra)
pf$age_with_month <- pf$age + (12-pf$dob_month)/12
pf.fc_by_age_months <- pf %>%
  group_by(age_with_months) %>%
  summarise(friend_count_mean = mean(friend_count),
            friend_count_median = median(friend_count),
            n=n()) %>%
  arrange(age_with_months)
p1 <- ggplot(aes(x=age_with_month,y=friend_count_mean),
       data=subset(pf.fc_by_age_months,age_with_month<71))+
  geom_line()+
  geom_smooth()
p2 <- ggplot(aes(x=age,y=friend_count_mean),
             data=subset(pf.fc_by_age,age<71))+
  geom_line()+
  geom_smooth()
p3 <- ggplot(aes(x=round(age/5)*5,y=friend_count),
             data=subset(pf,age<71))+
  geom_line(stat = 'summary',fun.y=mean)


grid.arrange(p1,p2,p3,ncol=1)

習題:鑽石數據集分析

1.價格與x的關系

ggplot(aes(x=x,y=price),data=diamonds)+
  geom_point()

 

2.價格和x的相關性

with(diamonds,cor.test(price,x,method = 'pearson'))
with(diamonds,cor.test(price,y,method = 'pearson'))
with(diamonds,cor.test(price,z,method = 'pearson'))

3.價格和深度的關系

ggplot(aes(x=depth,y=price),data=diamonds)+
  geom_point()

4.價格和深度圖像的調整

ggplot(aes(x=depth,y=price),data=diamonds)+
  geom_point(alpha=1/100)+
  scale_x_continuous(breaks = seq(43,79,2))

5.價格和深度的相關性

with(diamonds,cor.test(price,depth,method = 'pearson'))

6.價格和克拉

ggplot(aes(x=carat,y=price),data=diamonds)+
  geom_point()+
  scale_x_continuous(limits = c(0,quantile(diamonds$carat,0.99)))

 7.價格和體積

volume <- diamonds$x * diamonds$y * diamonds$z
ggplot(aes(x=volume,y=price),data=diamonds)+
  geom_point()

8.子集相關特性

diamonds$volume <- with(diamonds,x*y*z)
sub_data <- subset(diamonds,volume < 800 & volume >0)
cor.test(sub_data$volume,sub_data$price)

9.調整,價格與體積

ggplot(aes(x=price,y=volume),data=diamonds)+
  geom_point()+
  geom_smooth()

10.平均價格,凈度

library(dplyr)
diamondsByClarity <- diamonds %>%
  group_by(clarity) %>%
  summarise(mean_price = mean(as.numeric(price)),
            median_price = median(as.numeric(price)),
            min_price = min(as.numeric(price)),
            max_price = max(as.numeric(price)),
            n= n()) %>%
  arrange(clarity)

11.平均價格柱狀圖(探索每種凈度和顏色的價格柱狀圖)

library(dplyr)
library(gridExtra)
diamonds_by_clarity <- group_by(diamonds, clarity)
diamonds_mp_by_clarity <- summarise(diamonds_by_clarity, mean_price = mean(price))

diamonds_by_color <- group_by(diamonds, color)
diamonds_mp_by_color <- summarise(diamonds_by_color, mean_price = mean(price))


p1 <- ggplot(aes(x=clarity,y=mean_price),data=diamonds_mp_by_clarity)+
  geom_bar(stat = "identity")


p2 <- ggplot(aes(x=color,y=mean_price),
             data=diamonds_mp_by_color)+
  geom_bar(stat = "identity")


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM