始める無料で始める

直前値の保持(LOCF)

もう一つ重要な補完手法が「Last observation carried forward(LOCF)」です。この方法は、欠損値をその直前に現れた NA ではない最新の値で埋めることで機能します。

この演習では、学生の月次試験スコアの時系列 monthly_test_scores を補完します。データセットと、ggplot2zoo パッケージはあらかじめ利用可能です。

この演習はコースの一部です

Rで学ぶ時系列データ操作

コースを見る

演習の手順

  • last observation carried forward を用いて monthly_test_scores の欠損値を補完し、monthly_scores_locf として保存します。

  • 最初の autoplot() を完成させ、元の monthly_test_scores の時系列プロットを作成します。

  • monthly_scores_locf を 'minimal' テーマで自動プロットし、y 軸ラベルを "Examination Score (LOCF)" にします。

  • プロット間を切り替えて、monthly_scores_locf のプロットと monthly_test_scores のプロットを比較します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Impute using last observation carried forward
___ <- ___

# Create a plot of the original time series 
autoplot(___) + 
  theme_minimal() + 
  labs(y = "Examination Score")

# Autoplot with theme and axis title and compare to the above plot
autoplot(___) + 
  ___
コードを編集して実行