ใคร ๆ ก็สามารถสำรวจข้อมูลพื้นฐานได้ด้วยฟังก์ชันง่าย ๆ บน R Studio

ใคร ๆ ก็สามารถสำรวจข้อมูลพื้นฐานได้ด้วยฟังก์ชันง่าย ๆ บน R Studio

04 September 2021

สวัสดีครับทุกคน สำหรับบทความนี้ผมจะพาทุกคนไปพบกับวิธีการสำรวจข้อมูลเบื้องต้น ซึ่งในทางเทคนิคเราจะเรียกว่า การวิเคราะห์ข้อมูลเชิงสำรวจ หรือ Exploratory Data Analysis (EDA) โดยโปรแกรมที่ผมจะใช้นั้นก็เช่นเคยครับ นั่นคือ R-studio นั่นเอง ถ้าใครได้เคยอ่านบทความก่อน ๆ ที่สอนเขียนโปรแกรมภาษา R นั้นจะเห็นว่าภาษา R นั่นค่อนข้างใช้งานง่ายมากสำหรับนักวิเคราะห์ข้อมูลหรือบุคคลทั่ว ๆ ไป อีกทั้งยังมีแพคเกจที่เขียนมาให้สำหรับมือใหม่หรือผูที่ไม่ค่อยถนัดการเขียนโค้ดได้ใช้กันง่าย ๆ อีกด้วย

บทความที่เกี่ยวกับ R studio

https://temp.bdi.or.th/big-data-101/covid-19-plot-r-studio/
https://temp.bdi.or.th/big-data-101/shiny-interactive-web-apps-r-studio/

การวิเคราะห์ข้อมูลการสํารวจนั้นเป็นหนึ่งในกระบวนการที่สําคัญมากของการดําเนินการตรวจสอบเบื้องต้นในการวิเคราะห์ข้อมูล โดยแนวคิดพื้นฐานคือการตรวจสอบโครงสร้างพื้นฐานของข้อมูล การหาข้อมูลเชิงลึกในชุดข้อมูล,ค้นหารูปแบบความผิดปกติและตรวจสอบด้วยเทคนิคทางด้านสถิติ หาตัวแปรที่เป็นปัจจัยหลักหรือสำคัญกับโจทย์ของงาน การสรุปผลและการแสดงกราฟในรูปแบบต่าง ๆ

ไปลุยกันเลย!!!

ก่อนอื่นเรามาทำความรู้จักตัวช่วยหรือแพคเกจที่เราจะใช้ในการสำรวจข้อมูลกันก่อน

DataExplorer

เป็นแพจเกจหลักในบทความนี้เลยก็ว่าได้ เพราะเป็นแพคเกจที่คิดขึ้นมาสำหรับการทำ EDA โดยเฉพาะในโปรแกรม R ซึ่งเราจะได้ไปเห็นตัวอย่างในการใช้กันในบทความนี้เลยครับ

Tidyverse

เป็นแพคเกจที่นิยมใช้กันมากเลยครับในกลุ่มนักวิเคราะห์ข้อมูลหรือ Data scientist เพราะมันสามารถใช้ในงานได้อย่างครอบคลุมหรือครบวงจร หรือพูดได้ว่ามันครอบจักรวาลดาต้าเลยทีเดียว ตั้งแต่การ Import หรือนำเข้าข้อมูล, ดัดแปลงรูปแบบข้อมูล, เลือกเฉพาะบางข้อมูลหรือกรุ๊ปข้อมูล ไปจนถึงการ Visualization หรือพลอตกราฟ ได้เลยครับ (ggplot2 ก็อยู่ในจักรวาลของ Tidyverse ด้วยนะ)

เริ่มต้นการใช้งาน

วิธีการติดตั้งแพคเกจที่จำเป็นต้องใช้ ถ้าหากเครื่องใครไม่เคยใช้แพคเกจข้างต้นมาก่อนจำเป็นต้อง install package ก่อนถึงจะเรียกใช้ได้นะครับ

install.packages("DataExplorer","tidyverse")  #ติดตั้ง
library(DataExplorer)                         #เปิดใช้งานแพคเกจ DataExplorer
library(tidyverse)                            #เปิดใช้งานแพคเกจ tidyverse 

เกี่ยวกับชุดข้อมูล

