最終モデルの評価

SAS ユーザーのための R

Melinda Higgins, PhD

Research Professor/Senior Biostatistician Emory University

最終演習

コースのまとめ

  • 回帰モデルを実行

    • さまざまな予測変数で
    • さまざまなグループで
  • 最良のモデルを選ぶ

    • モデル結果を保存
    • 当てはめ指標を抽出・表示

スキルを示そう

  • モデルを評価・比較

    • 可視化を用いる
  • 最良の関連を報告

    • 変数間で
    • 全体とグループ別
SAS ユーザーのための R

モデル比較

# Run lm() for diffht by bmi, save model
lmdiffhtbmi <- lm(diffht ~ bmi,
                  data = daviskeep)

# Run lm() for diffht by weight, save model
lmdiffhtwt <- lm(diffht ~ weight,
                 data = daviskeep)
# Run summary() for each model, save results
smrylmdiffhtbmi <- summary(lmdiffhtbmi)
smrylmdiffhtwt <- summary(lmdiffhtwt)
SAS ユーザーのための R

モデル比較

# Display r.squared for weight model
smrylmdiffhtwt$r.squared
# Display r.squared for bmi model
smrylmdiffhtbmi$r.squared
# Compare AICs for both models
AIC(lmdiffhtbmi, lmdiffhtwt)

[1] 0.003281645

[1] 0.00121824
            df      AIC
lmdiffhtbmi  3 788.0816
lmdiffhtwt   3 787.7052
SAS ユーザーのための R

グループ別モデル:男性 vs 女性

# Plot diffht by weight by sex
ggplot(daviskeep,
       aes(diffht, weight)) +
  geom_point() +
  geom_smooth(method = "lm") +
  facet_wrap(vars(sex)) +
  ggtitle("Height differences
          predicted by weight,
          model fit by sex")

性別別の diffht と weight のプロット

SAS ユーザーのための R

サブセットでの回帰

R の lm 関数の subset オプションのような SAS proc reg の where オプション

SAS ユーザーのための R

R の lm 関数の subset オプションのような SAS proc reg の where オプション

SAS ユーザーのための R

サブセットごとのモデル当てはめ

# lm() of diffht by weight for females
lmdiffhtwtF <- lm(diffht ~ weight,
                  subset = (sex == "F"),
                  data = daviskeep)

# lm() of diffht by weight for males
lmdiffhtwtM <- lm(diffht ~ weight,
                  subset = (sex == "M"),
                  data = daviskeep)
# Run summary() for each model save results
smrylmdiffhtwtF <- summary(lmdiffhtwtF)
smrylmdiffhtwtM <- summary(lmdiffhtwtM)
SAS ユーザーのための R

サブセットごとのモデル当てはめ

# r.squared for females only model
smrylmdiffhtwtF$r.squared
# r.squared for males only model
smrylmdiffhtwtM$r.squared

[1] 4.00807e-05

[1] 0.00804139
SAS ユーザーのための R

アワビの年齢予測モデルをいくつか作って締めくくりましょう!

SAS ユーザーのための R

Preparing Video For Download...