R 具有不规则时间自相关的glmmTMB
我正在组装一个glmmTMB模型。我每年5月在一个地点收集了4年的数据。一年内的时间分辨率可以从几分钟(甚至同一分钟)到几天不等。 表示R 具有不规则时间自相关的glmmTMB,r,mixed-models,R,Mixed Models,我正在组装一个glmmTMB模型。我每年5月在一个地点收集了4年的数据。一年内的时间分辨率可以从几分钟(甚至同一分钟)到几天不等。 表示ar1()结构需要一个规则的时间序列,但是ou(times+0 | group)结构可以处理不规则的时间。这就是说,时间参数似乎是一个因素——在时间结构不规则的情况下,它是如何工作的 例如,这是否正确使用了ou()结构 df <- structure(list(DayYear = c(234, 220, 234, 231, 243, 229, 228,
ar1()
结构需要一个规则的时间序列,但是ou(times+0 | group)
结构可以处理不规则的时间。这就是说,时间
参数似乎是一个因素——在时间结构不规则的情况下,它是如何工作的
例如,这是否正确使用了ou()
结构
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%
组别(年份)%>%
突变(次数=1:n())%>%
解组()%>%
突变(YearF=as.factor(Year),
次数=numFactor(次))
mod1不,它不是:您必须在numFactor
中使用十进制时间/日期。您这样做的方式强制数据集的间距相等。下面我使用lubridate::decimal.date(DateTime)%%1
获取用作时间坐标的年份变量的分数
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)
库(dplyr)
图书馆(lubridate)
图书馆(glmmTMB)
df2%排列(日期时间)
%>%分组单位(年)
%>%突变(时间=润滑::十进制日期(日期时间)%%1)
%>%解组()
)
df3%突变(YearF=年因子),
times=glmmTMB::numFactor(times))
%>%选择(N,DayYear,YearF,times)
)
mod1那么你是说这个小插曲有一个问题,考虑到这一节的开头是“将数字时间编码在因子级别中,我们现在可以尝试Ornstein–Uhlenbeck协方差结构”?:-)这个小插曲在技术上是正确的,但不清楚。示例中使用的时间序列恰好是等距整数。。。数字时间以因子级别(而不是因子值)进行编码。
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)