maddhatt1
maddhatt1

Reputation: 3

Autocorrelation within day within person in R

I am new to working with multilevel data. I have multilevel data, nested within day within person. I'm working in R, and I need the average autocorrelation of mood within a day for each person. How can I calculate this?

#Simulated Data
ID <- rep(101:105, each = 15)
day <- rep(1:5, each = 3, times = 5)
observation <- rep(1:3, times = 25 )
mood <- sample(1:50, 75, replace = T)
nada <- which(mood %in% sample(mood, 5))
mood[nada] <- NA
df <- data.frame(ID, day, observation, mood)
print(df)
    ID day observation mood
1  101   1           1   40
2  101   1           2   20
3  101   1           3   NA
4  101   2           1   10
5  101   2           2   NA
6  101   2           3   15
7  101   3           1   41
8  101   3           2    5
9  101   3           3   40
10 101   4           1   34
11 101   4           2    2
12 101   4           3   47
13 101   5           1    2
14 101   5           2   NA
15 101   5           3   39
16 102   1           1   12
17 102   1           2   26
18 102   1           3   NA
19 102   2           1   21
20 102   2           2   31
21 102   2           3   30
22 102   3           1    6
23 102   3           2    8
24 102   3           3   NA
25 102   4           1   36
26 102   4           2    9
27 102   4           3   31
28 102   5           1    6
29 102   5           2   44
30 102   5           3   17
31 103   1           1    5
32 103   1           2   NA
33 103   1           3   17
34 103   2           1   NA
35 103   2           2   43
36 103   2           3   NA
37 103   3           1   17
38 103   3           2   40
39 103   3           3   25
40 103   4           1   47
41 103   4           2   NA
42 103   4           3   34
43 103   5           1   27
44 103   5           2   20
45 103   5           3    5
46 104   1           1   28
47 104   1           2   47
48 104   1           3   20
49 104   2           1   NA
50 104   2           2   47
51 104   2           3   36
52 104   3           1   41
53 104   3           2    4
54 104   3           3    1
55 104   4           1   NA
56 104   4           2   NA
57 104   4           3   NA
58 104   5           1   NA
59 104   5           2    2
60 104   5           3   48
61 105   1           1   45
62 105   1           2   39
63 105   1           3   31
64 105   2           1    9
65 105   2           2   NA
66 105   2           3   29
67 105   3           1   40
68 105   3           2   10
69 105   3           3    9
70 105   4           1   25
71 105   4           2   NA
72 105   4           3   47
73 105   5           1   NA
74 105   5           2   NA
75 105   5           3   26


I have tried a few things and been unsuccessful.

For example, I tried auto_by with dplyr:

library(dplyr)
df <- df %>%
  group_by(ID, day) %>%
  mutate(mood.autocorr = quest::auto_by(x=mood, grp=ID, n=1L, how = "cor")

But I got this error:

Error in mutate(): ℹ In argument: mood.autocorr = quest::auto_by(...). ℹ In group 1: ID = 101, day = 1. Caused by error in FUN(): ! abs(n) is greater than length(x)

Without mutate I get this error: Error in if (cw) { : the condition has length > 1

There's probably a simple solution, so any suggestions would be helpful. Thanks!

Upvotes: 0

Views: 49

Answers (0)

Related Questions