'How to fix jagged line from predict_gam in ggplot2?

Data:

structure(list(ID = c(19903L, 28185L, 28207L, 28429L, 28522L, 
29092L, 29127L, 29219L, 29304L, 30981L, 31166L, 31411L, 32010L, 
33231L, 33640L, 33714L, 34093L, 34193L, 34385L, 35054L, 35337L, 
35377L, 35608L, 35881L, 35940L, 37112L, 37122L, 37125L, 37170L, 
37198L, 37266L, 37378L, 37589L, 37725L, 37877L, 38519L, 38522L, 
38605L, 38623L, 38806L, 39040L, 39083L, 39159L, 39218L, 39593L, 
39636L, 39657L, 39686L, 39700L, 39819L, 39820L, 39951L, 40151L, 
40152L, 40181L, 40226L, 40248L, 40286L, 40382L, 40556L, 40623L, 
40628L, 40798L, 40800L, 40815L, 40915L, 43282L, 43299L, 43450L, 
43466L, 43509L, 43677L, 43740L, 43762L, 43998L, 44068L, 44130L, 
44131L, 44307L, 44408L, 50679L, 50848L, 51064L, 51455L, 51690L, 
51726L, 51727L, 51796L, 52126L, 52183L, 52461L, 52500L, 52502L, 
52577L, 52614L, 53202L, 53320L, 53390L, 53456L, 53473L, 53474L, 
53475L, 53577L, 53626L, 53851L, 53873L, 54153L, 54206L, 54532L, 
54581L, 54913L, 55122L, 55267L, 55332L, 55462L, 55542L, 55612L, 
55728L, 55867L, 55903L, 55920L, 55991L, 56022L, 56098L, 56307L, 
56420L, 56679L, 56703L, 56746L, 56919L, 57005L, 57035L, 57405L, 
57445L, 57480L, 57725L, 57808L, 57809L, 57863L, 58004L, 58060L, 
58130L, 58145L, 58215L, 58229L, 58503L, 58515L, 58667L, 58999L, 
59326L, 59327L, 59344L, 59361L, 59428L, 59756L, 59865L, 60099L, 
60100L, 60169L, 60252L, 60280L, 60306L, 60384L, 60429L, 60472L, 
60493L, 60503L, 60575L, 60603L, 60662L, 60664L, 60806L, 60846L, 
60925L, 61274L, 61415L, 61727L, 61749L, 61882L, 61883L, 62081L, 
62144L, 62210L, 62285L, 62411L, 62809L, 62917L, 62934L, 62937L, 
62983L, 62989L, 63327L, 63329L, 63383L, 63458L, 63470L, 63589L, 
64081L, 64328L, 64418L, 64507L, 64596L, 65178L, 65250L, 65302L, 
65478L, 65480L, 65487L, 65565L, 65572L, 65574L, 65617L, 65802L, 
65865L, 65934L, 65935L, 65974L, 65975L, 65978L, 65991L, 65995L, 
66013L, 66154L, 66232L, 66237L, 66245L, 66314L, 66389L, 66396L, 
66460L, 66572L, 66589L, 66735L, 67174L, 73230L, 73525L, 73539L, 
73677L, 73705L, 73942L, 73953L, 74034L, 74113L, 74114L, 74425L, 
74427L, 74439L, 74607L, 74618L, 74641L, 74657L, 74794L, 74800L, 
74836L, 74942L, 74952L, 74962L, 74969L, 74975L, 74977L, 74985L, 
74989L, 75220L, 75229L, 75377L, 75407L, 75432L, 75653L, 75732L, 
75735L, 75737L, 75757L, 75895L, 75898L, 76381L, 76559L, 76574L, 
76594L, 76595L, 76746L, 76751L, 76755L, 76759L, 76775L, 77088L, 
77091L, 77099L, 77109L, 77134L, 77182L, 77188L, 77203L, 77204L, 
77252L, 77304L, 77453L, 77528L, 77556L, 77585L, 77668L, 77733L, 
77758L, 78262L, 79724L, 79730L, 79747L, 79850L, 79977L, 80052L, 
80819L, 80901L, 80932L, 81064L, 81065L, 81071L, 81098L, 81112L, 
81142L, 81175L, 81727L, 81938L, 82554L, 83744L, 83949L), Age = c(83L, 
26L, 26L, 20L, 84L, 20L, 23L, 77L, 32L, 14L, 21L, 9L, 76L, 18L, 
21L, 15L, 75L, 27L, 34L, 81L, 81L, 15L, 24L, 24L, 16L, 35L, 27L, 
7L, 30L, 31L, 24L, 24L, 79L, 30L, 19L, 78L, 25L, 20L, 42L, 62L, 
83L, 79L, 18L, 26L, 66L, 23L, 83L, 21L, 77L, 24L, 57L, 42L, 32L, 
76L, 85L, 29L, 77L, 65L, 79L, 9L, 34L, 20L, 11L, 16L, 9L, 21L, 
16L, 34L, 22L, 19L, 23L, 25L, 14L, 53L, 28L, 79L, 22L, 22L, 21L, 
82L, 81L, 16L, 19L, 77L, 15L, 18L, 15L, 78L, 24L, 16L, 14L, 29L, 
18L, 50L, 17L, 43L, 8L, 14L, 85L, 31L, 20L, 30L, 23L, 78L, 29L, 
6L, 61L, 14L, 22L, 10L, 83L, 15L, 13L, 15L, 15L, 29L, 8L, 9L, 
15L, 8L, 9L, 15L, 9L, 34L, 8L, 9L, 9L, 16L, 8L, 25L, 21L, 23L, 
13L, 56L, 10L, 7L, 27L, 8L, 8L, 8L, 8L, 80L, 80L, 6L, 15L, 42L, 
25L, 23L, 21L, 8L, 11L, 43L, 69L, 34L, 34L, 14L, 12L, 10L, 22L, 
78L, 16L, 76L, 12L, 10L, 16L, 6L, 13L, 66L, 11L, 26L, 12L, 16L, 
13L, 24L, 76L, 10L, 20L, 13L, 25L, 14L, 12L, 15L, 43L, 51L, 27L, 
15L, 24L, 34L, 63L, 17L, 15L, 9L, 12L, 17L, 82L, 75L, 24L, 44L, 
69L, 11L, 10L, 12L, 10L, 10L, 70L, 54L, 45L, 42L, 84L, 54L, 23L, 
23L, 14L, 81L, 17L, 42L, 44L, 16L, 15L, 43L, 45L, 50L, 53L, 23L, 
53L, 49L, 13L, 69L, 14L, 65L, 14L, 13L, 22L, 67L, 59L, 52L, 54L, 
44L, 78L, 62L, 69L, 10L, 63L, 57L, 22L, 12L, 62L, 9L, 82L, 53L, 
54L, 66L, 49L, 63L, 51L, 9L, 45L, 49L, 77L, 49L, 61L, 62L, 57L, 
67L, 16L, 65L, 75L, 45L, 16L, 55L, 17L, 64L, 67L, 56L, 52L, 63L, 
10L, 62L, 14L, 66L, 68L, 15L, 13L, 43L, 47L, 55L, 69L, 21L, 67L, 
34L, 52L, 15L, 31L, 64L, 55L, 13L, 48L, 71L, 64L, 13L, 25L, 34L, 
50L, 61L, 70L, 33L, 57L, 51L, 46L, 57L, 69L, 46L, 8L, 11L, 46L, 
71L, 33L, 38L, 56L, 17L, 29L, 28L, 6L, 8L), Sex = structure(c(1L, 
1L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 1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L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 
2L, 1L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 
2L, 2L, 2L, 2L, 1L, 2L, 1L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 
2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 2L, 1L, 
1L, 2L, 2L), .Label = c("Male", "Female"), class = "factor"), 
    mean_FA_scaled = c(-1.52160414281774, -1.30073487609629, 
    -1.39164271432334, -1.83373601712535, -2.19478262184568, 
    -0.47769168350816, -1.66624867866514, -0.36061779499817, 
    -1.10976759821506, -2.01706489349897, -1.21708170925372, 
    -0.68001882107227, -0.770347444019124, -1.21756680205088, 
    -1.04908755742334, -0.654272701867476, 0.791455877697352, 
    0.0263414533200063, -1.48353521852673, -1.48465744813212, 
    0.885781086077571, 0.937258844105155, -1.76609091258925, 
    -1.40930154017838, -1.42620014597815, -0.395529996012095, 
    -1.79188771313106, -1.6968602062236, -1.6213377738768, -1.26578647412735, 
    -1.3364652186935, -1.52114801078458, 0.587760344033774, -1.4860765255686, 
    -1.41824317606643, -1.08076339305916, -1.84290933912549, 
    -1.42950167307528, -0.186882171702826, 0.94192876730175, 
    -1.96157606965602, -0.668579319288362, -1.2972378638421, 
    -2.10201405453099, 0.593407693015703, -1.87521507137852, 
    -0.399874110613579, -2.16173114991939, -1.71213049306692, 
    -2.03230549555918, 0.864393561856266, 1.66450706953957, -1.76062456838238, 
    -1.42625806750617, -0.635317881823001, -1.05738481631217, 
    -0.905876579394418, 0.0731565283419971, -1.15139145628828, 
    -0.742407546940581, -1.69348627721645, 0.153573329806466, 
    -1.09929828202549, -0.982123030841461, 0.725678742439884, 
    -0.850887328730634, -0.99078229928042, 0.215368360012574, 
    -0.402661584149531, 0.0241114744912448, -0.71105027970887, 
    0.366463906043185, 0.957024565541906, 0.669292134912623, 
    1.05465854121026, 1.82844671440856, -0.181835758574102, 0.736386984932541, 
    -1.09078381740658, 0.0590019549321627, -1.02109697900777, 
    0.321350275906775, -0.0449237467173357, 0.0239956314352051, 
    0.117669222625202, -0.725516181331811, 0.387590783388401, 
    0.829691326381412, 1.37355999410519, -0.459526044282955, 
    -0.460235583001197, -0.311304854080326, 0.578796987572713, 
    0.997164184459617, 0.18257029477137, 0.291839257380694, -0.863007408468775, 
    -1.87780705975741, 2.29568520056216, 0.00319456268509986, 
    0.881190804982003, 0.930713711438919, -0.525093214001351, 
    2.54459572703618, 0.166620153992923, 1.20602921449896, -0.289055747129726, 
    1.46280982859267, -0.391909900510859, 2.11139337878521, 1.59105533181948, 
    -0.209203680563451, -0.763585105622814, -0.373635658420616, 
    0.6654186327263, -1.62880965099135, -0.961003393687248, 0.201720599972912, 
    -0.335957704443747, 0.757593504378786, -0.162251041912412, 
    0.141221563956246, 0.0760670851249914, -2.24164331007099, 
    0.424957409152164, -0.0769326311392693, -0.0363368801884033, 
    0.30505984615121, -0.551628514025415, 0.33740901955026, -0.31017538428394, 
    0.966704700912213, -1.19032920349958, 0.711567610176064, 
    0.67279638735782, -0.599819225337876, 0.0996845881750585, 
    0.656310472445189, -0.0716472917074639, -0.483100106187007, 
    -0.511691620455773, 2.1239406297925, 1.29844301245453, 0.101559797644699, 
    -1.35720112572458, 0.307058138867893, -0.0785544339238233, 
    0.27531714151305, -0.660383423073563, -0.957274695320974, 
    -1.47069111968835, -0.526229923988739, -0.645664114765535, 
    -0.887580616731169, 0.119110020634694, -0.368379279752821, 
    -1.37513507883771, 0.756384392481372, 0.0675019391690662, 
    1.18129672203451, 0.788168830982229, 0.780204620879509, 0.283447876008828, 
    0.146224535938955, -0.389296191558966, 0.807326376374772, 
    0.590410253940679, -0.41226207741881, -1.02024263646948, 
    0.0042805913354707, -0.217414057160255, 0.302561980255357, 
    -0.0445038156391923, -0.782909175408415, 0.298159944125853, 
    0.0170233274998232, -0.0487465675666421, -0.456839933421037, 
    0.310127979852941, -0.787615299560023, -0.21877521306872, 
    -0.395986128045251, -0.266386709100983, 0.372589107631277, 
    -0.47845190356342, 0.546216128061583, -0.483150787524024, 
    -0.638590448156119, 2.21420409102033, 0.550980173741211, 
    0.781797462900053, 0.0321553266949922, 0.224223113608598, 
    0.45913835087484, 0.924827436153908, 2.19646562306427, -0.622017650951458, 
    0.554498906568413, -0.0470089217260485, -0.401307668432068, 
    -0.588777934059104, 0.462266113387909, 0.263008816808847, 
    -0.162403085923465, -0.062640494100388, 0.660965915259779, 
    0.113397509933743, 0.191685695243484, 1.14629763872856, 0.407899519150338, 
    0.473039517599588, 0.589070818605222, 1.07992680780889, 0.0233440142449823, 
    0.303792812725778, 0.560066613449315, -0.401387310533095, 
    -0.286101749200717, -0.673299923821975, 1.66157479218356, 
    1.44751130500445, 0.402802424684597, 1.46472123901732, -0.397311082998703, 
    -0.641768892006205, 0.839031172774602, -0.603272796446055, 
    1.48020076738061, -0.550643848049078, 0.299513859843316, 
    0.739782634512702, 0.517841819522891, 0.240976915588321, 
    0.407841597622318, 1.04632508136641, 0.140700270204069, 0.320249766874399, 
    -0.0720093012575883, 0.191207842637321, 1.89043722977174, 
    1.44823532410469, -0.403472485541808, 1.81747058484881, 0.510261339543303, 
    0.874862878045841, -0.274271277102676, 1.60814942277632, 
    -0.625188854610541, 0.262176194843562, 0.546426093600656, 
    -0.0371912227266948, -0.0447861830882888, 1.43379838324576, 
    -0.0424331210124857, 1.86971580312266, -0.228122299652913, 
    0.731789463645971, 0.0910470403091081, 0.618791802670374, 
    0.267229848163289, 0.199251694841068, 0.246957313356364, 
    1.87125072361518, -1.40312565725327, -0.190900477709198, 
    0.257180463051856, 1.48421907338698, 0.0556569866890196, 
    -0.667601893503029, 0.247688572647614, 0.188977863808559, 
    0.91364858124609, 1.5448556730327, 0.930329981315788, 0.312119032378622, 
    1.15772266013046, -0.0360834735033167, 1.78212397237474, 
    -0.861407326257228, 0.476608931763807, 1.38366006055364, 
    0.803771442592559, 0.145174708243597, -1.13023561817905, 
    0.570130478942752, 0.862605234678655, -0.328963679935357, 
    0.654840713671687, 0.852222800781108, 0.304538552399032, 
    0.652132882236762, -0.639712677761503, 0.046078213992748, 
    -0.171257839519489, 0.349420496423362, 0.184018332971865, 
    0.149583984564103, 1.29365724620189, 0.621419992004272, -0.866656464734021, 
    1.09066401106555, 0.810541021179871, 1.62963106948065, 1.03406743799922, 
    -0.118969180099629, -0.372665472826285, 1.40028353909531, 
    0.381002209576151, 0.508378889882659, 0.667424165633985, 
    0.4092534348678, 0.813183690895774, 1.08099111588625, 0.708867018932142, 
    0.0693192271106869, 1.26885235182742, -0.117571823236151, 
    0.174801569825717, 0.584835306868775, -0.84211945742664, 
    1.05460061968224, 1.61507104537468, -1.62830066556388, 0.0799550676933195
    ), RAVLT_DELAY = c(NA, 12L, NA, NA, NA, NA, NA, NA, NA, NA, 
    NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, 
    7L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, 12L, 
    NA, NA, NA, NA, 14L, NA, NA, NA, NA, NA, 6L, 7L, NA, NA, 
    NA, NA, 7L, 1L, 1L, 11L, 4L, 12L, 7L, 9L, 9L, 8L, 14L, 12L, 
    7L, 12L, 7L, 6L, 13L, 10L, 13L, NA, 11L, 14L, 8L, 0L, 11L, 
    15L, 13L, 6L, 9L, 9L, 12L, 5L, 14L, 15L, 12L, 4L, 15L, 8L, 
    15L, 14L, 5L, 12L, 8L, 9L, 9L, 13L, 6L, 4L, 10L, NA, 4L, 
    13L, 9L, 14L, 8L, 15L, 14L, 9L, 15L, 14L, 11L, 11L, 15L, 
    12L, 9L, 13L, 14L, 7L, 13L, 9L, 12L, 10L, 6L, 9L, 10L, 11L, 
    15L, 11L, 11L, NA, 9L, 12L, 10L, 9L, 11L, 2L, 12L, NA, 6L, 
    12L, 12L, 10L, 11L, 4L, 13L, 4L, 5L, 6L, 12L, 15L, 11L, 11L, 
    14L, 2L, 11L, 5L, 10L, 12L, 10L, NA, 12L, 8L, 12L, 12L, 8L, 
    7L, 14L, 14L, 7L, 8L, NA, 9L, 6L, 15L, 7L, 14L, 8L, 14L, 
    11L, 13L, 6L, 12L, 11L, 14L, 15L, 10L, 6L, 13L, 7L, 4L, 12L, 
    14L, 7L, 13L, 3L, 13L, 7L, 10L, 6L, 8L, 3L, 15L, 11L, 15L, 
    11L, 11L, 8L, 4L, 7L, 10L, 5L, 7L, 8L, 9L, 14L, 12L, 14L, 
    12L, NA, NA, 11L, 10L, 13L, 7L, 12L, 12L, 14L, 8L, 13L, 2L, 
    11L, 8L, 7L, 4L, 7L, 9L, 4L, 12L, 14L, 15L, 12L, 13L, 9L, 
    7L, 11L, 10L, 14L, 6L, 5L, 5L, 10L, 8L, 5L, 12L, 2L, 11L, 
    8L, NA, 9L, 7L, 8L, 12L, 10L, 7L, 13L, 15L, 9L, 6L, 4L, 10L, 
    8L, 13L, 10L, 9L, 7L, 7L, 15L, 8L, 12L, 9L, 10L, 12L, 6L, 
    13L, 8L, 11L, 9L, 1L, 13L, 12L, NA, 8L, 2L, 11L, 9L, 7L, 
    6L, 10L, 13L, 15L, 6L, 5L, 7L, 5L, 5L, 11L, 11L, 13L, 9L, 
    4L, 10L, 2L, NA, 12L, 10L, 15L, NA, 6L)), row.names = c(NA, 
-324L), class = c("tbl_df", "tbl", "data.frame"))

