首页 > 解决方案 > 使用“日期”作为固定效应分析时间相关数据;您是先转换为因子还是在 R 中保留为“日期”类?

问题描述

这个问题简短而有趣的是:你如何处理时间作为 R 中重复 ANOVA 的参数?

我重复观察了来自温室数据的以 3 天、7 天和 14 天为间隔的花盆中生长的植物数量。结合时间因素很重要,因为每个物种的生长速度不同,有些发芽非常快,而有些则需要几个月。调查频率的变化反映了这些变化。我正在查看治疗(2 个级别)、物种(10 个级别)和日期(n=4)的效果。我使用它作为随机效果来控制锅的变化。这一切都由以下等式总结:

Establishment ~ Date + Species + Treatment + (1|Pot)

我已经建立了我的数据的快速模拟并通过 lmer 运行它。我的困惑源于我如何处理日期变量。我是否将日期变量留在“日期”类中,还是应该将其考虑在内?也许我误解了文献,但似乎两者都可以使用,但我希望选择最合适的。

library(tidyverse)
library(lubridate)
library(lme4)

gmean <- c(.4,.5,.6,.65) #Simulate 4 means to represent date and treatment effects
sigma_g <- .1            #Standard deviation
reps <- 30               #Replicates
nspp <- 10               #Simulated number of species
ntrt <- 2                #Simulated number of treatments
n <- nspp*reps           #Total reps for simulation

tibble(est = rnorm(n, gmean, sigma_g),
       date = rep(seq(ymd('2018-04-07'),ymd('2018-05-03'), by = '1 week'), length.out=n),
       species = rep(1:nspp, each=n/nspp),
       trt  = rep(c("control","trt"), length.out=n),
       pot_id = paste0(species,"-",trt)) %>% 
  mutate_each(funs(factor(.)),species:trt) %>% 
  glimpse() -> sim_df 

mm <- lmer(est ~ date + species + trt  + (1|pot_id), data=sim_df, REML=F)
mm.factored <- lmer(est ~ factor(date) + species + trt  + (1|pot_id), data=sim_df, REML=F)

anova(mm,mm.factored)

Data: sim_df
Models:
mm: est ~ date + species + trt + (1 | pot_id)
mm.factored: est ~ factor(date) + species + trt + (1 | pot_id)
            Df     AIC     BIC logLik deviance  Chisq Chi Df Pr(>Chisq)  
mm          14 -515.80 -463.94 271.90  -543.80                           
mm.factored 15 -518.39 -462.83 274.19  -548.39 4.5909      1    0.03214 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

我的“真实”数据集有接近 6000 个对象。

标签: rdatetimetime-seriesanova

解决方案


推荐阅读