![](/img/trans.png)
[英]Error while using “micv” smooth function in predict.gam (mgcv)
[英]mgcv GAM plot and predict with tensor smooth by factor
我正在運行 GAM 以了解加利福尼亞軟體動物大小計數的時空趨勢。 我將數據計算為對空間和時間(緯度、經度、年)和北/東洋流和時間(uo、vo、年)之間的三向相互作用的響應,每個相互作用分為 3 個大小類別(小、中、大)。 這是游戲:
count_te_model.xy.vo.I = gam(count ~ size_bin +
te(latitude, longitude, year, d=c(2,1), by=size_bin) +
te(vo, uo, year, d=c(2,1), by=size_bin) +
offset(log(plots_sampled)),
data=LG_count_plot_mpa_F, family=nb(link="log"), method="REML")
summary(count_te_model.xy.vo.I)
Family: Negative Binomial(2.271)
Link function: log
Formula:
count ~ size_bin + te(latitude, longitude, year, d = c(2, 1),
by = size_bin) + te(vo, uo, year, d = c(2, 1), by = size_bin) +
offset(log(plots_sampled))
Parametric coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 2.60406 0.02163 120.411 <2e-16 ***
size_binmed 0.30197 0.03050 9.900 <2e-16 ***
size_binsmall 0.04658 0.03093 1.506 0.132
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Approximate significance of smooth terms:
edf Ref.df Chi.sq p-value
te(latitude,longitude,year):size_binlarge 44.64 51.05 630.2 <2e-16 ***
te(latitude,longitude,year):size_binmed 55.82 65.78 563.4 <2e-16 ***
te(latitude,longitude,year):size_binsmall 53.13 60.41 724.4 <2e-16 ***
te(vo,uo,year):size_binlarge 30.58 40.02 105.3 <2e-16 ***
te(vo,uo,year):size_binmed 37.54 49.24 135.8 <2e-16 ***
te(vo,uo,year):size_binsmall 53.13 67.03 266.2 <2e-16 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R-sq.(adj) = 0.429 Deviance explained = 54.4%
-REML = 15736 Scale est. = 1 n = 2944
我現在想繪制每個尺寸等級隨時間變化的豐度變化,並且想知道是否有人知道如何通過因子的 3 向交互來最好地做到這一點?
我試過“plot.gam”如下:
plot(count_te_model.xy.vo.I, all.terms=TRUE, too.far=0.05)
它產生這些圖:
我還創建了一個 xy 網格來預測 gam 輸出,然后映射。 我正在使用預測函數:
head(predict_count_coast_L)
# A tibble: 6 x 8
longitude latitude year size_bin plots_sampled uo vo model_fit
<dbl> <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl>
1 -124. 41.7 1995 large 5 1 1 0.162
2 -124. 41.7 1995 large 5 1 1 0.161
3 -124. 41.7 1995 large 5 1 1 0.160
4 -124. 41.7 1995 large 5 1 1 0.159
5 -124. 41.7 1995 large 5 1 1 0.158
6 -124. 41.7 1995 large 5 1 1 0.157
predict_count_coast_L$model_fit = predict(count_te_model.xy.vo.I,
predict_count_coast_L,type = "link",
exclude = "te(vo, uo, year, d=c(2,1), by=size_bin)")
ggplot(aes(longitude, latitude, fill= model_fit),
data=predict_count_coast_L)+
geom_tile()+
facet_wrap(~year,nrow=3)+
scale_fill_viridis("count")+
ggtitle("large individuals")+
theme_bw(10)
這會生成看起來與 gam.plot 具有完全不同模式的地圖。 (*請注意,現在年份是從上到下遞增的!)
我還在嘗試使用“鏈接”預測類型來理解輸出值......小插圖說它“在附加預測變量的規模上產生預測”,但我正在努力理解這實際上意味着什么。 這些是日志鏈接值嗎?
我也嘗試了上面的類型為“響應”而不是“鏈接”,它給了我更多不同的模式(這里只顯示給小人):
如果有人知道為什么這些會給出不同的輸出,並且如果有預測/繪制此類 GAM 的首選方法,那將不勝感激!
你正在策划兩件截然不同的事情; 通過plot()
獲得的plot()
顯示了所選平滑(您顯示的平滑plot()
的部分效果,而您是從完整模型進行預測的,因此您將所有變量/項的影響包括在模型中。
您不會像正在做的那樣排除平滑; 您應該包含要排除的平滑的名稱,與它們在summary()
生成的匯總表中顯示的完全一樣。 所以你要:
exclude = c("te(vo,uo,year):size_binlarge",
"te(vo,uo,year):size_binmed",
"te(vo,uo,year):size_binlarge")
但即使這樣做也無法獲得您想要的結果(假設您正在嘗試復制plot()
的輸出),因為其他參數項也將包含在predict()
生成的值中,並且模型截距,這將導致您的繪圖也包括組均值。
我可以看到幾個選項:
使用type = "terms"
predict
並確定與要繪制的三個平滑(平滑因子)中的每一個相關聯的結果矩陣的列。
你可以從plot()
命令中獲取輸出,然后使用該對象中的數據用 ggplot 繪制你想要的: pdat <- plot(....)
使用gratia::smooth_estimates()
評估值網格上的平滑度,然后使用該函數返回的對象和 ggplot 進行繪圖。
(我希望draw()
在 [2021] 年底之前在 {gratia} 內為 3 維和 4 維平滑工作。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.