簡體   English   中英

在 ggplot 中從我的 GAM 中繪制平滑函數

[英]Plotting smooth functions from my GAM in ggplot

我創建了一個 GAM 並設置了預測,但在如何從我的模型中繪制任何平滑函數時遇到了麻煩。 一直試圖在 ggplot 中繪制這些,但現在我在一個月內添加了參數/美學問題,看到有人說也要使用 geom_smooth() 但我不確定。 如果有人能就此向我提出建議,那就太好了,我在下面添加了我的數據、模型和預測;

模型

mod = gam(co2 ~ s(timeStep, k = 200, bs = "cs") + s(month, k = 12, bs = "cc"), 
                data = carbonD,
                family = gaussian(link = "identity"))

預測

#create predictions
preds = predict(mod, type = 'terms', se.fit = TRUE) 
#combine our predictions with coefficients
fit = preds$fit + coef(mod)[1] 

數據片段

carbonD
       co2 month year timeStep
1   315.42     1 1959        1
2   316.31     2 1959        2
3   316.50     3 1959        3
4   317.56     4 1959        4
5   318.13     5 1959        5
6   318.00     6 1959        6
7   316.39     7 1959        7
8   314.65     8 1959        8
9   313.68     9 1959        9
10  313.18    10 1959       10
11  314.66    11 1959       11
12  315.43    12 1959       12
13  316.27     1 1960       13
14  316.81     2 1960       14
15  317.42     3 1960       15

有兩種方法可以在 ggplot 中繪制您的精確模型。 一種是使用geom_smooth ,但您不能使用右側的兩個變量來執行此操作。 實際上,在您的情況下,這是可能的,因為可以從時間步長計算月份,但現在讓我們忽略它,直接使用功能區和線繪制模型預測。

首先,加載所需的包並創建模型(注意因為我們只有你的數據片段,我不得不減少結的數量)

library(mgcv)
library(ggplot2)

mod = gam(co2 ~ s(timeStep, k = 4, bs = "cs") + s(month, k = 12, bs = "cc"), 
                data = carbonD,
                family = gaussian(link = "identity"))

現在我們創建一個小數據框,其中包含我們想要預測的值,在我們的數據范圍內有 1000 個點:

newdata <- data.frame(timeStep = seq(1, 15, length.out = 1000),
                      month = (seq(1, 15, length.out = 1000) - 1) %% 12 + 1)

現在我們進行預測並使用標准誤差擬合來創建上下置信帶。

pred <- predict(mod, newdata, type = 'response', se.fit = TRUE) 

newdata$co2   <- pred$fit
newdata$lower <- pred$fit - 1.96 * pred$se.fit
newdata$upper <- pred$fit + 1.96 * pred$se.fit

現在我們可以繪制我們的結果:

ggplot(carbonD, aes(timeStep, co2)) +
  geom_point() +
  geom_ribbon(data = newdata, alpha = 0.3,
              aes(ymin = lower, ymax = upper, fill = "confidence interval")) +
  geom_line(data = newdata, aes(color = "GAM")) +
  scale_fill_manual(values = "lightblue", name = NULL) +
  scale_color_manual(values = "darkblue", name = NULL) +
  theme_minimal(base_size = 16)

在此處輸入圖像描述

也可以直接在geom_smooth中使用您的 gam,但您需要能夠用yx來表達模型,其中x是時間步長。 您可以通過從時間步中減去 1,將該數字取模 12,然后再次加 1 來獲得月份,因此可以避免顯式創建預測數據框,但代價是使繪圖代碼更加復雜:

ggplot(carbonD, aes(timeStep, co2)) +
  geom_point() +
  geom_smooth(formula = y ~ s(x, k = 4, bs = "cs") + 
                            s((x - 1) %% 12 + 1, k = 12, bs = "cc"),
              method = "gam", size = 0.7,
              method.args = list(family = gaussian(link = "identity")),
              aes(color = "gam", fill = "confidence interval")) +
  scale_fill_manual(values = "lightblue", name = NULL) +
  scale_color_manual(values = "darkblue", name = NULL) +
  theme_minimal(base_size = 16)

在此處輸入圖像描述

作為對此的警告,我不清楚你應該同時擁有月份和時間步長,因為一個只是另一個的模數。 最好只使用時間步長,或者如果您想區分長期和季節性影響,則使用年份和月份。

最簡單的方法是將geom_smooth與 LOESS 一起使用: geom_smooth(method="loess", span=0.5)並使用span參數來獲得更平滑或擺動的形狀。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM