我正在使用GAM(使用mgcv软件包)来查看一种生物相对于几种环境变量的丰度变化。
我的数据如下:
library(tidyverse)
Station abd Temperature Oxygen Salinity
crossing_01_st02 343.199 10.880389 222.62732 33.24007
crossing_01_st03 3427.2116 9.896878 170.52723 33.60561
crossing_01_st04 2091.7223 10.325418 146.88088 33.71725
crossing_01_st05 3365.6303 10.447803 152.83051 33.71425
crossing_01_st06 4895.5442 10.858568 168.23048 33.68162
crossing_01_st07 4785.7697 10.221149 142.10408 33.75456
crossing_01_st08 4739.7415 10.307083 139.84557 33.78507
crossing_01_st10 3930.383 9.624018 123.82912 33.81374
crossing_01_st11 5710.4398 9.513673 124.79245 33.80772
crossing_01_st12 8213.8983 9.314383 122.80188 33.83794
crossing_01_st13 6175.7285 9.268011 135.29098 33.83685
crossing_02_st01 5952.9914 9.055155 95.48518 33.9358
crossing_02_st03 2696.3755 9.335852 116.52235 33.85236
crossing_02_st04 4072.1314 9.754846 138.25631 33.77391
crossing_02_st05 2394.5551 9.597139 128.31808 33.77155
crossing_02_st06 1744.0551 9.774439 149.00231 33.65033
crossing_02_st07 1427.5309 9.535947 176.11583 33.54753
crossing_02_st08 541.8052 10.005704 197.96463 33.40531
crossing_02_st09 173.3703 10.205607 206.16945 33.36126
crossing_02_st10 456.3426 10.622767 221.5186 33.26646
cycle_01_cast01 6823.9262 9.786862 116.12442 33.85257
cycle_01_cast02 7400.7068 9.785606 120.85602 33.83325
cycle_01_cast03 5343.2857 10.158589 139.62563 33.77581
cycle_01_cast04 5561.953 9.689744 139.47995 33.76726
cycle_01_cast05 6855.6716 9.267254 138.03602 33.79975
cycle_01_cast06 6859.4645 9.791765 134.7162 33.76183
cycle_02_cast01 330.9797 10.746551 208.04335 33.33226
cycle_02_cast03 231.1604 10.616365 209.49808 33.31173
cycle_02_cast04 544.3918 10.565562 205.8037 33.34717
cycle_02_cast05 235.6214 10.38223 197.70585 33.39902
cycle_02_cast07 182.0388 10.540305 208.21308 33.33308
cycle_02_cast08_2 1187.5021 10.414415 201.71851 33.37685
cycle_02_cast09 1656.9597 9.978496 186.95632 33.48471
cycle_03_cast01 4816.7298 9.416381 100.71815 33.90358
cycle_03_cast02 5514.5958 9.427468 101.28535 33.90143
cycle_03_cast03 4436.9845 9.528815 107.20829 33.88003
cycle_03_cast04 5334.6542 9.40963 117.85671 33.82466
cycle_03_cast05 8826.6174 9.410874 116.09786 33.82881
cycle_03_cast06 8792.56 9.257086 111.92037 33.84714
cycle_03_cast08 8110.7264 9.372722 123.3256 33.82041
cycle_04_cast01 211.0595 11.554293 230.22028 33.20413
cycle_04_cast04 179.7914 11.197575 225.20708 33.23228
cycle_04_cast05 166.5575 11.393151 226.56168 33.21736
cycle_04_cast06 147.1021 11.379475 228.57757 33.20565
cycle_04_cast07 138.6963 11.339498 226.59277 33.21873
cycle_05_cast01 617.0933 10.538117 195.79625 33.49209
cycle_05_cast02 763.2103 9.854859 181.83223 33.50503
cycle_05_cast03 1454.6652 10.719085 208.2208 33.40604
cycle_05_cast04 940.1714 10.695348 211.12725 33.35623
cycle_05_cast05 984.9135 11.114098 214.13466 33.42644
p1408_008 429.6539 9.955556 117.60191 33.79807
p1408_009 551.5781 10.109037 129.78458 33.71664
p1408_010 1090.7607 10.096017 139.46102 33.64799
p1408_011 1008.6421 9.773443 121.056 33.7683
p1408_012 1018.451 10.009549 127.55012 33.72919
p1408_013 1478.1 9.837543 132.82643 33.71031
p1408_014 634.3147 9.171992 143.98665 33.69484
p1408_019 1261.4535 9.925149 144.02358 33.66018
p1408_020 895.1759 10.281067 157.89194 33.57496
p1408_021 894.1513 9.941134 137.3812 33.70525
p1408_022 1013.2815 10.012316 133.53466 33.71364
p1408_023 1465.2027 9.998437 132.7186 33.72103
p1408_024 1387.3383 10.023705 133.01306 33.70337
p1408_025 1131.3854 9.798109 126.38143 33.77076
p1408_026 1920.4206 9.796771 124.29865 33.78191
p1408_027 1518.2211 9.706037 115.40608 33.83985
p1408_028 1351.4425 9.71432 115.06117 33.83702
p1408_029 1776.763 9.819473 123.35006 33.7771
p1408_032 169.5453 9.601247 182.55748 33.46663
p1408_033 185.7481 9.459888 188.18871 33.44482
p1408_034 533.2673 9.805629 167.51825 33.50092
p1408_035 918.7359 9.430061 180.51512 33.49057
p1408_036 257.1976 9.674115 171.3 33.51555
p1408_037 905.7629 9.676409 171.52495 33.49375
p1408_038 867.7851 9.408992 161.45491 33.59505
p1408_039 597.6163 9.68413 188.2719 33.4009
p1408_040 1785.0849 9.474682 170.19411 33.54021
p1408_041 668.1221 9.448858 177.52229 33.506
p1408_051 166.1487 10.980822 216.65745 33.16259
p1408_056 106.383 11.867383 228.92551 33.0945
p1408_058 226.3468 12.124444 228.97567 33.08706
p1604_02 2527.2334 12.074301 199.61586 33.28931
p1604_03 3380.0072 11.922921 195.74938 33.30905
p1604_05 3675.7893 11.671741 188.88949 33.33841
p1604_09 3453.929 11.735068 190.83477 33.34591
p1604_10 1189.6463 12.264773 218.65712 33.18436
p1604_11 1196.9789 12.450878 220.982 33.1585
p1604_12 2027.3274 11.793328 210.32928 33.21888
p1604_13 1441.1081 12.653082 222.26812 33.17408
p1604_14 963.5869 12.326745 219.86165 33.17834
p1604_15 1613.0636 12.393148 219.8886 33.14464
p1604_16 658.4892 12.750862 223.2642 33.14848
p1604_17 1309.0813 12.471335 221.47351 33.15247
p1604_18 1354.5372 12.216455 218.88083 33.12412
p1604_19 1434.5431 12.657842 221.77662 33.1185
p1604_20 1062.6206 12.322844 220.29652 33.14848
p1604_21 1941.8302 12.782927 224.20348 33.089
p1604_23 3401.0905 10.280862 182.66022 33.51492
p1604_24 3502.4929 9.71893 163.12155 33.5462
p1604_25 4742.5206 9.772787 158.93855 33.55103
p1604_26 5364.7968 9.767572 151.34155 33.62338
p1604_27 3940.3145 9.472281 165.1516 33.6148
p1604_29 5670.4874 9.677677 169.85598 33.54579
p1604_30 4986.9538 9.987309 180.425 33.50413
p1604_33 5215.7347 9.530931 170.52985 33.57246
p1604_36 3154.6535 9.472636 110.74072 33.86774
p1604_37 3598.488 9.155606 101.19137 33.93614
p1604_38 3733.3041 9.238361 109.37808 33.87358
p1604_39 3777.4703 9.34017 112.88268 33.84185
p1604_40 4474.8835 9.165892 108.03168 33.88497
p1604_42 4838.1334 8.980138 109.30434 33.89938
p1604_43 4931.4312 8.925082 107.71142 33.91524
p1604_45 6332.5305 8.927316 111.68705 33.88828
在运行GAM之前,我进行了简单的图形检查,以查看我的丰度如何随变量而变化。使用ggplot,代码如下所示:
df %>%
gather(variable, value, -Station, -abd) %>%
ggplot(aes(x = value, y = abd)) +
geom_point() +
stat_smooth(aes(color = "lm"), method = "lm", geom = 'line') +
stat_smooth(aes(color = "gam"), method = "gam", formula = y ~ s(x),geom = 'line') +
scale_y_log10() +
facet_wrap(~variable, scales = "free_x") +
theme_bw()
并提供以下图表:
在这里我可以看到每个变量的趋势。例如,我可以看到我的丰度似乎随着温度的升高而降低,或者随着氧气的增加而出现单调的丰度降低。
- 现在,在运行GAM时,我遇到了一些奇怪的反应。 乍一看,我将像这样运行GAM:
library(mgcv)
df.gam <- gam(log10(abd) ~ s(Oxygen) + s(Temperature) + s(Salinity), data = df, method = "REML")
输出告诉我,所有三个变量均具有显着影响,并且总共可以解释74%的偏差。 但是,当我绘制响应曲线(使用图或此处使用visreg)时,它会出现“问题”
library(visreg)
visreg(df.gam, scale = "response", rug = F, line = list(lwd = 1))
现在,曲线的行为与ggplot和stat_smooth输出完全不同(即使没有stat_smooth,我也可以说预测的曲线不适合数据??
为了增加复杂性,如果我逐个对每个变量运行单变量GAM并查看它们的响应曲线,则它们实际上与ggplot输出一致。
这种差异是否是由于GAM的累加性质造成的,例如,如果我加上氧气或盐度的影响,我的丰度对温度的响应会有所不同(基本上是从减少到增加...)?
这是GAM函数的某种奇怪响应吗?
答案 0 :(得分:1)
撤消了我的密切投票。在显示变量和氧气之间令人费解的关系的两个变量上使用te()
函数,并在结果上调用plot.gam
,这样相互作用就可以不是线性的。
df.gam <- gam(log10(abd) ~ te(Oxygen, Temperature) + s(Salinity), data = df,
method = "REML")
#-----
> ?plot.gam
> plot(df.gam)
Hit <Return> to see next plot:
Hit <Return> to see next plot:
O 2 和温度都可以促进生长或丰度,但是氧气在液态水中的溶解度与温度成反比,因此这种关系似乎是合理的。