This version: 2025-02-14

1 はじめに

  • パネルデータの分析において、バランスド・パネルデータを使うことが多い
  • バランスド・パネルデータは、全ての時間において全ての個体がデータを持っているデータセットである。
  • しかし、実際のデータセットは、バランスド・パネルデータではないことが多い。
  • この資料では、アンバランスド・パネルデータからバランスド・パネルデータをRで作成する方法を説明する。
  • なお、Stataでは、fillinコマンドを使うことで、同様の処理が可能である。

2 自分でバランスド・パネルを作成する方法

2.1 データの作成

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)

# Example data: Some country-year combinations are missing
df <- data.frame(
  country = c("A", "A", "B", "C"),
  year = c(2000, 2002, 2001, 2003),
  value = c(10, 20, 30, 40)
)

# データの表示
print(df)
##   country year value
## 1       A 2000    10
## 2       A 2002    20
## 3       B 2001    30
## 4       C 2003    40

2.2 全ての組み合わせを作成

all_combinations <- expand.grid(
  country = unique(df$country),
  year = seq(min(df$year), max(df$year))
)

2.3 元のデータと組み合わせを結合

df_filled <- all_combinations %>%
  left_join(df, by = c("country", "year"))

# View result
print(df_filled)
##    country year value
## 1        A 2000    10
## 2        B 2000    NA
## 3        C 2000    NA
## 4        A 2001    NA
## 5        B 2001    30
## 6        C 2001    NA
## 7        A 2002    20
## 8        B 2002    NA
## 9        C 2002    NA
## 10       A 2003    NA
## 11       B 2003    NA
## 12       C 2003    40

3 別の方法1: plmパッケージを使う

  • 欠損を埋めて、バランスド・パネルにする。

3.1 データの作成

# 必要なパッケージを読み込む
library(plm)
## 
## Attaching package: 'plm'
## The following objects are masked from 'package:dplyr':
## 
##     between, lag, lead
library(dplyr)
# サンプルデータ: アンバランスド・パネル
df <- data.frame(
  country = c("A", "A", "A", "B", "B", "C"),
  year = c(2000, 2001, 2003, 2000, 2001, 2000),
  value = c(10, 15, 20, 25, 30, 35)
)
print(df)
##   country year value
## 1       A 2000    10
## 2       A 2001    15
## 3       A 2003    20
## 4       B 2000    25
## 5       B 2001    30
## 6       C 2000    35

3.2 欠損を埋めて、バランスド・パネルにする

  • plm::make.pbalanced(df, balance.type = c("fill"), index = c("country", "year"))は、データフレームをバランスド・パネルに限定する関数である。
  • 欠損を埋めて、バランスド・パネルにする。
# country(個体)とyear(時点)のすべての組み合わせを含むバランスド・パネルを作成
df_balanced <- plm::make.pbalanced(df, balance.type = c("fill"), index = c("country", "year"))

# 結果を表示
print(df_balanced)
##    country year value
## 1        A 2000    10
## 2        A 2001    15
## 4        A 2003    20
## 5        B 2000    25
## 6        B 2001    30
## 8        B 2003    NA
## 9        C 2000    35
## 10       C 2001    NA
## 12       C 2003    NA

4 別の方法2: BMiscパッケージを使う

  • バランスド・パネルに限定する。

4.1 データの作成

# 必要なパッケージを読み込む
library(BMisc)
library(dplyr)

# サンプルデータ: アンバランスド・パネル
df <- data.frame(
  country = c("A", "A", "A", "B", "B", "C"),
  year = c(2000, 2001, 2003, 2000, 2001, 2000),
  value = c(10, 15, 20, 25, 30, 35)
)
print(df)
##   country year value
## 1       A 2000    10
## 2       A 2001    15
## 3       A 2003    20
## 4       B 2000    25
## 5       B 2001    30
## 6       C 2000    35

4.2 バランスド・パネルに限定する

  • BMisc::makeBalancedPanel(df, idname=“country”, tname=“year”) は、データフレームdfをバランスド・パネルに限定する関数である。
# country(個体)とyear(時点)のすべての組み合わせを含むバランスド・パネルを作成
df_balanced <-BMisc::makeBalancedPanel(df, idname="country", tname="year")

# 結果を表示
print(df_balanced)
##   country year value
## 1       A 2000    10
## 2       A 2001    15
## 3       A 2003    20