mgcv:绘图因子 'by' 平滑

mgcv: plotting factor 'by' smooths

我想绘制名为 "NO2" 的参数对出生体重的样条效应,但我想要四个四分位数的 4 个图表。我目前的代码只给出了一张图,你能帮我找出问题所在吗?你可以看到最后的代码,model_1_F1_spline是针对不同的参数进行调整的,但是我的问题是关于F1_quartile。当我按 F1_quartile 调整 NO2 时,它包括四个四分位数的结果,但我不知道如何提取这些结果并绘制 4 个图表。

这是一个可重现的例子:

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")

这是我的做法:

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()

根据你提供的拟合 GAM 系数向量,我可以推断 F1_quartile 是一个因子 by 变量,水平 1, 2, 3, 4,所以你有平滑的函数 s(NO2300_mean_total):F1_quartile1s(NO2300_mean_total):F1_quartile2s(NO2300_mean_total):F1_quartile3s(NO2300_mean_total):F1_quartile4.

在这种情况下,调用 predict.gam 应该 return 你有 5 个图,一个是你的 34 级随机截距的 Q-Q 图 s(UHF34, bs = 're'),还有 4 个图 by平滑。

您的问题主要与 by 平滑有关,因此请考虑以下最小可重现示例。

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))

请注意,您也需要将 by 作为协变量,因为因子 by smooth 受居中约束(如果不清楚,请跳过)。

现在,如果您调用 plot.gam(fit, page = 1),您将看到 4 个图:f 的每个级别平滑 s(x)

请注意,plot.gam 可以无形地 return 生成图表的数据。如果你这样做

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

你会看到 oo 是一个包含 4 个元素的列表。对于每个元素,比如 oo[[1]]$x$fit 分别给出 x 坐标和 y -坐标图,而 se 给出标准误差。 $xlab 给出变量名,$ylab 给出平滑函数名。这些数据足以让您重建 plot.gam.

的地块