ส่วนชุดข้อมูลที่เราใช้สําหรับการวิเคราะห์ข้อมูลเชิงสํารวจมีชื่อว่า gss_cat เป็นข้อมูลจากการสำรวจของกลุ่มตัวอย่างทั่วไป ซึ่งชุดข้อมูลนี้จะติดว่ากับโปรแกรม R-studio อยู่แล้วนะครับ เราจึงไม่จำเป็นต้องติดตั้งแพคเกจใด ๆ ใช้คำสั่ง library เพื่อเปิดใช้งานได้เลย

 library(forcats)  #เปิดใช้งานแพคเกจ forcats
 gss_cat           #เรียกดูชุดข้อมูล 

ภาพรวมชุดข้อมูล

 gss_cat %>% glimpse()  

ฟังก์ชันด้านบนจะทำให้เราตัวอย่างชุดข้อมูลว่าตัวแปรหรือคอลัมน์ต่าง ๆ ประกอบไปด้วยข้อมูลประเภทใดและมีข้อมูลอะไรบ้าง

Rows: 21,483
Columns: 9
$ year    <int> 2000, 2000, 2000, 2000,~
$ marital <fct> Never married, Divorced~
$ age     <int> 26, 48, 67, 39, 25, 25,~
$ race    <fct> White, White, White, Wh~
$ rincome <fct> $8000 to 9999, $8000 to~
$ partyid <fct> "Ind,near rep", "Not st~
$ relig   <fct> Protestant, Protestant,~
$ denom   <fct> "Southern baptist", "Ba~
$ tvhours <int> 12, NA, 2, 4, 1, NA, 3,~
</int></fct></fct></fct></fct></fct></int></fct></int>

ชุดข้อมูลประกอบด้วยการค่าสังเกต(จำนวนแถว) 21,483 และ 9 ตัวแปร(คอลัมน์)

gss_cat  %>% introduce() 

introduce() จะอธิบายรายละเอียดต่างๆของชุดข้อมูล เช่น มีข้อมูลสูญหายกี่แถว มีตัวแปรที่เป็นตัวเลขแบบต่อเนื่องและไม่ต่อเนื่องมีกี่ตัวแปร มีข้อมูลที่สมบูรณ์กี่แถว รวมถึงหน่วยความจำที่ใช้เก็บข้อมูลเป็นเท่าไหร่

รูปที่ 1 คำอธิบายของชุดข้อมูลด้วยคำสั่ง introduce()

สำรวจข้อมูลด้วยกราฟรูปแบบต่าง ๆ บน R-studio

ดูรายละเอียดแบบนี้บางคนอาจจะว่าดูยากใช่ไหมครับ ใช่แล้วเรามาพลอตให้เห็นกันชัดๆไปเลยดีกว่าครับ

gss_cat %>% plot_intro() 
รูปที่ 2 คำอธิบายชุดข้อมูลด้วยกราฟด้วยคำสั่ง plot_intro()

ง่ายขึ้นไหมครับ โค้ดแค่บรรทัดเดียวเราก็จะเห็นรายละเอียดข้อมูลแบบครบถ้วนในรูปแบบของกราฟง่าย ๆ แบบนี้เลย

จากกราฟเราก็จะเห็นว่า

ตัวแปรที่มีข้อมูลแบบไม่ต่อเนื่อง 66.7% และข้อมูลแบบต่อเนื่อง 33.3%

รวมถึงจำนวนแถวหรือข้อมูลที่สมบูรณ์ 52.6% และมีค่าสังเกตที่ขาดหายไปอยู่ 5.3%

คอลัมน์ทั้งหมดที่มีค่าที่หายไปคือ 0%

ทีนี้เราก็จะมาดูกันว่าข้อมูลที่หายไปนั้นอยู่ที่ตัวแปรไหนกัน?

gss_cat %>% plot_missing() 
รูปที่ 3 กราฟแสดงข้อมูลที่สุญหายจากคำสั่ง plot_missing()
gss_cat %>% profile_missing() 
รูปที่ 4 คำอธิบายข้อมูลที่สูญหายของชุดข้อมูล

ตรวจสอบการกระจายตัวและความหนาแน่นของข้อมูลด้วย Density Plot และ Histogram

*สำหรับตัวแปรที่เป็นตัวเลขนะครับ

