最終模型評估

給 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

子集上的迴歸

SAS proc reg 的 where 選項類似 R `lm` 的子集選項

給 SAS 使用者的 R

SAS proc reg 的 where 選項類似 R `lm` 的子集選項

給 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...