简体   繁体   English

拟合游戏的一阶导数和置信区间

[英]First derivative and confidence intervals from fitted gam

I have created mock data which are available here我创建了可在此处获得的模拟数据

My data relate to the growth of animals through time and have the below variables:我的数据与动物随时间的生长有关,并具有以下变量:

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 is a unique identifier test$scale_id是唯一标识符

test$tagged indicates if the animal had an ear tag (1) or not (0) test$tagged表示动物是否有耳标 (1) 或没有 (0)

test$sex_t0 indicates if the animal was a male (m) or female (f) test$sex_t0表示动物是雄性 (m) 还是雌性 (f)

test$age.x is the age of the animal test$age.x是动物的年龄

test$weight_t represents weight measurements at six different time point for each animal, NA shows that the animals had been removed form the study test$weight_t代表每只动物在六个不同时间点的体重测量值,NA 显示动物已从研究中移除

Using this data I fit the following gam in mgcv使用这些数据,我在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")

This gam includes discrete fixed effect for tagged and sex_t0 , separate smooth functions for sexes through time s(age.x, by = sex_t0, k = 5) , a random intercept for individual s(scale_id, bs = "re") and a random slope for individuals through time s(age.x, scale_id, bs = "re") .这个 gam 包括对taggedsex_t0离散固定效应,通过时间s(age.x, by = sex_t0, k = 5)对性别进行单独的平滑函数,对个体s(scale_id, bs = "re")的随机截距和一个个人通过时间s(age.x, scale_id, bs = "re")随机斜率。

I would now like to calculate and plot the rate of change in growth for tagged and untagged animals and male and female animals through time/against age.我现在想计算并绘制标记和未标记动物以及雄性和雌性动物随时间/年龄变化的生长变化率。 This will show if animals grew more rapidly at certain ages or not.这将显示动物在某些年龄是否生长得更快。 To do this we can calculate the first derivatives of the spline functions from our fitted gam.为此,我们可以从拟合的 gam 中计算样条函数的一阶导数。 Gavin Simpson has two great blog post on this here and here . Gavin Simpson 在这里这里有两篇很棒的博客文章。 There is also an example of calculating the first derivative from a very simple fitted gam here .还有一个从一个非常简单的装GAM计算一阶导数的一个例子在这里 However, I struggle to follow these examples and can't seem to find an example where someone has calculated the first derivative from a more complex gam that also includes random effects - any help would be very much appreciated ?但是,我很难遵循这些示例,并且似乎无法找到一个示例,其中有人从更复杂的游戏中计算出一阶导数,该游戏还包括随机效应 - 非常感谢任何帮助

Edit: I managed to load Gavin Simpson derivSimulCI() function from his GitHub page here , which 'generates posterior simulations for the first derivatives of the spline terms in an additive model'.编辑:我设法从他的 GitHub 页面这里加载了 Gavin Simpson derivSimulCI()函数,它“为加性模型中样条项的一阶导数生成后验模拟”。 However, this gives me an error I am yet to solve.但是,这给了我一个尚未解决的错误。

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() gives the below 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) 

I managed to find an answer using the gratia package...我设法使用 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)")

在此处输入图片说明

I don't think it is possible to produce a similar figure for tagged and untagged animals as there is no such smooth in gam1我认为不可能为标记和未标记的动物产生类似的数字,因为在gam1没有这样的平滑

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 GAM 的一阶导数的显着变化点的测试统计是否平滑? - Test statistics for significant change points from First Derivative of GAM smooth? 如何计算“ plot.gam”的置信区间? - How are the “plot.gam” confidence intervals calculated? 如何从GAM(`mgcv :: gam`)中提取拟合的样条曲线 - How to extract fitted splines from a GAM (`mgcv::gam`) 使用 broom::tidy 为 GAM 系数和置信区间取幂 - Exponentiating GAM coefficients and confidence intervals using broom::tidy 使用 predictNLS 围绕 R 中的拟合值创建置信区间? - Using predictNLS to create confidence intervals around fitted values in R? 为时间序列回归中的拟合值差异生成渐近置信区间 - Generate asymptotic confidence intervals for difference of fitted values in time series regression 从数据框绘制置信区间 - Plotting confidence intervals from a dataframe 从 coeftest 获得置信区间? - getting confidence intervals from coeftest? 如何 plot 拟合值、观测值、置信区间和预测区间在一个 plot - How to plot Fitted values, observed values and confidence interval and prediction intervals in one plot 如何使用 R 中的 bootstrap 方法计算 beta 回归拟合值的置信区间 - How to calculate confidence intervals for fitted values of beta regression using the bootstrap method in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM