Big-data-analytics-data-visualization

提供:Dev Guides
移動先:案内検索

ビッグデータ分析-データの可視化

データを理解するために、それを視覚化することがしばしば役立ちます。 通常、ビッグデータアプリケーションでは、関心は単に美しいプロットを作成するのではなく、洞察を見つけることに依存しています。 以下は、プロットを使用してデータを理解するためのさまざまなアプローチの例です。

フライトデータの分析を開始するには、数値変数間に相関関係があるかどうかを確認することから始めます。 このコードは 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()

このコードは、次の相関行列の可視化を生成します-

相関

プロットでは、データセット内のいくつかの変数間に強い相関関係があることがわかります。 たとえば、到着遅延と出発遅延は非常に相関しているようです。 これは、楕円が両方の変数の間でほぼ直線的な関係を示しているためにわかりますが、この結果から因果関係を見つけることは簡単ではありません。

2つの変数が相関しているため、一方が他方に影響を与えるとは言えません。 また、プロットでは、飛行時間と距離の間に強い相関関係があることがわかります。これは、飛行距離が長くなると、飛行時間が長くなるため、かなり合理的です。

データの単変量解析を行うこともできます。 分布を視覚化する簡単で効果的な方法は、ボックスプロット*です。 次のコードは、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()