Skip to contents

Introduction

talatThaiR มีกลุ่มฟังก์ชันสำหรับดึงข้อมูลดัชนีชี้วัด 2 ประเภทหลัก ได้แก่ Price Index (ดัชนีราคา) และ Production Index (ดัชนีผลผลิต) โดยแบ่งความละเอียดออกเป็นรายเดือน (_month), รายไตรมาส (_quarter), และรายปี (_year)


ลำดับชั้นของดัชนี (Index Hierarchy)

โหมดการค้นหาของดัชนีถูกแบ่งออกเป็น 4 ระดับ (Endpoints) ซึ่งคุณต้องเลือกใช้เพียง 1 โหมดเท่านั้น:

ระดับ พารามิเตอร์ คำอธิบาย ตัวอย่าง
Sector sector = TRUE ภาพรวมระดับมหภาค (ใหญ่ที่สุด) LIVESTOCK, FISHERY, MAJOR_CROP
Category category_code หมวดสินค้า LIVESTOCK, FISHERY, MAJOR_CROP
Group group_code กลุ่มสินค้าย่อย OIL_CROP, VEGETABLE, FRUIT
Product product_code สินค้าระดับตัว BANANA_HOM_THONG, GARLIC_DRY_MIX

นอกจากนี้ ยังมีโหมด Standalone Date ซึ่งใช้ year_th คู่กับ month หรือ quarter เพื่อดึงดัชนีของสินค้าทุกตัวในช่วงเวลาที่ระบุ


ตรวจสอบรหัสดัชนี

ก่อนใช้ฟังก์ชันดัชนี คุณสามารถตรวจสอบรหัสที่มี:

# สำหรับดัชนีรายเดือน/รายปี
show_index_categories()   # หมวด: LIVESTOCK, FISHERY, MAJOR_CROP
show_index_groups()       # กลุ่ม: OIL_CROP, VEGETABLE, FRUIT, etc.
show_index_products()     # สินค้ารายเดือน/ปี: BANANA_HOM_THONG, GARLIC_DRY_MIX, etc.

# สำหรับดัชนีรายไตรมาส (ใช้แยกต่างจากดัชนีรายเดือน/ปี)
show_quarter_products()   # สินค้ารายไตรมาส: GARLIC, ORCHID, COFFEE, etc.

ดัชนีราคา (Price Index Functions)

ฟังก์ชัน ช่วงเวลา Endpoints ที่ใช้
get_price_index_month() รายเดือน 5 (sector, category, group, product, all)
get_price_index_quarter() รายไตรมาส 5 (sector, category, group, product, all)
get_price_index_year() รายปี 5 (sector, category, group, product, all)

1. การดึงข้อมูลระดับ Sector

เมื่อกำหนด sector = TRUE คุณจะได้รายชื่อสินค้าทั้งหมดในระบบ สามารถกรองด้วย year_th (ยกเว้นรายปี)

# ดัชนีผลผลิตระดับ Sector รายเดือน
prod_sector_m <- get_production_index_month(sector = TRUE)
head(prod_sector_m)

# ดัชนีราคาระดับ Sector รายปี เฉพาะปี 2567
price_sector_y <- get_price_index_year(sector = TRUE, year_th = 2567)
head(price_sector_y)

# ดัชนีราคาระดับ Sector รายปี ทุกปี
price_sector_all <- get_price_index_year(sector = TRUE)
head(price_sector_all)

หมายเหตุ: - get_production_index_year(sector = TRUE) ดึงได้เฉพาะหน้าแรกเท่านั้น (เนื่องจากข้อจำกัดของฐานข้อมูล) - หากต้องการดัชนีผลผลิตรายปี แนะนำให้ใช้ category_code หรือ group_code แทน

2. การค้นหาตาม Category, Group, หรือ Product

คุณสามารถเลือกระดับความละเอียดที่ต้องการและกรองข้อมูลเพิ่มเติมด้วย year_th, month, หรือ quarter

ดัชนีราคารายไตรมาส

# ค้นหาด้วย Category
idx_category <- get_price_index_quarter(
  category_code = "LIVESTOCK",
  quarter = 1
)
#> Found 21 records (1 page(s)) - fetching...
#> Done. 21 records retrieved.

# ค้นหาด้วย Group
idx_group <- get_production_index_month(
  group_code = "OIL_CROP",
  month = 6
)
#> Found 252 records (3 page(s)) - fetching...
#>   Fetching page 2 / 3
#>   Fetching page 3 / 3
#> Done. 252 records retrieved.

# ค้นหาด้วย Product
idx_product <- get_price_index_year(
  product_code = "GARLIC_DRY_MIX",
  year_th = 2568
)
#> Found 1 records (1 page(s)) - fetching...
#> Done. 1 records retrieved.

ดัชนีผลผลิตรายเดือน

