sim_ema: Simulate EMA data

Description Usage Arguments Value Examples

Description

Simulate EMA data

Usage

1
2
3
sim_ema(plan = sample_plan(), mm_par = list(fixed = c(intercept = 0,
  time = 0, time2 = 0), random = c(intercept = 0, time = 0, time2 = 0),
  error = 0.02, phi = 0), lim = NULL)

Arguments

plan

a sample plan, as generated by 'sample_plan'

mm_par

a list, defining the parameters of a mixed effects model; the list should contain elements "fixed", "random", "error" and "phi".

lim

optional upper and lower limits of data (defaults to NULL, in which case data values are unbounded).

Value

a data.frame, containing the simulated data:

id

Participant id (numeric)

observation

observation counter (within participant; numeric)

day

day counter (numeric)

beep

beep counter, within day (numeric)

t

time of day (string, in "%H:%M" format)

Y

Simulated EMA value

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
sample_plan <- sample_plan(n_participants = 20, n_days = 7)

mm_par_a <- list(
  fixed  = c(intercept = 0,
             time      = 0),
  random = c(intercept = 0.1,
             time      = 0.005),
  error  = 0.2,
  phi    = 0.5
)
a <- sim_ema(plan = sample_plan,
             mm_par = mm_par_a)

mm_par_b <- mm_par_a
mm_par_b$fixed['time']   <- -0.5
mm_par_b$random['time']  <- 0.005

b <- sim_ema(plan = sample_plan,
             mm_par = mm_par_b)

b$id <- b$id + max(a$id)

c <- rbind(a, b)
c$group <- c(rep(0, nrow(a)), rep(1, nrow(b)))

# plot
library(ggplot2)
ggplot(data = c, aes(x = time, y = Y)) +
       geom_line(aes(group = id, colour = factor(group))) +
       geom_point(size = .8) +
       geom_smooth(aes(group = factor(group), colour = factor(group)),
                   method = "loess", se = FALSE, size = 2) +
       xlab("Time")

# test
library(nlme)
fm <- lme(Y ~ 1 + time * group ,
         random = ~ 1 + time | id,
         data = c,
         correlation = corAR1())
summary(fm)

jruwaard/emaph documentation built on May 3, 2019, 8:04 p.m.