Visualize daily data
## pivot_longer: reorganized (stressed, angry, sad, anxious, depressed, …) into (var, value) [was 931x44, now 32585x11]
## filter: removed 30,723 rows (94%), 1,862 rows remaining

## filter: removed 26,999 rows (83%), 5,586 rows remaining

## filter: removed 26,999 rows (83%), 5,586 rows remaining

## filter: removed 30,723 rows (94%), 1,862 rows remaining

Analyze DIF data
Visualize dif

DIF linear model
# fit and look at linear model
dif_lm = lm(difc ~ paq_g_dif, data=daily_clean)
summary(dif_lm)
##
## Call:
## lm(formula = difc ~ paq_g_dif, data = daily_clean)
##
## Residuals:
## Min 1Q Median 3Q Max
## -2.8556 -1.3316 -0.5145 1.4245 4.7293
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -1.464394 0.171064 -8.561 <2e-16 ***
## paq_g_dif 0.060958 0.006591 9.248 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 1.747 on 886 degrees of freedom
## (43 observations deleted due to missingness)
## Multiple R-squared: 0.08803, Adjusted R-squared: 0.087
## F-statistic: 85.53 on 1 and 886 DF, p-value: < 2.2e-16
DIF mixed model
Unconditional means model
model0_fit <- lmer(formula = difc ~ 1 + (1|pid),
data=daily_clean)
summary(model0_fit)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: difc ~ 1 + (1 | pid)
## Data: daily_clean
##
## REML criterion at convergence: 3420.6
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.4618 -0.6001 -0.1865 0.5111 3.8125
##
## Random effects:
## Groups Name Variance Std.Dev.
## pid (Intercept) 1.280 1.131
## Residual 2.062 1.436
## Number of obs: 924, groups: pid, 52
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) -0.03045 0.16390 50.95016 -0.186 0.853
VarCorr(model0_fit)
## Groups Name Std.Dev.
## pid (Intercept) 1.1313
## Residual 1.4359
# Store random effect variances
RandomEffects0 <- as.data.frame(VarCorr(model0_fit))
RandomEffects0
## grp var1 var2 vcov sdcor
## 1 pid (Intercept) <NA> 1.279803 1.131284
## 2 Residual <NA> <NA> 2.061684 1.435856
# compute ICC between
ICC_between0 <- RandomEffects0[1,4]/(RandomEffects0[1,4]+RandomEffects0[2,4])
ICC_between0
## [1] 0.3830041
# predicting state_dif without accounting for trait dif
# ICC ≈ 0.38 --> ~38% of variance attributable to between-person
# ~62% attributable to within-person variation
Random intercept and slope model
model1_fit = lmer(difc ~ paq_g_dif + (1 + paq_g_dif | pid),
data=daily_clean)
## boundary (singular) fit: see help('isSingular')
summary(model1_fit)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: difc ~ paq_g_dif + (1 + paq_g_dif | pid)
## Data: daily_clean
##
## REML criterion at convergence: 3297.6
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.4433 -0.6144 -0.2004 0.5475 3.7320
##
## Random effects:
## Groups Name Variance Std.Dev. Corr
## pid (Intercept) 7.107e-01 0.843057
## paq_g_dif 3.935e-05 0.006273 1.00
## Residual 2.102e+00 1.449921
## Number of obs: 888, groups: pid, 50
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) -1.48395 0.41838 36.45601 -3.547 0.001092 **
## paq_g_dif 0.06121 0.01683 45.58842 3.638 0.000697 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## paq_g_dif -0.935
## optimizer (nloptwrap) convergence code: 0 (OK)
## boundary (singular) fit: see help('isSingular')
#predictInterval(model1_fit)
REsim(model1_fit)
## groupFctr groupID term mean median sd
## 1 pid 77131 (Intercept) -0.9476186269 -0.9603404828 0.3482472
## 2 pid 89353 (Intercept) 0.8453133606 0.8508519874 0.3527943
## 3 pid 94714 (Intercept) -0.8675573133 -0.8751674707 0.3629993
## 4 pid 98533 (Intercept) -1.1349938943 -1.1538372565 0.2916940
## 5 pid 99391 (Intercept) 0.1484211047 0.1476870425 0.2996009
## 6 pid 100222 (Intercept) 1.0132790183 1.0071272216 0.2628711
## 7 pid 101662 (Intercept) -0.3219895446 -0.3127030670 0.3206842
## 8 pid 101788 (Intercept) 1.0485427134 1.0531430864 0.2901425
## 9 pid 103156 (Intercept) -0.7871965080 -0.7722317503 0.3283093
## 10 pid 103711 (Intercept) 0.1350613641 0.1451649321 0.3312420
## 11 pid 104128 (Intercept) 0.1754120369 0.1656758070 0.3017367
## 12 pid 104770 (Intercept) 0.4216329935 0.4153836325 0.3160458
## 13 pid 104836 (Intercept) 0.2731181554 0.2714816305 0.3227672
## 14 pid 105151 (Intercept) -0.7372730398 -0.7302029777 0.3436983
## 15 pid 105163 (Intercept) 0.0306581696 0.0115067083 0.2929821
## 16 pid 105172 (Intercept) -1.2072008957 -1.2188069290 0.3096794
## 17 pid 105196 (Intercept) -0.0091181971 -0.0373603411 0.3868272
## 18 pid 105232 (Intercept) 0.1656302542 0.1594589534 0.3054237
## 19 pid 105253 (Intercept) 0.0028985172 0.0321705510 0.3421199
## 20 pid 105268 (Intercept) -0.9147091371 -0.9214869555 0.3656671
## 21 pid 105334 (Intercept) -0.8938371458 -0.8977177224 0.3056553
## 22 pid 105376 (Intercept) -1.1270842699 -1.1225563204 0.2669884
## 23 pid 105427 (Intercept) 0.3775184182 0.3853883155 0.2966022
## 24 pid 105454 (Intercept) -0.6761682395 -0.6549956113 0.3188193
## 25 pid 105457 (Intercept) 0.9176924995 0.9303724981 0.3142846
## 26 pid 105463 (Intercept) 1.2612627370 1.2460305847 0.2741698
## 27 pid 105466 (Intercept) 1.1706633038 1.1403241474 0.3979956
## 28 pid 105478 (Intercept) -0.9305615518 -0.9294763434 0.3096830
## 29 pid 105499 (Intercept) -1.1413775696 -1.1468623262 0.3271617
## 30 pid 105538 (Intercept) 0.3419547462 0.3371566974 0.3294450
## 31 pid 105562 (Intercept) -0.5871444765 -0.6037519503 0.3300382
## 32 pid 105616 (Intercept) 0.1728597529 0.1691911752 0.2963830
## 33 pid 105670 (Intercept) -0.6383130700 -0.6243115266 0.2811478
## 34 pid 105679 (Intercept) -0.2679845861 -0.2766491524 0.3192527
## 35 pid 105748 (Intercept) 0.5527394620 0.5714851254 0.3077551
## 36 pid 105796 (Intercept) -1.1178920024 -1.1356760762 0.3021950
## 37 pid 105904 (Intercept) 0.4403042587 0.4214277927 0.3282645
## 38 pid 105937 (Intercept) 0.9417985407 0.9485108220 0.3062181
## 39 pid 105940 (Intercept) 0.4198436122 0.4128391344 0.3298614
## 40 pid 105958 (Intercept) -0.7816970549 -0.8190642779 0.3385914
## 41 pid 106033 (Intercept) -1.0957781392 -1.0697149956 0.3065233
## 42 pid 106036 (Intercept) 1.4340234032 1.4232094025 0.3029339
## 43 pid 106057 (Intercept) 0.5075762407 0.5054646105 0.2675360
## 44 pid 106063 (Intercept) 0.2434426174 0.2712729824 0.3084338
## 45 pid 106108 (Intercept) -0.1757950629 -0.1566537523 0.3007256
## 46 pid 106123 (Intercept) 0.5912402776 0.5803564548 0.3437228
## 47 pid 106282 (Intercept) 1.6029186729 1.5813887285 0.3249854
## 48 pid 106369 (Intercept) 0.9759266782 0.9777088304 0.2883310
## 49 pid 106438 (Intercept) -0.5894649096 -0.6094875194 0.3819405
## 50 pid 106519 (Intercept) 0.0418135623 0.0309306614 0.3124778
## 51 pid 77131 paq_g_dif -0.0069101573 -0.0069101573 0.0000000
## 52 pid 89353 paq_g_dif 0.0064084214 0.0064084214 0.0000000
## 53 pid 94714 paq_g_dif -0.0063331297 -0.0063331297 0.0000000
## 54 pid 98533 paq_g_dif -0.0083087659 -0.0083087659 0.0000000
## 55 pid 99391 paq_g_dif 0.0011644736 0.0011644736 0.0000000
## 56 pid 100222 paq_g_dif 0.0076426121 0.0076426121 0.0000000
## 57 pid 101662 paq_g_dif -0.0021752318 -0.0021752318 0.0000000
## 58 pid 101788 paq_g_dif 0.0074834601 0.0074834601 0.0000000
## 59 pid 103156 paq_g_dif -0.0057450183 -0.0057450183 0.0000000
## 60 pid 103711 paq_g_dif 0.0006004511 0.0006004511 0.0000000
## 61 pid 104128 paq_g_dif 0.0017944222 0.0017944222 0.0000000
## 62 pid 104770 paq_g_dif 0.0029417301 0.0029417301 0.0000000
## 63 pid 104836 paq_g_dif 0.0021424708 0.0021424708 0.0000000
## 64 pid 105151 paq_g_dif -0.0054969003 -0.0054969003 0.0000000
## 65 pid 105163 paq_g_dif 0.0005005193 0.0005005193 0.0000000
## 66 pid 105172 paq_g_dif -0.0092658060 -0.0092658060 0.0000000
## 67 pid 105196 paq_g_dif 0.0003067329 0.0003067329 0.0000000
## 68 pid 105232 paq_g_dif 0.0013638099 0.0013638099 0.0000000
## 69 pid 105253 paq_g_dif 0.0003319452 0.0003319452 0.0000000
## 70 pid 105268 paq_g_dif -0.0068705404 -0.0068705404 0.0000000
## 71 pid 105334 paq_g_dif -0.0067386441 -0.0067386441 0.0000000
## 72 pid 105376 paq_g_dif -0.0081770286 -0.0081770286 0.0000000
## 73 pid 105427 paq_g_dif 0.0029033708 0.0029033708 0.0000000
## 74 pid 105454 paq_g_dif -0.0047375266 -0.0047375266 0.0000000
## 75 pid 105457 paq_g_dif 0.0068523552 0.0068523552 0.0000000
## 76 pid 105463 paq_g_dif 0.0094173334 0.0094173334 0.0000000
## 77 pid 105466 paq_g_dif 0.0089509058 0.0089509058 0.0000000
## 78 pid 105478 paq_g_dif -0.0065396414 -0.0065396414 0.0000000
## 79 pid 105499 paq_g_dif -0.0087737178 -0.0087737178 0.0000000
## 80 pid 105538 paq_g_dif 0.0027316126 0.0027316126 0.0000000
## 81 pid 105562 paq_g_dif -0.0043917241 -0.0043917241 0.0000000
## 82 pid 105616 paq_g_dif 0.0012407231 0.0012407231 0.0000000
## 83 pid 105670 paq_g_dif -0.0047683466 -0.0047683466 0.0000000
## 84 pid 105679 paq_g_dif -0.0014962750 -0.0014962750 0.0000000
## 85 pid 105748 paq_g_dif 0.0041543336 0.0041543336 0.0000000
## 86 pid 105796 paq_g_dif -0.0080075173 -0.0080075173 0.0000000
## 87 pid 105904 paq_g_dif 0.0035908913 0.0035908913 0.0000000
## 88 pid 105937 paq_g_dif 0.0070400245 0.0070400245 0.0000000
## 89 pid 105940 paq_g_dif 0.0032372996 0.0032372996 0.0000000
## 90 pid 105958 paq_g_dif -0.0058471146 -0.0058471146 0.0000000
## 91 pid 106033 paq_g_dif -0.0076797860 -0.0076797860 0.0000000
## 92 pid 106036 paq_g_dif 0.0106717828 0.0106717828 0.0000000
## 93 pid 106057 paq_g_dif 0.0038074405 0.0038074405 0.0000000
## 94 pid 106063 paq_g_dif 0.0018210250 0.0018210250 0.0000000
## 95 pid 106108 paq_g_dif -0.0010689732 -0.0010689732 0.0000000
## 96 pid 106123 paq_g_dif 0.0042483960 0.0042483960 0.0000000
## 97 pid 106282 paq_g_dif 0.0122114151 0.0122114151 0.0000000
## 98 pid 106369 paq_g_dif 0.0073832073 0.0073832073 0.0000000
## 99 pid 106438 paq_g_dif -0.0038619425 -0.0038619425 0.0000000
## 100 pid 106519 paq_g_dif 0.0002506223 0.0002506223 0.0000000
plotREsim(REsim(model1_fit))

