Search code examples
rplotregressiongammgcv

mgcv: plotting factor 'by' smooths


I want to plot spline effect of a parameter called "NO2" on birthweight, but I want 4 graphs for four quartiles. My current code gives only one graph, could you please help me to figure out the problem? You can see the code at the end, model_1_F1_spline is adjusted for different parameters, but my question is about F1_quartile. When I adjust NO2 by F1_quartile, it includes results for four quartiles, but I don't know how to extract those results and draw 4 graphs.

Here is a reproducible example:

structure(list(coefficients = structure(c(2779.15322482481, 11.6029323631846, 
-109.637722127332, -70.5777182211836, -33.2026137282293, 1.34507275289371, 
-104.16616170941, -84.3138020433217, 17.079775791272, 49.2699120523702, 
65.7993773354024, 73.9523088264003, 62.1308005103464, 11.8305504033343, 
17.2509811135892, 34.167485824927, 37.5379409075558, 39.4891005510156, 
2.08045456267659, 95.0617726758795, 159.185162814325, 216.767405256274, 
30.4053773772453, 67.9509936017346, 75.9715680793893, 76.0634702947319, 
197.304475883704, 346.536371507916, 452.520999581153, 582.904282791219, 
646.972345369266, -13.117918823958, -21.2577276011179, -36.4775602045112, 
-2.53495678184362, 4.25561833400684, -4.24061504987865, 1.22183358211853, 
-17.6781972182122, -13.9465039223737, -24.9221422877004, -26.5305128528655, 
2.72740931108257, 17.3508955652218, -4.33132009995294, -11.4103790176564, 
48.1115836583216, -23.8853869176324, -11.9906695483978, 0.159117077270929, 
3.1823388043623, -30.2233558177321, 22.9158634128136, 1.86241593993877, 
-7.46279510854093, -17.7265172939209, 15.6908002520418, 10.7367940888643, 
11.9368630460758, 48.0464522543244, -10.5383667390476, 8.84142833076189, 
38.6344171322845, -4.18823289724547, 20.9039579936433, -27.1572322476693, 
-23.3055121479652, -10.125234127069, -2.3505578660444, -5.59801575548779, 
21.0487614265911, -0.113655733751338, 1.4592300415459, -0.395003023852113, 
-1.33572259818002, -0.195697887437374, -1.22245366980104, 0.161927450428184, 
-8.83284987935688, -11.7655241486702, 10.0814083754381, 4.95053998927621, 
0.0512729497898481, -2.47612645668306, -0.324705343736638, -2.73702305143146, 
0.367899109531455, -17.8006136959884, -20.7138572162521, 1.66439599003613, 
0.991339450831016, -0.094477049206764, -0.333359963322134, -0.0535341357101135, 
-0.166135609567417, 0.0263694684353763, -0.790300658406237, -7.88088655871398, 
2.30124665956728, 0.526763779856579, -0.729268724581621, -1.64502812073609, 
0.245438533444878, -1.68875200672467, 0.471404077584143, -12.0519624220913, 
-8.61178665100117), .Names = c("(Intercept)", "M_ethni_cat3FB White", 
"M_ethni_cat3USB Black", "M_ethni_cat3FB Black", "M_ethni_cat3USB Hispanic", 
"M_ethni_cat3FB Hispanic", "M_ethni_cat3USB Asian", "M_ethni_cat3FB Asian", 
"M_Age_Cat1", "M_Age_Cat2", "M_Age_Cat3", "M_Age_Cat4", "M_Age_Cat5", 
"M_EDU_Cat1", "M_EDU_Cat2", "M_EDU_Cat3", "M_EDU_Cat4", "M_EDU_Cat5", 
"MEDICAID1", "prepregBMI_4cat1", "prepregBMI_4cat2", "prepregBMI_4cat3", 
"PNC_RECEIVED1", "Parity_Cat1", "Parity_Cat2", "Parity_Cat3", 
"gest_clin38", "gest_clin39", "gest_clin40", "gest_clin41", "gest_clin42", 
"concept_year2008", "concept_year2009", "concept_year2010", "conc_season_num2", 
"conc_season_num3", "conc_season_num4", "s(UHF34).1", "s(UHF34).2", 
"s(UHF34).3", "s(UHF34).4", "s(UHF34).5", "s(UHF34).6", "s(UHF34).7", 
"s(UHF34).8", "s(UHF34).9", "s(UHF34).10", "s(UHF34).11", "s(UHF34).12", 
"s(UHF34).13", "s(UHF34).14", "s(UHF34).15", "s(UHF34).16", "s(UHF34).17", 
"s(UHF34).18", "s(UHF34).19", "s(UHF34).20", "s(UHF34).21", "s(UHF34).22", 
"s(UHF34).23", "s(UHF34).24", "s(UHF34).25", "s(UHF34).26", "s(UHF34).27", 
"s(UHF34).28", "s(UHF34).29", "s(UHF34).30", "s(UHF34).31", "s(UHF34).32", 
"s(UHF34).33", "s(UHF34).34", "s(NO2300_mean_total):F1_quartile1.1", 
"s(NO2300_mean_total):F1_quartile1.2", "s(NO2300_mean_total):F1_quartile1.3", 
"s(NO2300_mean_total):F1_quartile1.4", "s(NO2300_mean_total):F1_quartile1.5", 
"s(NO2300_mean_total):F1_quartile1.6", "s(NO2300_mean_total):F1_quartile1.7", 
"s(NO2300_mean_total):F1_quartile1.8", "s(NO2300_mean_total):F1_quartile1.9", 
"s(NO2300_mean_total):F1_quartile2.1", "s(NO2300_mean_total):F1_quartile2.2", 
"s(NO2300_mean_total):F1_quartile2.3", "s(NO2300_mean_total):F1_quartile2.4", 
"s(NO2300_mean_total):F1_quartile2.5", "s(NO2300_mean_total):F1_quartile2.6", 
"s(NO2300_mean_total):F1_quartile2.7", "s(NO2300_mean_total):F1_quartile2.8", 
"s(NO2300_mean_total):F1_quartile2.9", "s(NO2300_mean_total):F1_quartile3.1", 
"s(NO2300_mean_total):F1_quartile3.2", "s(NO2300_mean_total):F1_quartile3.3", 
"s(NO2300_mean_total):F1_quartile3.4", "s(NO2300_mean_total):F1_quartile3.5", 
"s(NO2300_mean_total):F1_quartile3.6", "s(NO2300_mean_total):F1_quartile3.7", 
"s(NO2300_mean_total):F1_quartile3.8", "s(NO2300_mean_total):F1_quartile3.9", 
"s(NO2300_mean_total):F1_quartile4.1", "s(NO2300_mean_total):F1_quartile4.2", 
"s(NO2300_mean_total):F1_quartile4.3", "s(NO2300_mean_total):F1_quartile4.4", 
"s(NO2300_mean_total):F1_quartile4.5", "s(NO2300_mean_total):F1_quartile4.6", 
"s(NO2300_mean_total):F1_quartile4.7", "s(NO2300_mean_total):F1_quartile4.8", 
"s(NO2300_mean_total):F1_quartile4.9"))), .Names = "coefficients")

