빅 데이터 분석-데이터 시각화

데이터를 이해하기 위해 시각화하는 것이 유용한 경우가 많습니다. 일반적으로 빅 데이터 애플리케이션에서 관심은 아름다운 플롯을 만드는 것보다 통찰력을 찾는 데 달려 있습니다. 다음은 플롯을 사용하여 데이터를 이해하는 다양한 접근 방식의 예입니다.

비행 데이터 분석을 시작하려면 숫자 변수간에 상관 관계가 있는지 확인하는 것부터 시작할 수 있습니다. 이 코드는bda/part1/data_visualization/data_visualization.R 파일.

# Install the package corrplot by running
install.packages('corrplot')  

# then load the library 
library(corrplot)  

# Load the following libraries  
library(nycflights13) 
library(ggplot2) 
library(data.table) 
library(reshape2)  

# We will continue working with the flights data 
DT <- as.data.table(flights)  
head(DT) # take a look  

# We select the numeric variables after inspecting the first rows. 
numeric_variables = c('dep_time', 'dep_delay',  
   'arr_time', 'arr_delay', 'air_time', 'distance')

# Select numeric variables from the DT data.table 
dt_num = DT[, numeric_variables, with = FALSE]  

# Compute the correlation matrix of dt_num 
cor_mat = cor(dt_num, use = "complete.obs")  

print(cor_mat) 
### Here is the correlation matrix 
#              dep_time   dep_delay   arr_time   arr_delay    air_time    distance 
# dep_time   1.00000000  0.25961272 0.66250900  0.23230573 -0.01461948 -0.01413373 
# dep_delay  0.25961272  1.00000000 0.02942101  0.91480276 -0.02240508 -0.02168090 
# arr_time   0.66250900  0.02942101 1.00000000  0.02448214  0.05429603  0.04718917 
# arr_delay  0.23230573  0.91480276 0.02448214  1.00000000 -0.03529709 -0.06186776 
# air_time  -0.01461948 -0.02240508 0.05429603 -0.03529709  1.00000000  0.99064965 
# distance  -0.01413373 -0.02168090 0.04718917 -0.06186776  0.99064965  1.00000000  

# We can display it visually to get a better understanding of the data 
corrplot.mixed(cor_mat, lower = "circle", upper = "ellipse")  

# save it to disk 
png('corrplot.png') 
print(corrplot.mixed(cor_mat, lower = "circle", upper = "ellipse")) 
dev.off()

이 코드는 다음과 같은 상관 행렬 시각화를 생성합니다.

그림에서 데이터 세트의 일부 변수간에 강한 상관 관계가 있음을 알 수 있습니다. 예를 들어, 도착 지연과 출발 지연은 높은 상관 관계가있는 것 같습니다. 타원이 두 변수 사이에 거의 선형 관계를 보여주기 때문에 이것을 볼 수 있지만이 결과에서 원인을 찾는 것은 간단하지 않습니다.

두 변수가 상관 관계가 있으므로 하나가 다른 변수에 영향을 미친다고 말할 수 없습니다. 또한 플롯에서 공기 시간과 거리 사이의 강한 상관 관계를 발견했습니다. 이는 거리가 멀수록 비행 시간이 늘어날 것으로 예상하는 것이 상당히 합리적입니다.

데이터의 일 변량 분석도 수행 할 수 있습니다. 분포를 시각화하는 간단하고 효과적인 방법은 다음과 같습니다.box-plots. 다음 코드는 ggplot2 라이브러리를 사용하여 상자 그림 및 격자 차트를 생성하는 방법을 보여줍니다. 이 코드는bda/part1/data_visualization/boxplots.R 파일.

source('data_visualization.R') 
### Analyzing Distributions using box-plots  
# The following shows the distance as a function of the carrier 

p = ggplot(DT, aes(x = carrier, y = distance, fill = carrier)) + # Define the carrier 
   in the x axis and distance in the y axis 
   geom_box-plot() + # Use the box-plot geom 
   theme_bw() + # Leave a white background - More in line with tufte's 
      principles than the default 
   guides(fill = FALSE) + # Remove legend 
   labs(list(title = 'Distance as a function of carrier', # Add labels 
      x = 'Carrier', y = 'Distance')) 
p   
# Save to disk 
png(‘boxplot_carrier.png’) 
print(p) 
dev.off()   

# Let's add now another variable, the month of each flight 
# We will be using facet_wrap for this 
p = ggplot(DT, aes(carrier, distance, fill = carrier)) + 
   geom_box-plot() + 
   theme_bw() + 
   guides(fill = FALSE) +  
   facet_wrap(~month) + # This creates the trellis plot with the by month variable
   labs(list(title = 'Distance as a function of carrier by month', 
      x = 'Carrier', y = 'Distance')) 
p   
# The plot shows there aren't clear differences between distance in different months  

# Save to disk 
png('boxplot_carrier_by_month.png') 
print(p) 
dev.off()