I am using the following model in mgcv::gam:

m1 <- gam(mean_FA_scaled ~ s(Age, bs = 'ad', k = -1) + Sex +
            te(Age, by = Sex, bs ='fs') + 
            te(RAVLT_DELAY, by = Sex, bs = 'fs') + s(RAVLT_DELAY), 
            data = DF,
            method = 'REML', family = gaussian)

I would like to reproduce the gam plot:

GAM

But in ggplot. However, When I use predict_gam my plot is very jagged. This doesn't happen when I try to plot the smooth term effect on age.

# Plot
m1_p <- predict_gam(m1)

m1_p %>% 
  ggplot(aes(x = RAVLT_DELAY, y = fit)) +
  geom_line(aes(color = Sex))
  geom_smooth_ci(Sex, size = 1, alpha = 1) +
  theme_classic(base_size = 24)

ggplot



Solution 1:[1]

Your fit object has predictions for each age and each sex along the length of RAVLY_DELAY. With your existing code, each series tries to plot all the values from these various lines as one series, hence the jaggies.

If we tell ggplot to treat each Age,Sex combination as a different series (aka group), we get:

m1_p %>% 
  ggplot(aes(x = RAVLT_DELAY, y = fit)) +
  geom_line(aes(color = Sex, group = interaction(Age,Sex))) 