# ค้นหาด้วย Category + month filter
prod_idx_cat_month <- get_production_index_month(
  category_code = "LIVESTOCK",
  month = 1
)
#> Found 252 records (3 page(s)) - fetching...
#>   Fetching page 2 / 3
#>   Fetching page 3 / 3
#> Done. 252 records retrieved.

# ค้นหาด้วย Group + month filter
prod_idx_group_month <- get_production_index_month(
  group_code = "OIL_CROP",
  month = 6
)
#> Found 252 records (3 page(s)) - fetching...
#>   Fetching page 2 / 3
#>   Fetching page 3 / 3
#> Done. 252 records retrieved.

# ค้นหาด้วย Product + month filter
prod_idx_prod_month <- get_production_index_month(
  product_code = "BANANA_HOM_THONG",
  month = 3
)
#> Found 252 records (3 page(s)) - fetching...
#>   Fetching page 2 / 3
#>   Fetching page 3 / 3
#> Done. 252 records retrieved.

3. โหมด Standalone Date (/all endpoint)

หากคุณไม่ใส่ Code หรือ Sector เลย แต่ระบุข้อมูลเวลา (เช่น ใส่แค่ year_th คู่กับ month หรือ quarter) ระบบจะเปลี่ยนเป็นโหมดค้นหาดัชนีของสินค้าทุกตัวในช่วงเวลานั้นๆ

# ดึงดัชนีราคาของสินค้าทุกตัวในไตรมาสที่ 4 ปี 2568
all_price_q4 <- get_price_index_quarter(year_th = 2568, quarter = 4)

# ดึงดัชนีราคาของสินค้าทุกตัว ประจำปี 2568
all_price_2568 <- get_price_index_year(year_th = 2568)

หมายเหตุ: การดึงดัชนีผลผลิตรายเดือนด้วยโหมด standalone (year_th + month) อาจเกิด database error จากฝั่ง API


Advanced Examples

เปรียบเทียบดัชนีระหวดสินค้า

# ดึงดัชนีผลผลิตรายปีของหมวดปศุสัตว์
livestock_prod_idx <- get_production_index_year(
  category_code = "LIVESTOCK"
)

livestock_prod_idx |> head()

หมายเหตุ: เกิด database error จากฝั่ง API - ข้อมูลดังกล่าวอาจไม่สามารถดึงได้ในขณะนี้

การสร้าง Heatmap ของดัชนีราคาระหวดสินค้า

# ดึงข้อมูลดัชนีราคารายไตรมาสของหมวดปศุสัตว์
livestock_q_idx <- get_price_index_quarter(
  category_code = "LIVESTOCK"
)

# สร้าง pivot table
livestock_pivot <- livestock_q_idx |>
  dplyr::filter(!is.na(price_index)) |>
  tidyr::pivot_wider(
    names_from = quarter,
    values_from = price_index,
    values_fill = NA
  ) |>
  dplyr::arrange(product_name, year_th)

# วาด heatmap (ต้องการ ggplot2)
library(ggplot2)

livestock_pivot |>
  tidyr::pivot_longer(
    cols = c("1", "2", "3", "4"),
    names_to = "quarter",
    values_to = "price_index"
  ) |>
  ggplot(aes(x = year_th, y = product_name, fill = price_index)) +
  geom_tile() +
  scale_fill_viridis_c() +
  labs(
    title = "Price Index Heatmap - Livestock",
    x = "Year (Thai Buddhist)",
    y = "Product",
    fill = "Price Index"
  )

Known Limitations

เนื่องจากข้อจำกัดของฐานข้อมูล NABC มีข้อควรระวังดังนี้:

  1. ดัชนีผลผลิตรายปี (Production Index Year) - เมื่อใช้ sector = TRUE จะดึงได้เฉพาะหน้าแรกเท่านั้น (pagination ไม่ทำงาน)
    • แนะนำ: ใช้ category_code หรือ group_code แทน sector = TRUE
  2. ดัชนีราคาและผลผลิตรายเดือน (Month Index) - เมื่อใช้โหมด year_th + month (standalone /all endpoint) อาจดึงได้เฉพาะหน้าแรกเท่านั้น
    • แนะนำ: ใช้ category_code, group_code, หรือ product_code แทน

Tips and Best Practices

  1. เริ่มจาก sector = TRUE - เพื่อดูว่ามีหมวด/กลุ่ม/สินค้าอะไรบ้างในระบบ
  2. ใช้ระดับที่แคบกว่าที่จำเป็น - category → group → product เพื่อจำกัดผลลัพธ์ที่เหมาะสม
  3. ตรวจสอบสินค้ารายไตรมาส - รายไตรมาสใช้รหัสสินค้าแยกต่างจากรายเดือน/ปี (ใช้ show_quarter_products())
  4. กรองช่วงเวลา - ระบุ month หรือ quarter กับ category_code, group_code, product_code เพื่อลดปริมาณการดึง
  5. จัดเก็บข้อมูล - ดัชนีเป็นข้อมูลที่มีคุณค่าสำหรับการวิเคราะห์ ควรจัดเก็บให้ดี