Random intercept model
model2_fit = lmer(difc ~ paq_g_dif + (1 | pid),
data=daily_clean)
summary(model2_fit)
## Linear mixed model fit by REML. t-tests use Satterthwaite's method [
## lmerModLmerTest]
## Formula: difc ~ paq_g_dif + (1 | pid)
## Data: daily_clean
##
## REML criterion at convergence: 3297.8
##
## Scaled residuals:
## Min 1Q Median 3Q Max
## -2.4331 -0.6123 -0.1970 0.5547 3.7243
##
## Random effects:
## Groups Name Variance Std.Dev.
## pid (Intercept) 0.9903 0.9951
## Residual 2.1023 1.4499
## Number of obs: 888, groups: pid, 50
##
## Fixed effects:
## Estimate Std. Error df t value Pr(>|t|)
## (Intercept) -1.46690 0.42897 48.26271 -3.42 0.001284 **
## paq_g_dif 0.06050 0.01667 48.04836 3.63 0.000687 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Correlation of Fixed Effects:
## (Intr)
## paq_g_dif -0.938
## groupFctr groupID term mean median sd
## 1 pid 77131 (Intercept) -0.97943992 -0.97436008 0.3803142
## 2 pid 89353 (Intercept) 0.95997860 0.98951735 0.3959864
## 3 pid 94714 (Intercept) -0.93998008 -0.92398024 0.3999010
## 4 pid 98533 (Intercept) -1.33104378 -1.34725669 0.2935873
## 5 pid 99391 (Intercept) 0.20649282 0.17489845 0.3463002
## 6 pid 100222 (Intercept) 1.22733464 1.22378118 0.3359804
## 7 pid 101662 (Intercept) -0.28859112 -0.29822616 0.3420691
## 8 pid 101788 (Intercept) 1.25291128 1.24792030 0.3778740
## 9 pid 103156 (Intercept) -0.87317652 -0.87044434 0.3866888
## 10 pid 103711 (Intercept) 0.08595970 0.11180313 0.4176200
## 11 pid 104128 (Intercept) 0.24275340 0.24139058 0.3572522
## 12 pid 104770 (Intercept) 0.49595420 0.52668729 0.3799179
## 13 pid 104836 (Intercept) 0.38650095 0.35946852 0.3566772
## 14 pid 105151 (Intercept) -0.97647349 -0.98382538 0.4036077
## 15 pid 105163 (Intercept) 0.11660212 0.11066371 0.3332836
## 16 pid 105172 (Intercept) -1.51442021 -1.49613731 0.3505864
## 17 pid 105196 (Intercept) 0.07039156 0.07299063 0.3997944
## 18 pid 105232 (Intercept) 0.23831704 0.22894961 0.3731939
## 19 pid 105253 (Intercept) 0.05768113 0.04386741 0.3870567
## 20 pid 105268 (Intercept) -1.04796466 -1.06171200 0.4474064
## 21 pid 105334 (Intercept) -1.07079260 -1.06502300 0.3582869
## 22 pid 105376 (Intercept) -1.26311239 -1.27727834 0.3249504
## 23 pid 105427 (Intercept) 0.49704964 0.47384304 0.3741975
## 24 pid 105454 (Intercept) -0.67438868 -0.70326735 0.3846623
## 25 pid 105457 (Intercept) 1.08528355 1.10185179 0.3640322
## 26 pid 105463 (Intercept) 1.52068818 1.51211268 0.3350433
## 27 pid 105466 (Intercept) 1.31792741 1.29866757 0.4400124
## 28 pid 105478 (Intercept) -0.93785215 -0.93608303 0.3288600
## 29 pid 105499 (Intercept) -1.49448585 -1.45883206 0.3917556
## 30 pid 105538 (Intercept) 0.43435930 0.42092937 0.3967392
## 31 pid 105562 (Intercept) -0.73797908 -0.71581838 0.4177120
## 32 pid 105616 (Intercept) 0.24001560 0.24259743 0.3562145
## 33 pid 105670 (Intercept) -0.77905146 -0.77018723 0.3943447
## 34 pid 105679 (Intercept) -0.21289406 -0.20950859 0.3494223
## 35 pid 105748 (Intercept) 0.70840137 0.74246699 0.3621841
## 36 pid 105796 (Intercept) -1.23283459 -1.21603993 0.3565957
## 37 pid 105904 (Intercept) 0.55442954 0.55891967 0.3550909
## 38 pid 105937 (Intercept) 1.23261298 1.23459361 0.3767109
## 39 pid 105940 (Intercept) 0.51491202 0.53216396 0.3976102
## 40 pid 105958 (Intercept) -0.98227316 -0.97677687 0.4098495
## 41 pid 106033 (Intercept) -1.21980900 -1.19178932 0.3201359
## 42 pid 106036 (Intercept) 1.70419340 1.72015862 0.3767140
## 43 pid 106057 (Intercept) 0.62882609 0.62792047 0.3307094
## 44 pid 106063 (Intercept) 0.32217452 0.30617923 0.3421897
## 45 pid 106108 (Intercept) -0.17294648 -0.17645118 0.3340275
## 46 pid 106123 (Intercept) 0.73876342 0.70821159 0.4253281
## 47 pid 106282 (Intercept) 1.92924795 1.92207887 0.4068192
## 48 pid 106369 (Intercept) 1.15747835 1.17458749 0.3303119
## 49 pid 106438 (Intercept) -0.53613535 -0.55951650 0.4199189
## 50 pid 106519 (Intercept) 0.09853801 0.08974452 0.4064069

Run function on ddf
