簡體   English   中英

擬合游戲的一階導數和置信區間

[英]First derivative and confidence intervals from fitted gam

我創建了可在此處獲得的模擬數據

我的數據與動物隨時間的生長有關,並具有以下變量:

read.csv(test.csv, header = T)
test$sex_t0 <- factor(test$sex_t0)
test$tagged <- factor(test$tagged)
test$scale_id <- factor(test$scale_id)

colnames(test)
[1] "weight_t" "age.x"    "sex_t0"   "tagged"   "scale_id"

test$scale_id是唯一標識符

test$tagged表示動物是否有耳標 (1) 或沒有 (0)

test$sex_t0表示動物是雄性 (m) 還是雌性 (f)

test$age.x是動物的年齡

test$weight_t代表每只動物在六個不同時間點的體重測量值,NA 顯示動物已從研究中移除

使用這些數據,我在mgcv擬合了以下 gam

gam1 <- gam(weight_t ~ 
+                tagged + 
+                sex_t0 +
+                s(age.x, by = sex_t0, k = 5) + 
+                s(scale_id, bs = "re") + 
+                s(age.x, scale_id, bs = "re"), 
+            data = test, 
+            method = "REML")

這個 gam 包括對taggedsex_t0離散固定效應,通過時間s(age.x, by = sex_t0, k = 5)對性別進行單獨的平滑函數,對個體s(scale_id, bs = "re")的隨機截距和一個個人通過時間s(age.x, scale_id, bs = "re")隨機斜率。

我現在想計算並繪制標記和未標記動物以及雄性和雌性動物隨時間/年齡變化的生長變化率。 這將顯示動物在某些年齡是否生長得更快。 為此,我們可以從擬合的 gam 中計算樣條函數的一階導數。 Gavin Simpson 在這里這里有兩篇很棒的博客文章。 還有一個從一個非常簡單的裝GAM計算一階導數的一個例子在這里 但是,我很難遵循這些示例,並且似乎無法找到一個示例,其中有人從更復雜的游戲中計算出一階導數,該游戲還包括隨機效應 - 非常感謝任何幫助

編輯:我設法從他的 GitHub 頁面這里加載了 Gavin Simpson derivSimulCI()函數,它“為加性模型中樣條項的一階導數生成后驗模擬”。 但是,這給了我一個尚未解決的錯誤。

library(devtools)
tmpf <- tempfile()
download.file("https://gist.githubusercontent.com/gavinsimpson/ca18c9c789ef5237dbc6/raw/295fc5cf7366c831ab166efaee42093a80622fa8/derivSimulCI.R",
              tmpf, method = "auto")
source(tmpf)

fd <- derivSimulCI(gam1, samples = 10000)
Error in Summary.factor(c(2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,  : 
  ‘min’ not meaningful for factors 

traceback()給出以下

7.
stop(gettextf("%s not meaningful for factors", sQuote(.Generic))) 
6.
Summary.factor(structure(c(2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 
2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L,  ... at C:\Users\taggarp\AppData\Local\Temp\Rtmp0u2MEM\file8b14519e163a#8
5.
seq(min(x), max(x) - (2 * eps), length = n) at C:\Users\taggarp\AppData\Local\Temp\Rtmp0u2MEM\file8b14519e163a#8
4.
FUN(X[[i]], ...) 
3.
lapply(X = X, FUN = FUN, ...) 
2.
sapply(model.frame(mod)[, m.terms, drop = FALSE], function(x) seq(min(x), 
    max(x) - (2 * eps), length = n)) at C:\Users\taggarp\AppData\Local\Temp\Rtmp0u2MEM\file8b14519e163a#8
1.
derivSimulCI(wt9, samples = 10000) 

我設法使用 gratia 包找到了答案......

library(gratia)

# Calculate and plot growth rate for male and female pythons across ages
# Extract first derivatives for smooths of age.x by sex_t0
fd <- fderiv(gam1, newdata = pred.dat,  term = "age.x")

# Calculate 95% confidence intervals for deriviatives
ci <- confint(fd, type = "confidence")

# Attach deriviatives and confidence intervals to pred.dat for plotting
fd.plot1 <- cbind(pred.dat, ci)

ggplot(fd.plot1, aes(x = age.x, y = est, colour = sex_t0, fill = sex_t0)) + 
  geom_line(size = 1.2) + 
  geom_ribbon(aes(ymin = lower, ymax = upper), alpha = 0.2, colour = NA) + 
  scale_colour_manual(labels = c("Female", "Male"), values = c("#F8766D", "#00BFC4")) +
  scale_fill_manual(labels = c("Female", "Male"), values = c("#F8766D", "#00BFC4")) +
  theme_classic() + 
  theme(axis.title.x = element_text(face = "bold", size = 14), 
        axis.title.y = element_text(face = "bold", size = 14), 
        axis.text.x = element_text(size = 12), 
        axis.text.y = element_text(size = 12),
        legend.text = element_text(size = 12), legend.title = element_blank()) + 
  xlab("Age (days since hatch)") + 
  ylab("Growth rate (g/day)")

在此處輸入圖片說明

我認為不可能為標記和未標記的動物產生類似的數字,因為在gam1沒有這樣的平滑

暫無
暫無

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

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