r - mgcv:绘图因子 'by' 平滑

标签 r plot regression gam mgcv

我想绘制一个名为“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_quartile1 , s(NO2300_mean_total):F1_quartile2 , s(NO2300_mean_total):F1_quartile3s(NO2300_mean_total):F1_quartile4 .

遇到这种情况,请调用predict.gam应该返回 5 个图,其中一个是 34 级随机截距的 Q-Q 图 s(UHF34, bs = 're') ,以及 by 的 4 个图平滑。

您的问题主要是关于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 个图:平滑 s(x)对于 f 的每个级别.

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

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

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

关于r - mgcv:绘图因子 'by' 平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42848570/

相关文章:

r - ggplot2:如何在 geom_smooth 中获得可靠的预测置信区间?

r - 在 R 中,将同一 ID 的多个逻辑行折叠为 1 行

Python matplotlib/pylab - 3D 地毯图

r - 如何做一个荒谬的情节

r - 比较回归系数时如何解释linearHypothesis函数的结果?

machine-learning - ANN 回归、线性函数逼近

r - 使用 Shiny 来突出显示条形图中的特定条形

r - 在 paste() 中抑制 NAs

r - 将列表列表简化为单个列表

r - R 条形图的组着色错误