enter image description here

There are a lot of age groups here, which we could see separately with:

m1_p %>% 
  mutate(Age = round(Age, 1)) %>%
  ggplot(aes(x = RAVLT_DELAY, y = fit)) +
  geom_line(aes(color = Sex)) +
  facet_wrap(~Age, ncol = 10)

enter image description here

While wrong, I liked the aesthetic qualities that arose when I grouped by Age only:

enter image description here

Solution 2:[2]

I had the same problem and I finally managed to fix it, or that's what I think. I am a beginner, not an expert, so sorry for my dummy language in this field. This is happening because you have more variables in your model, apart from the ones you are plotting, that cause variance. So, what you have to do is to create a new database with all the variables you are not plotting fixed somehow, the numeric you can use means, the factors, choose one, etc. Then run the model with the function predict.gam (not "_"), that let you add a new database, that will be the one with your variables fixed.

Then predict.gam has to be turned into a database to plot it, so you bind it (the result) with your new data, and then you can use ggplot2 and geom_smooth_ci with no problem.

EXAMPLE:

model<-x~gam(s(v4, by=v3) + s(v2, by=v1)

#I want to plot the first smooth, first create the data:

new=expand.grid(v1=levels(circ$v1)[1], v2=mean(circ$v2), v3=levels(circ$v3), v4=seq(0,23, 0.1))

# see that I maintain the levels and the numbers of v3 and v4, and I fix the other ones randomly.

predict<-predict.gam(model, newdata = new, se.fit = TRUE)
mew=cbind(new,preddist2n)

mew %>%
  ggplot(aes(v4, fit)) +
  geom_smooth_ci(v3, ci_z = 1.96, ci_alpha = 0.05 )

If after you need to plot the second smooth, you should create another database to fix the variables out of the second smooth...

Tell me if it worked for you :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2