gss_cat  %>% plot_density()
gss_cat  %>% plot_histogram() 
รูปที่ 5 กราฟแสดงการกระจายของข้อมูลด้วย density plot
รูปที่ 6 กราฟแสดงการกระจายของข้อมูลด้วย Histogram

แล้วตัวแปรที่ไม่ใช่ตัวเลขเราสำรวจอย่างไรดี?

กราฟแท่ง (Bar plot) จะสามารถนับข้อมูลประเภทนี้ได้ดีและง่ายที่สุดเลยครับ

gss_cat  %>% plot_bar() 
รูปที่ 7 กราฟแท่งแสดงปริมาณของข้อมูลที่เป็นตัวแปรเชิงคุณภาพ

การหาความสัมพันธ์ของข้อมูล

gss_cat  %>% plot_correlation() 
รูปที่ 8 กราฟแสดงความสัมพันธ์ (correlation) ของชุดข้อมูล

จากกราฟด้านบนจะเห็นว่าฟังก์ชันได้ทำการแปลงข้อมูลทั้งหมดให้เราเรียบร้อยเลยเพราะว่าถ้าข้อมูลประเภทที่เป็นกลุ่มหรือไม่ใช่ตัวเลขนั้นจะไม่สามารถหาความสัมพันธ์ได้นอกจากทำการ one-hot encoding ก่อน เราก็จะสามารถมองเห็นความสัมพันธ์ทั้งหมดของตัวแปรต่าง ๆ ได้ง่ายมากเพียงแค่สั่งคำสั่งที่เดียวเท่านั้นครับ

สรุป

การวิเคราะห์ข้อมูลเชิงสํารวจนั้นหมือนเป็นด่านแรกของการวิเคราะห์ข้อมูลเชิงลึกในลำดับถัดไปซึ่งค่อนข้างสำคัญมาก ๆ เลยทีเดียว

จะเห็นว่าฟังก์ชันที่ผมใช้แทบไม่ต้องบอกรายละเอียดข้อมูลอะไรใส่เข้าไปในโค้ดเลย แพคเกจนั้นสามารถแยกประเภทและนำคอลัมน์ที่เหมาะสมมาแสดงให้เราเห็นได้อย่างถูกต้อง หวังว่าบทความนี้จะเป็นประโยชน์ให้แก่นักวิเคราะห์มือใหม่หรือผู้ที่ไม่ค่อยชำนาญในการการเขียนชุดคำสั่งหรือโค้ดแบบผมนั้นนำไปใช้ประโยชน์ได้ไม่มากก็น้อยครับ

แหล่งที่มา: Exploratory Data Analysis (EDA) » Overview » finnstats

เนื้อหาโดย ยุรนันท์ จามจุรี

ตรวจทานและปรับปรุงโดย อนันต์วัฒน์ ทิพย์ภาวัต

Data Scientist
Government Big Data Institute: GBDi

แบ่งปันบทความ

กลุ่มเนื้อหา

แท็กยอดนิยม

แจ้งเรื่องที่อยากอ่าน

คุณสามารถแจ้งเรื่องที่อยากอ่านให้เราทราบได้ !
และเราจะนำไปพัฒนาบทความให้มีเนื้อหาที่น่าสนใจมากขึ้น

PDPA Icon

We use cookies to optimize your browsing experience and improve our website’s performance. Learn more at our Privacy Policy and adjust your cookie settings at Settings

Privacy Preferences

You can choose your cookie settings by turning on/off each type of cookie as needed, except for necessary cookies.

Accept all
Manage Consent Preferences
  • Strictly Necessary Cookies
    Always Active

    This type of cookie is essential for providing services on the website of the Personal Data Protection Committee Office, allowing you to access various parts of the site. It also helps remember information you have previously provided through the website. Disabling this type of cookie will result in your inability to use key services of the Personal Data Protection Committee Office that require cookies to function.
    Cookies Details

  • Performance Cookies

    This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

  • Functional Cookies

    This type of cookie enables the Big Data Institute (Public Organization)’s website to remember the choices you have made and deliver enhanced features and content tailored to your usage. For example, it can remember your username or changes you have made to font sizes or other customizable settings on the page. Disabling these cookies may result in the website not functioning properly.

  • Targeting Cookies

    "This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

Save settings
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.