Making predictions

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Noam Ross

Senior Research Scientist, EcoHealth Alliance

mgcv's predict() function

predict(log_mod2)
            1             2             3             4 
-0.8672827973 -2.9135420237 -0.4839780158 -0.1996086132 
            5             6             7             8 
-0.4416783066 -1.2351679544 -0.6148559122 -2.9135420237 
...
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Prediction types

predict(log_mod2, type = "link")
            1             2             3             4 
-0.8672827973 -2.9135420237 -0.4839780158 -0.1996086132 
            5             6             7             8 
-0.4416783066 -1.2351679544 -0.6148559122 -2.9135420237 
...
predict(log_mod2, type="response")
         1          2          3          4 
0.29582001 0.05148818 0.38131322 0.45026288 
         5          6          7          8 
0.39134114 0.22527819 0.35095230 0.05148818 
...
plogis(predict(log_mod2, type="link"))
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Standard errors

predict(log_mod2, type = "link", se.fit = TRUE)
$fit
         1          2          3          4 
-0.8672828 -2.9135420 -0.4839780 -0.1996086 
         5          6          7          8 
-0.4416783 -1.2351680 -0.6148559 -2.9135420 

$se.fit
        1         2         3         4 
0.2850848 0.1646090 0.2299404 0.2159088 
        5         6         7         8 
0.2767443 0.7601131 0.2454877 0.1646090 
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Standard errors (2)

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Predictions on new data

trained_model <- gam(response ~ s(predictor),
                     data = train_df,
                     family = binomial,
                     method = "REML")
# Test data
test_predictions <- predict(trained_model,
                            type = "response",
                            newdata = test_df) 
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Explaining predictions by terms

predict(log_mod2, type = "terms")
      s(n_acts) s(bal_crdt_ratio) s(avg_prem_balance) ... 
1     1.2115213      0.3327855673        -0.135920526 ...
2    -0.8850186     -0.4058818961        -0.135920526 ...   
3     0.5693622      0.2972364048        -0.135920526 ...  
4     0.8974704      0.3827671103        -0.135920526 ...
5     0.8974704     -0.0727464938        -0.135920526 ...
6    -0.6228781      0.1936974771        -0.135920526 ...
7     0.3642246      0.3377181800        -0.135920526 ...
8    -0.8850186     -0.4058818961        -0.135920526 ...
9     1.0209905      0.3604064595         0.317309246 ...
10    1.7675666     -0.4533384774         0.346837355 ...
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Explaining predictions by terms (2)

predict(log_mod2, type = "terms")[1, ]
           s(n_acts)    s(bal_crdt_ratio) 
          1.21152126           0.33278557 
 s(avg_prem_balance) s(retail_crdt_ratio) 
         -0.13592053           0.06789949 
  s(avg_fin_balance)      s(mortgage_age) 
         -0.04057249          -0.29183903 
       s(cred_limit) 
         -0.37055621 
plogis(sum(predict(log_mod2, type = "terms")[1, ]) + coef(log_mod2)[1])
0.29582
Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Let's practice!

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Preparing Video For Download...