问题描述
我正在组装一个 glmmTMB 模型.我每年 5 月在一个站点收集了 4 年的数据.一年内的时间分辨率可以从几分钟(甚至同一分钟)到几天不等.协方差小插图说ar1()
结构需要有规律的时间序列,但是 ou(times + 0 | group)
结构可以处理不规律的时间.也就是说 - 看起来 times
参数是一个因素 - 它如何处理不规则的时间结构??
I'm putting together a glmmTMB model. I have data collected at a single site over the course of May, every year, for 4 years. Time resolution within year can range from a few minutes (or even same minute) to days apart.The covariance vignette says that the ar1()
structure requires a regular time series, but the ou(times + 0 | group)
structure can handle irregular times. That said - it looks like the times
argument is a factor - how does that work with irregular time structure??
那么,例如,这是对 ou()
结构的正确使用吗?
So, for example, is this a correct use of the ou()
structure?
df <- structure(list(DayYear = c(234, 220, 234, 231, 243, 229, 228,
223, 220, 218, 234, 237, 234, 231, 241, 237, 241, 241, 233, 234,
234, 232, 218, 227, 232, 229, 220, 223, 228, 224), DateTime = structure(c(1495477980,
1399590540, 1495479780, 1495225920, 1464631980, 1495052760, 1463324460,
1494525780, 1494256560, 1494088440, 1495471320, 1495730940, 1495476960,
1495225200, 1432919940, 1495725900, 1432924200, 1432918860, 1495384020,
1495479900, 1463848140, 1495298820, 1399420080, 1463253000, 1463692920,
1495037040, 1494275160, 1494510780, 1463348220, 1494597180), class = c("POSIXct",
"POSIXt"), tzone = ""), Year = c(2017, 2014, 2017, 2017, 2016,
2017, 2016, 2017, 2017, 2017, 2017, 2017, 2017, 2017, 2015, 2017,
2015, 2015, 2017, 2017, 2016, 2017, 2014, 2016, 2016, 2017, 2017,
2017, 2016, 2017), N = c(2, 2, 7, 2, 6, 4, 1, 4, 1, 3, 1, 6,
2, 2, 2, 2, 5, 5, 3, 5, 3, 2, 4, 1, 6, 2, 2, 3, 5, 2)), row.names = c(NA,
-30L), class = c("tbl_df", "tbl", "data.frame"))
在一年内创建采样因子
df <- df %>%
arrange(DateTime) %>%
group_by(Year) %>%
mutate(times = 1:n()) %>%
ungroup() %>%
mutate(YearF = as.factor(Year),
times = numFactor(times))
mod1 <- glmmTMB(N ~ DayYear + YearF +
ou(times + 0 | YearF),
family = nbinom2,
data = df)
这个特定模型运行得不太好,因为玩具数据集太小了(并且可能没有显示我需要显示的内容) - 但这是不规则时间序列下自相关结构的正确规范吗?
This particular model doesn't run too well because the toy dataset is so tiny (and probably doesn't show what I need showing) - but is that a correct specification of the autocorrelation structure under an irregular time series?
推荐答案
不,不是:您必须在 numFactor
中使用十进制时间/日期.你这样做的方式强制数据集等距.下面我使用 lubridate::decimal.date(DateTime) %% 1
来获取用作时间坐标的年份变量.
No, it's not: you have to use decimal times/dates in numFactor
. The way you've done it coerces the data set to be equally spaced. Below I use lubridate::decimal.date(DateTime) %% 1
to get the fraction-of-year variable that's used as the time coordinate.
library(dplyr)
library(lubridate)
library(glmmTMB)
df2 <- (df
%>% arrange(DateTime)
%>% group_by(Year)
%>% mutate(times = lubridate::decimal_date(DateTime) %% 1)
%>% ungroup()
)
df3 <- (df2
%>% mutate(YearF = as.factor(Year),
times = glmmTMB::numFactor(times))
%>% select(N, DayYear, YearF, times)
)
mod1 <- glmmTMB(N ~ DayYear + YearF +
ou(times + 0 | YearF),
family = nbinom2,
data = df3)
这篇关于glmmTMB 与不规则时间的自相关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!