Here is how I do:

model_1_F1_spline <- gam(BWGT~ s(UHF34,bs="re") + s(NO2300_mean_total, by=F1_quartile)+M_ethni_cat3 + M_Age_Cat + M_EDU_Cat + MEDICAID + 
                          prepregBMI_4cat + PNC_RECEIVED + Parity_Cat + gest_clin + concept_year + conc_season_num, data=births_stressors, method="REML")

png(filename="plot_factor1_spline.png")
plot(model_1_F1_spline, ylab="Change in birth weight (g)", xlab="NO2")
dev.off()

Solution

  • From your provide coefficient vector of your fitted GAM, I could infer that F1_quartile is a factor by variable, with levels 1, 2, 3, 4, so that you have smooth functions s(NO2300_mean_total):F1_quartile1, s(NO2300_mean_total):F1_quartile2, s(NO2300_mean_total):F1_quartile3 and s(NO2300_mean_total):F1_quartile4.

    In this situation, calling predict.gam should return you 5 plots, one being a Q-Q plot of your 34-level random intercept s(UHF34, bs = 're'), and 4 plots for the by smooths.

    Your question is mainly regarding the by smooths, so consider the following minimal reproducible example.

    dat <- data.frame(y = rnorm(40), x = runif(40), f = gl(4, 10))
    library(mgcv)
    fit <- gam(y ~ f + s(x, k = 5, by = f))
    

    Note that you need to put by as a covariate, too, as factor by smooth is subject to centering constraint (if unclear of this, skip it).

    Now if you call plot.gam(fit, page = 1), you will see 4 plots: a smooth s(x) for each level of f.

    Note that plot.gam can invisibly return data generating the plots. If you do

    oo <- plot.gam(fit, page = 1)
    

    you will see that oo is a list of 4. For each element, say oo[[1]], $x and $fit gives respectively the x-coordinate and y-coordinate of the plot, while se gives standard error. $xlab gives variable name, $ylab gives smooth function name. These data are sufficient for you to reconstruct the plots by plot.gam.