gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
206 stars 28 forks source link

delete_response() fails on a model with an offset #189

Closed gavinsimpson closed 1 year ago

gavinsimpson commented 2 years ago

See Question here https://stackoverflow.com/q/73992843/429846

df <- data.frame(
  Landings = c(6918, 4899, 43, 0, 1712, 34427, 1080, 2826, 30521, 53302, 19467, 
    98013, 399, 13915, 568, 1399, 5345, 219271, 79400, 8195, 4956, 
    634, 12963, 2430, 32003, 2598, 3772, 5759, 11695, 35459, 0, 8959, 
    66760, 30628, 52, 859, 14417, 1688, 3287, 0, 22661, 31184, 4169, 
    78, 4647, 49, 1241, 5684, 11788, 43606, 2662, 9887, 17844, 64693, 
    28943, 55279, 5321, 18504, 197130, 298, 77454, 22884, 5359, 117578, 
    14252, 16361, 3775, 4375, 4402, 39140, 13047, 2892, 21982, 458, 
    68849, 4966, 72827, 29788, 319, 20712, 10327, 11536, 21070, 2545, 
    51660, 12031, 16566, 3263, 64641, 82188, 101116, 3510, 694, 5999, 
    70, 21446, 1908, 66598, 52434, 39591),
  per.change = c(NA, NA, NA, -0.0170667179320969, -0.0310932741933139, -0.00876883009292576, 
    -0.0337896071523766, NA, -0.0383733367412093, 0.0313493941187739, 
    NA, 0.00309694472510056, 0.00440176543788437, 0.0162746873496682, 
    0.0633103545787895, -0.0304660643253923, -0.0477002654740667, 
    0.0465198702443307, 0.0848090520628948, NA, 0.00136588406239499, 
    0.02768520985601, -0.0439813588624489, 0.150138388427594, 0, 
    0.0393789469840049, -0.0339055127886893, -0.0253342452162168, 
    0.06662884765506, 0.0489644208362528, NA, NA, 0.118029428502036, 
    0.00145541011350925, NA, NA, 0.129622031181149, NA, -0.0308115567673083, 
    NA, -0.0425221990122855, 0.0294010131077341, -0.0227758522729325, 
    NA, 0.0664264452451825, -0.0542076704097344, -0.0630814426046203, 
    -0.0202991435976089, -0.0228409071757005, -0.0481243087379853, 
    -0.0222487699626362, 0.00268148684149014, 0.032275119594268, 
    0.0592311473502147, NA, -0.0402932362775077, 0.0225902785267178, 
    -0.0245393760611263, -0.0910224764171599, 0.0248344347525319, 
    -0.0132098512036838, -0.00480626865142122, -0.0207390648567119, 
    -0.0210938178547339, 0.0653168963830966, -0.0258970505397598, 
    -0.0266521730813433, NA, 0.00766938294024467, 0.0361020509477387, 
    -0.00356050066471782, -0.0303002256316303, -0.0493053708804782, 
    NA, 0.095008528099584, 0.00673520533179099, -0.0145235131366679, 
    -0.0915368065151916, NA, -0.0633484162895928, -0.0364410398900781, 
    0.0297277420555085, -0.0259575275766121, 0.000333945513698282, 
    -0.040540978212464, -0.105289527646177, 0.00931710632328382, 
    -0.00481869261842514, 0.00990973004165871, -0.0207742441119774, 
    -0.0725670373822072, -0.00197394223951389, NA, NA, 0.069556715359811, 
    NA, -0.00233517445977803, -0.118707735630186, -0.0283717012552832, 
    0.0224756418583045),
  lmb_eff = c(9348, 5383, 86.5, 1160, 1520.7, 37832, 1800.6, 9421, 24693, 
    80761, 20754, 297008.8, 3067, 10871, 1798, 3515, 19089, 261037, 
    107881, 4737, 10114, 396, 29462, 14639, 16328, 6186.5, 12572, 
    9930, 15188, 48112, 967, 16967.9, 71785, 69608.5, 742, 1492, 
    8099.5, 5723, 5218, 88, 35519, 31853, 9063, 654, 13276, 2439, 
    14262.5, 10526, 15113, 118817.9, 3646, 2808.7, 137263.9, 143763, 
    15816, 50026, 17221.6, 21516, 148777, 966, 140824.7, 35259.8, 
    6615.5, 113492, 39590, 68170, 16415, 3580, 8151, 32918, 20386, 
    3825.5, 19453, 208, 51380, 6208, 137409, 17409.8, 1028, 12007, 
    15413.6, 16622.5, 32974, 3397.5, 53812.6, 15057, 31256.6, 7124.8, 
    72063, 70913, 65447, 4228.8, 1020, 14887, 212, 14966, 10721.5, 
    58063, 100834, 43175),
  stringsAsFactors = FALSE)

m <- gam(Landings ~ s(per.change) + offset(log(lmb_eff)), data = df)
ds <- data_slice(m, per.change = evenly(per.change, n = 100),
                 Season = evenly(Season), lmb_eff = 1)