始める無料で始める

試作モデル

2つの物体間の距離ごとに測定した重力のデータセットが与えられ、特定の距離から力を予測するシンプルなモデルを構築する課題です。まずは単回帰(線形回帰)で試してみたいと考えています。データは distanceforce の120組からなり、newton として読み込まれています。

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

Rで学ぶ特徴量エンジニアリング

コースを見る

演習の手順

  • ベースRの線形モデル関数を使って newton データに対する線形モデルを構築し、lr_force に代入します。
  • 予測値を元の newton データに結合して、新しいデータフレーム df を作成します。
  • ggplot() を使って、distance に対する force の散布図を作成します。
  • 散布図に当てはめ値を用いた回帰直線を追加します。

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

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

# Build a linear model for the newton the data and assign it to lr_force
lr_force <- ___(force ~ distance, data = ___)

# Create a new data frame by binding the prediction values to the original data
df <- newton %>% ___(lr_pred = predict(lr_force))

# Generate a scatterplot of force vs. distance
df %>%
  ggplot(aes(x = distance, y = force)) +
  geom____() +
# Add a regression line with the fitted values
  geom_line(aes(y = ___), color = "blue", lwd = .75) +
  ggtitle("Linear regression of force vs. distance") +
  theme_classic()
コードを編集して実行