予測値を手動で計算する
単回帰と同様に、モデルの係数から予測値を手動で計算することもできます。平行傾斜の場合に唯一変わる点は、カテゴリー変数の各カテゴリーごとに切片が異なることです。つまり、各カテゴリーが発生する場合を個別に考える必要があります。
taiwan_real_estate、mdl_price_vs_both、explanatory_data が使用できます。また、前の演習で作成した ic_0_15、ic_15_30、ic_30_45、slope も読み込まれています。
この演習はコースの一部です
statsmodels を使った中級 Regression
演習の手順
conditionsリストを定義します。このリストには、house_age_yearsが"0 to 15"である、house_age_yearsが"15 to 30"である、house_age_yearsが"30 to 45"である、という3つの条件文を含めます。choicesリストを定義します。このリストには、mdl_price_vs_bothから抽出した切片を、それぞれの条件に対応する形で含めます。np.select()を使って切片の配列を作成します。prediction_dataを作成します。explanatory_dataを基に、interceptには切片の配列を、price_twd_msqには手動で計算した予測値を割り当てます。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# Define conditions
conditions = [____]
# Define choices
choices = [____]
# Create array of intercepts for each house_age_year category
intercept = ____(____, ____)
# Create prediction_data with columns intercept and price_twd_msq
prediction_data = ____.____(
____ = ____,
____ = ____ + ____ * ____[____])
print(prediction_data)