I am using GAMs in the mgcv
package and have an interaction (a term I use loosely here - see here) that I want to plot. I am exploring how Pred1
changes the effect of Time
on an Outcome
and I have nested data within-person, as grouped by Person_ID
.
My understanding (from here) is that the ti()
command is perfect for deciding whether the interaction term is justified (despite coming at a cost of an additional penalty for the ti()
term, which is no big deal for me here).
However, when it comes to then presenting the interaction term to readers, I am more interested in showing them how Pred1
and Time
interact. That is, I don't want to plot just the partial effect of the interaction only (which, if I understand correctly, is modelled with the ti()
command).
I have a couple questions regarding interpretation and implementation:
Am I correct in saying that the 'full effect' (but after controlling for the covariates) of
Pred1
andTime
is given bys(Pred3) + s(Time, k=3) + ti(Pred3, Time, k=3)
, which is roughly equal tote(Pred3, Time, k=3)
(save for a few additional penalties)?Assuming 1 is correct, how do I model and plot this 'full effect' (after controlling for covariates) given that I also have covariates? My concern is that the covariates (particulalry the interaction terms) mess with the
te(Pred3, Time, k=3)
interaction term that I am interested in, but I don't have a good enough understanding of the logic 'under the hood' to know.
Here is what I have:
d <- structure(list(Person_ID=c(2668902L, 420002L, 2604402L, 7016101L,
2810104L, 2729904L, 331304L, 588010L, 2818201L, 5132702L, 2500901L,
5600903L, 2986101L, 2960202L, 5205701L, 2751002L, 133003L, 2777801L,
220402L, 134603L, 2729104L, 793903L, 2180002L, 2524001L, 8115302L,
489905L, 5042802L, 7000701L, 8166401L, 2967802L, 5088303L, 2627202L,
902702L, 1141602L, 2639302L, 1093605L, 5082404L, 328606L, 1188807L,
7003301L, 5106301L, 5504706L, 5152902L, 5042601L, 8761802L, 2700501L,
362003L, 7209802L, 2700501L, 2055602L, 8249801L, 8131703L, 237101L,
3033601L, 1271004L, 390502L, 5633603L, 2024102L, 5000602L, 2774101L,
607302L, 5186402L, 2975601L, 2104202L, 5303L, 3442901L, 2579901L,
2647301L, 3022801L, 2692903L, 3083401L, 380301L, 53902L, 5407802L,
2940304L, 5038502L, 8137902L, 8555704L, 2575001L, 5202403L, 3077003L,
734702L, 5204702L, 5603002L, 2839902L, 2855601L, 2992401L, 2185401L,
151902L, 5002103L, 2688802L, 3070502L, 5033404L, 467005L, 57204L,
2580301L, 2182602L, 3061403L, 5082408L, 264302L, 230404L, 501003L,
715103L, 2816403L, 234602L, 443303L, 8266902L, 8933302L, 2096001L,
2022202L, 544602L, 247102L, 5022804L, 7250902L, 2095802L, 2982401L,
3906302L, 3080301L, 475004L, 167101L, 179501L, 8041702L, 8719202L,
9602L, 214403L, 329203L, 3007202L, 533604L, 5094204L, 266303L,
3043702L, 2826804L, 8629501L, 756502L, 8064701L, 3088104L, 5676802L,
1312102L, 480702L, 2106803L, 8078403L, 169903L, 24804L, 5114101L,
5096603L, 5133002L, 8224303L, 2788103L, 2659902L, 2915003L, 2768401L,
5658602L, 2654603L, 2695003L, 2850102L, 2826801L, 1157302L, 5185304L,
2848301L, 3042002L, 101502L, 5101203L, 167501L, 5043004L, 5194202L,
366202L, 3044701L, 2818803L, 5071601L, 2869702L, 526202L, 8094101L,
1207403L, 5187203L, 5094403L, 336704L, 1163103L, 20206L, 5061604L,
2797801L, 8750403L, 5662302L, 5012801L, 262902L, 1013802L, 418903L,
2925301L, 3103204L, 732602L, 5660604L, 145201L, 3062603L, 218701L,
5132501L, 475403L, 5037801L, 7221101L, 3080301L, 5018602L, 8102502L
), Pred1 = c(0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L,
0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 1L,
0L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L,
0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 1L,
1L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 1L, 0L,
0L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L,
0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 0L, 0L,
0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 1L,
1L, 1L, 0L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L,
1L, 1L, 1L, 0L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 0L), Pred2 = c(42L,
44L, 58L, 48L, 26L, 22L, 42L, 23L, 52L, 48L, 52L, 29L, 37L, 47L,
56L, 46L, 33L, 55L, 48L, 46L, 30L, 19L, 37L, 47L, 50L, 45L, 48L,
44L, 56L, 36L, 32L, 45L, 41L, 43L, 37L, 27L, 40L, 51L, 38L, 36L,
65L, 44L, 58L, 59L, 28L, 49L, 32L, 44L, 53L, 65L, 53L, 29L, 57L,
51L, 55L, 50L, 19L, 60L, 48L, 41L, 51L, 42L, 53L, 51L, 38L, 30L,
43L, 61L, 59L, 20L, 71L, 44L, 61L, 39L, 26L, 40L, 56L, 27L, 49L,
21L, 25L, 46L, 45L, 40L, 45L, 44L, 62L, 47L, 59L, 30L, 69L, 56L,
27L, 23L, 44L, 51L, 31L, 41L, 38L, 60L, 49L, 45L, 22L, 27L, 49L,
41L, 29L, 44L, 38L, 56L, 73L, 56L, 34L, 31L, 46L, 49L, 34L, 40L,
26L, 59L, 54L, 72L, 59L, 57L, 29L, 32L, 32L, 40L, 28L, 56L, 60L,
24L, 51L, 53L, 59L, 19L, 29L, 48L, 48L, 36L, 22L, 48L, 22L, 69L,
42L, 64L, 21L, 21L, 54L, 59L, 47L, 30L, 26L, 40L, 46L, 56L, 33L,
22L, 34L, 53L, 47L, 34L, 54L, 30L, 41L, 54L, 41L, 30L, 71L, 41L,
56L, 55L, 59L, 34L, 36L, 35L, 33L, 36L, 53L, 44L, 35L, 33L, 52L,
52L, 38L, 56L, 45L, 23L, 50L, 25L, 54L, 24L, 49L, 54L, 47L, 50L,
46L, 38L, 54L, 64L), Pred3 = c(5, 3.33333333333333, 6, 5, 5.66666666666667,
6, 4.33333333333333, 6, 2.66666666666667, 6.33333333333333, 4.66666666666667,
5.33333333333333, 5, 6, 5, 5.33333333333333, 6, 5.33333333333333,
5.66666666666667, 6, 5.33333333333333, 3.66666666666667, 6.33333333333333,
4.66666666666667, 4, 7, 5, 4.33333333333333, 5, 5.66666666666667,
5.33333333333333, 6, 5.33333333333333, 4.33333333333333, 5.33333333333333,
4, 2.66666666666667, 3, 5.66666666666667, 6, 7, 4.33333333333333,
5.33333333333333, 4.33333333333333, 4.66666666666667, 7, 4.33333333333333,
4, 7, 6.66666666666667, 4.66666666666667, 4, 4.66666666666667,
6, 6, 4, 4.66666666666667, 4.33333333333333, 5, 5.66666666666667,
3.66666666666667, 2, 3.66666666666667, 5.33333333333333, 3.66666666666667,
4.66666666666667, 3.66666666666667, 3.66666666666667, 5.33333333333333,
5.66666666666667, 5.33333333333333, 4.33333333333333, 4, 5.66666666666667,
4.66666666666667, 6.33333333333333, 4.66666666666667, 3, 6, 5,
3.66666666666667, 5.33333333333333, 5.66666666666667, 4.33333333333333,
5.33333333333333, 3.33333333333333, 5, 6.33333333333333, 6.33333333333333,
4.66666666666667, 4.33333333333333, 5.66666666666667, 6.66666666666667,
5.66666666666667, 3.66666666666667, 4.33333333333333, 4.66666666666667,
5.66666666666667, 2, 4.33333333333333, 3, 4.33333333333333, 6.33333333333333,
7, 5.66666666666667, 5.33333333333333, 5, 4.33333333333333, 4,
5, 7, 4.66666666666667, 6.33333333333333, 3.33333333333333, 4.66666666666667,
7, 4.66666666666667, 5.66666666666667, 4.66666666666667, 6, 5.33333333333333,
5, 6, 4.33333333333333, 5.33333333333333, 4, 4, 4.33333333333333,
2.33333333333333, 5.33333333333333, 4.66666666666667, 3.33333333333333,
4, 5, 5.33333333333333, 4.66666666666667, 6, 3.33333333333333,
4.33333333333333, 7, 5, 5.33333333333333, 5.33333333333333, 6,
6, 4.33333333333333, 4.66666666666667, 6.33333333333333, 7, 4,
3.66666666666667, 6.33333333333333, 4, 3.33333333333333, 5.66666666666667,
3.33333333333333, 4.33333333333333, 5.66666666666667, 4, 5.66666666666667,
4.66666666666667, 5, 5, 6, 4.33333333333333, 5.66666666666667,
5.66666666666667, 3.66666666666667, 5.33333333333333, 5, 3.66666666666667,
5.33333333333333, 3.66666666666667, 4, 5.66666666666667, 5, 3.66666666666667,
5, 6, 4.33333333333333, 4, 5, 4, 5, 5.66666666666667, 3.33333333333333,
5.33333333333333, 4.33333333333333, 5.66666666666667, 4.66666666666667,
3.66666666666667, 6.33333333333333, 5, 4.33333333333333, 5.66666666666667,
4.33333333333333, 4.33333333333333, 5.66666666666667, 6, 7),
Time = c(1L, 1L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 1L,
2L, 3L, 1L, 4L, 1L, 4L, 2L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 2L,
1L, 3L, 1L, 2L, 2L, 1L, 4L, 2L, 2L, 1L, 1L, 1L, 2L, 4L, 2L,
4L, 2L, 1L, 1L, 2L, 4L, 3L, 1L, 2L, 2L, 3L, 3L, 3L, 4L, 1L,
3L, 1L, 4L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 4L, 1L, 1L, 1L,
3L, 3L, 1L, 1L, 3L, 1L, 3L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 3L,
1L, 3L, 4L, 3L, 4L, 2L, 1L, 3L, 2L, 3L, 3L, 1L, 1L, 2L, 1L,
2L, 2L, 1L, 1L, 1L, 1L, 3L, 2L, 2L, 1L, 2L,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L,
3L, 3L, 2L, 4L, 3L, 2L, 4L, 4L, 4L, 4L, 3L, 3L, 4L, 4L, 3L,
4L, 4L, 3L, 4L, 2L, 3L, 3L, 3L, 4L, 3L, 5L, 2L, 4L, 4L, 4L,
4L, 4L)), class = "data.frame", row.names = c(NA, -200L))
#Using ti() to assess interaction
Model1Plot <- bam(Outcome~
s(Time, k=3)+
(Pred1)+
s(Pred2)+
s(Pred3)+
ti(Pred2, Pred3)+
ti(Pred3, Time, k=3)+
s(Person_ID, bs="re"),
data=d, method="REML", gamma=1.4, family=gaussian())
summary(Model1Plot)
#Now plot - but is this only plotting the ti() portion?
vis.gam(Model1Plot, view=c("Time","Pred3"), too.far=.00,
plot.type="persp", color="gray", se=-1, theta=45, phi=25,
xlab="Time", ylab= "Pred3", zlab="Outcome",
ticktype="detailed")