始める無料で始める

より良いモデルを作る

前のセクションでは、year 特徴量だけを使って life expectancy を当てはめる単純なモデル群を作成しました。前回の分析から、これらの一部は当てはまりが良くないことが分かりました。

この演習では、利用可能なすべての特徴量を使って、各国ごとに重回帰モデルを構築します。とくに当てはまりが悪かった4つのモデルの性能を比較したい場合のために、調整済み \(R^2\) を以下に示します。

Country Adjusted \(R^2\)
Botswana -0.0060772
Lesotho -0.0169851
Zambia 0.1668999
Zimbabwe 0.2083979

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

Tidyverse で学ぶ Machine Learning

コースを見る

演習の手順

  • データセット内のすべての特徴量を使って、各国の life_expectancy を予測する線形モデルを作成します。
  • 各モデルの当てはまり統計量を含む列(fit)を追加し、このデータフレームを簡潔化します。
  • worst_fit データフレームにある4か国について、fullmodel_perf の調整済み \(R^2\) を出力してください。

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

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

# Build a linear model for each country using all features
gap_fullmodel <- gap_nested %>% 
  mutate(model = map(data, ~lm(formula = ___, data = .x)))

fullmodel_perf <- gap_fullmodel %>% 
  # Extract the fit statistics of each model into data frames
  mutate(fit = map(model, ~___(.x))) %>% 
  # Simplify the fit data frames for each model
  unnest(___)
  
# View the performance for the four countries with the worst fitting four simple models you looked at before
fullmodel_perf %>% 
  ___(country %in% worst_fit$country) %>% 
  select(country, adj.r.squared)
コードを編集して実行