-
Notifications
You must be signed in to change notification settings - Fork 0
/
vignette-tvem.html
717 lines (683 loc) · 103 KB
/
vignette-tvem.html
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="John J. Dziak" />
<meta name="date" content="2020-07-16" />
<title>Fitting time-varying effects models</title>
<script>// Hide empty <a> tag within highlighted CodeBlock for screen reader accessibility (see https://github.com/jgm/pandoc/issues/6352#issuecomment-626106786) -->
// v0.0.1
// Written by JooYoung Seo ([email protected]) and Atsushi Yasumoto on June 1st, 2020.
document.addEventListener('DOMContentLoaded', function() {
const codeList = document.getElementsByClassName("sourceCode");
for (var i = 0; i < codeList.length; i++) {
var linkList = codeList[i].getElementsByTagName('a');
for (var j = 0; j < linkList.length; j++) {
if (linkList[j].innerHTML === "") {
linkList[j].setAttribute('aria-hidden', 'true');
}
}
}
});
</script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap;
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }
code > span.kw { color: #555; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #d14; }
code > span.fl { color: #d14; }
code > span.ch { color: #d14; }
code > span.st { color: #d14; }
code > span.co { color: #888888; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #900; font-weight: bold; }
code > span.er { color: #a61717; background-color: #e3d2d2; }
</style>
</head>
<body>
<h1 class="title toc-ignore">Fitting time-varying effects models</h1>
<h4 class="author">John J. Dziak</h4>
<h4 class="date">2020-07-16</h4>
<p>In this example we simulate a longitudinal dataset and fit a simple time-varying coefficient model to it using the tvem package. We show that this can be done for either a continuous or a binary outcome variable. Time-varying coefficient models are discussed further by Tan, Shiyko, Li, Li and Dierker (2012) and are an application of the varying-coefficient models approach of Hastie and Tibshirani (1993) to intensive longitudinal data.</p>
<p>Before running the examples, first install and load the tvem package. A .zip or .tar.gz file containing the package is available at <a href="https://github.com/dziakj1/TvemPackage">https://github.com/dziakj1/TvemPackage</a>, and it can then be used with the install.packages() function in <a href="https://www.r-project.org">R code</a>, Packages > Install Package(s) from Local Files in the <a href="https://www.r-project.org">R GUI</a>, or Tools > Install Packages in <a href="https://www.rstudio.com">RStudio</a>, to install the package. We also intend to submit this package to CRAN, from where it would then be able to be installed directly. Of course, if you are viewing this guide from within R using the vignette() function, then the package is already installed.</p>
<p>After you install the package on your system, you can then load it as usual with the library() function.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">library</span>(tvem)</span>
<span id="cb1-2"><a href="#cb1-2"></a><span class="co">#> Loading required package: mgcv</span></span>
<span id="cb1-3"><a href="#cb1-3"></a><span class="co">#> Loading required package: nlme</span></span>
<span id="cb1-4"><a href="#cb1-4"></a><span class="co">#> This is mgcv 1.8-31. For overview type 'help("mgcv-package")'.</span></span></code></pre></div>
<div id="example-with-a-continuous-outcome-variable" class="section level1">
<h1>Example with a continuous outcome variable</h1>
<p>The tvem package has a function for simulating a dataset. It is good to start by specifying a random seed.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1"></a><span class="kw">set.seed</span>(<span class="dv">123</span>);</span>
<span id="cb2-2"><a href="#cb2-2"></a>the_data <-<span class="st"> </span><span class="kw">simulate_tvem_example</span>();</span></code></pre></div>
<div id="exploring-the-dataset" class="section level2">
<h2>Exploring the dataset</h2>
<p>When analyzing any dataset, it is important to examine it descriptively first.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1"></a><span class="kw">print</span>(<span class="kw">head</span>(the_data));</span>
<span id="cb3-2"><a href="#cb3-2"></a><span class="co">#> subject_id time x1 x2 y</span></span>
<span id="cb3-3"><a href="#cb3-3"></a><span class="co">#> 1 1 0.00 5.3 5.8 0.9</span></span>
<span id="cb3-4"><a href="#cb3-4"></a><span class="co">#> 2 1 0.05 6.4 5.3 0.0</span></span>
<span id="cb3-5"><a href="#cb3-5"></a><span class="co">#> 3 1 0.10 5.4 5.5 NA</span></span>
<span id="cb3-6"><a href="#cb3-6"></a><span class="co">#> 4 1 0.15 5.7 3.2 NA</span></span>
<span id="cb3-7"><a href="#cb3-7"></a><span class="co">#> 5 1 0.20 5.8 2.8 NA</span></span>
<span id="cb3-8"><a href="#cb3-8"></a><span class="co">#> 6 1 0.25 8.7 3.2 1.9</span></span>
<span id="cb3-9"><a href="#cb3-9"></a><span class="kw">print</span>(<span class="kw">summary</span>(the_data));</span>
<span id="cb3-10"><a href="#cb3-10"></a><span class="co">#> subject_id time x1 x2 </span></span>
<span id="cb3-11"><a href="#cb3-11"></a><span class="co">#> Min. : 1.00 Min. :0.00 Min. : 0.00 Min. : 0.000 </span></span>
<span id="cb3-12"><a href="#cb3-12"></a><span class="co">#> 1st Qu.: 75.75 1st Qu.:1.75 1st Qu.: 3.60 1st Qu.: 1.900 </span></span>
<span id="cb3-13"><a href="#cb3-13"></a><span class="co">#> Median :150.50 Median :3.50 Median : 5.00 Median : 3.300 </span></span>
<span id="cb3-14"><a href="#cb3-14"></a><span class="co">#> Mean :150.50 Mean :3.50 Mean : 5.02 Mean : 3.326 </span></span>
<span id="cb3-15"><a href="#cb3-15"></a><span class="co">#> 3rd Qu.:225.25 3rd Qu.:5.25 3rd Qu.: 6.40 3rd Qu.: 4.700 </span></span>
<span id="cb3-16"><a href="#cb3-16"></a><span class="co">#> Max. :300.00 Max. :7.00 Max. :10.00 Max. :10.000 </span></span>
<span id="cb3-17"><a href="#cb3-17"></a><span class="co">#> </span></span>
<span id="cb3-18"><a href="#cb3-18"></a><span class="co">#> y </span></span>
<span id="cb3-19"><a href="#cb3-19"></a><span class="co">#> Min. :0.000 </span></span>
<span id="cb3-20"><a href="#cb3-20"></a><span class="co">#> 1st Qu.:1.000 </span></span>
<span id="cb3-21"><a href="#cb3-21"></a><span class="co">#> Median :2.200 </span></span>
<span id="cb3-22"><a href="#cb3-22"></a><span class="co">#> Mean :2.308 </span></span>
<span id="cb3-23"><a href="#cb3-23"></a><span class="co">#> 3rd Qu.:3.400 </span></span>
<span id="cb3-24"><a href="#cb3-24"></a><span class="co">#> Max. :9.600 </span></span>
<span id="cb3-25"><a href="#cb3-25"></a><span class="co">#> NA's :29647</span></span></code></pre></div>
<p>The dataset is in long form (one row per observation, with multiple observation times for each participant). There are 300 participants. The observation time ranges from 0 (thought of as the beginning of an intensive study on people undergoing an intervention or lifestyle change) to 7 (imagined as the end of the study seven days later). There is a single response variable <span class="math inline">\(y\)</span>, and two predictor variables (covariates), <span class="math inline">\(x_1\)</span> and <span class="math inline">\(x_2\)</span>. In the context of an intensive longitudinal study with human participants, these variables might be ratings of different feelings, symptoms or behaviors, reported a few times per day at random times, during seven days following an event (such as the beginning of an intervention, treatment, lifestyle change, etc.). The values of the covariates and the response vary over time within subject. The analyst wishes to find out whether their means change systematically over time, whether they are interrelated, and whether this relationship, if it exists, changes over time.</p>
</div>
<div id="plotting-average-change-over-time" class="section level2">
<h2>Plotting average change over time</h2>
<p>One easy thing to do is to investigate whether and how the response changes over time on average. This is simply curve fitting, similar to polynomial regression, but can be fit using the TVEM function, in an approach sometimes called `intercept-only TVEM.’ This approach uses a spline function to approximate the average change in <span class="math inline">\(y\)</span> over time.</p>
<p>By default, the tvem function will fit a penalized B-spline (de Boor, 1972), which is called a ``P-spline’’ in the terminology of Eilers and Marx (1996). This approach uses an automatic tuning penalty to choose the level of smoothness versus flexibility of the fitted function. It is similar, though not identical, to the P-splines used in the Methodology Center’s %TVEM macro for the SAS programming language. Those P-splines were penalized truncated power splines (Li et al., 2017; see Ruppert, Wand & Carroll, 2003).</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1"></a>model1 <-<span class="st"> </span><span class="kw">tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb4-2"><a href="#cb4-2"></a> <span class="dt">formula=</span>y<span class="op">~</span><span class="dv">1</span>,</span>
<span id="cb4-3"><a href="#cb4-3"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb4-4"><a href="#cb4-4"></a> <span class="dt">time=</span>time);</span></code></pre></div>
<p>You also have the option to turn off the penalty and control the smoothness yourself, by specifying the number of interior knots, here 2.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1"></a>model1 <-<span class="st"> </span><span class="kw">tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb5-2"><a href="#cb5-2"></a> <span class="dt">formula=</span>y<span class="op">~</span><span class="dv">1</span>,</span>
<span id="cb5-3"><a href="#cb5-3"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb5-4"><a href="#cb5-4"></a> <span class="dt">num_knots=</span><span class="dv">2</span>,</span>
<span id="cb5-5"><a href="#cb5-5"></a> <span class="dt">penalize=</span><span class="ot">FALSE</span>,</span>
<span id="cb5-6"><a href="#cb5-6"></a> <span class="dt">time=</span>time);</span></code></pre></div>
<p>The implied mean model is <span class="math inline">\(E(y|t) =\beta_0(t)\)</span> Where <span class="math inline">\(t\)</span> is time in days. After fitting the model, you can print a summary and plot the estimated coefficient.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1"></a><span class="kw">print</span>(model1);</span>
<span id="cb6-2"><a href="#cb6-2"></a><span class="co">#> ======================================================= </span></span>
<span id="cb6-3"><a href="#cb6-3"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb6-4"><a href="#cb6-4"></a><span class="co">#> ======================================================= </span></span>
<span id="cb6-5"><a href="#cb6-5"></a><span class="co">#> Response variable: y </span></span>
<span id="cb6-6"><a href="#cb6-6"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb6-7"><a href="#cb6-7"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb6-8"><a href="#cb6-8"></a><span class="co">#> Effects specified as time-varying: (Intercept)</span></span>
<span id="cb6-9"><a href="#cb6-9"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb6-10"><a href="#cb6-10"></a><span class="co">#> ======================================================= </span></span>
<span id="cb6-11"><a href="#cb6-11"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb6-12"><a href="#cb6-12"></a><span class="co">#> Method REML</span></span>
<span id="cb6-13"><a href="#cb6-13"></a><span class="co">#> Formula:</span></span>
<span id="cb6-14"><a href="#cb6-14"></a><span class="co">#> y ~ s(time, bs = "ps", by = NA, pc = 0, k = 6, fx = TRUE)</span></span>
<span id="cb6-15"><a href="#cb6-15"></a><span class="co">#> Pseudolikelihood AIC: 46234.19</span></span>
<span id="cb6-16"><a href="#cb6-16"></a><span class="co">#> Pseudolikelihood BIC: 46260.11 </span></span>
<span id="cb6-17"><a href="#cb6-17"></a><span class="co">#> Note: Used listwise deletion for missing data.</span></span>
<span id="cb6-18"><a href="#cb6-18"></a><span class="co">#> =======================================================</span></span>
<span id="cb6-19"><a href="#cb6-19"></a><span class="kw">plot</span>(model1);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYA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AAwFgKEAMP8H3HFQxQXplv4AAAAASUVORK5CYII=" /><!-- --></p>
<p>The plot shows the estimated coefficient function, and approximate estimates for 95% pointwise confidence intervals (not corrected for potential multiple comparisons) for the value of the function at each time.</p>
<p>We don’t provide for random effects in the current version of this package. Instead, we use a (possibly penalized) form of generalized estimating equations with working independence, and adjust the standard errors for within-subject correlation using a sandwich formula.</p>
</div>
<div id="using-the-select_tvem-function" class="section level2">
<h2>Using the select_tvem function</h2>
<p>It is a very good idea to examine how the covariate means change over time. That is, we should fit intercept-only TVEM’s for <span class="math inline">\(x_1\)</span> and <span class="math inline">\(x_2\)</span>, not just <span class="math inline">\(y\)</span>.</p>
<p>While doing this, we can take the opportunity to additionally explore yet another way to fit a model using the tvem function, by using the select_tvem function to choose the number of interior knots by an pseudolikelihood equivalent to an AIC or BIC criterion. “Pseudolikelihood” here means that the information criterion doesn’t take within-subject correlation into account, because we are trying to fit a marginal model agnostic to the exact correlation structure. The code below will fit the model with 0 to 5 interior knots, record the fit criterion for each potential choice, and select the one which gives the lowest (best) value of the criterion.</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1"></a>model2 <-<span class="st"> </span><span class="kw">select_tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb7-2"><a href="#cb7-2"></a> <span class="dt">formula =</span> x1<span class="op">~</span><span class="dv">1</span>,</span>
<span id="cb7-3"><a href="#cb7-3"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb7-4"><a href="#cb7-4"></a> <span class="dt">time=</span>time,</span>
<span id="cb7-5"><a href="#cb7-5"></a> <span class="dt">max_knots=</span><span class="dv">5</span>);</span>
<span id="cb7-6"><a href="#cb7-6"></a><span class="co">#> knots ic</span></span>
<span id="cb7-7"><a href="#cb7-7"></a><span class="co">#> [1,] 0 177522.9</span></span>
<span id="cb7-8"><a href="#cb7-8"></a><span class="co">#> [2,] 1 177475.0</span></span>
<span id="cb7-9"><a href="#cb7-9"></a><span class="co">#> [3,] 2 177453.0</span></span>
<span id="cb7-10"><a href="#cb7-10"></a><span class="co">#> [4,] 3 177452.0</span></span>
<span id="cb7-11"><a href="#cb7-11"></a><span class="co">#> [5,] 4 177448.3</span></span>
<span id="cb7-12"><a href="#cb7-12"></a><span class="co">#> [6,] 5 177446.4</span></span>
<span id="cb7-13"><a href="#cb7-13"></a><span class="co">#> [1] "Selected 5 interior knots."</span></span>
<span id="cb7-14"><a href="#cb7-14"></a><span class="kw">print</span>(model2);</span>
<span id="cb7-15"><a href="#cb7-15"></a><span class="co">#> ======================================================= </span></span>
<span id="cb7-16"><a href="#cb7-16"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb7-17"><a href="#cb7-17"></a><span class="co">#> ======================================================= </span></span>
<span id="cb7-18"><a href="#cb7-18"></a><span class="co">#> Response variable: x1 </span></span>
<span id="cb7-19"><a href="#cb7-19"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb7-20"><a href="#cb7-20"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb7-21"><a href="#cb7-21"></a><span class="co">#> Effects specified as time-varying: (Intercept)</span></span>
<span id="cb7-22"><a href="#cb7-22"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb7-23"><a href="#cb7-23"></a><span class="co">#> ======================================================= </span></span>
<span id="cb7-24"><a href="#cb7-24"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb7-25"><a href="#cb7-25"></a><span class="co">#> Method fREML</span></span>
<span id="cb7-26"><a href="#cb7-26"></a><span class="co">#> Formula:</span></span>
<span id="cb7-27"><a href="#cb7-27"></a><span class="co">#> x1 ~ s(time, bs = "ps", by = NA, pc = 0, k = 9, fx = FALSE)</span></span>
<span id="cb7-28"><a href="#cb7-28"></a><span class="co">#> Pseudolikelihood AIC: 177446.42</span></span>
<span id="cb7-29"><a href="#cb7-29"></a><span class="co">#> Pseudolikelihood BIC: 177476.67 </span></span>
<span id="cb7-30"><a href="#cb7-30"></a><span class="co">#> Model selection table for number of interior knots:</span></span>
<span id="cb7-31"><a href="#cb7-31"></a><span class="co">#> knots ic</span></span>
<span id="cb7-32"><a href="#cb7-32"></a><span class="co">#> [1,] 0 177522.9</span></span>
<span id="cb7-33"><a href="#cb7-33"></a><span class="co">#> [2,] 1 177475.0</span></span>
<span id="cb7-34"><a href="#cb7-34"></a><span class="co">#> [3,] 2 177453.0</span></span>
<span id="cb7-35"><a href="#cb7-35"></a><span class="co">#> [4,] 3 177452.0</span></span>
<span id="cb7-36"><a href="#cb7-36"></a><span class="co">#> [5,] 4 177448.3</span></span>
<span id="cb7-37"><a href="#cb7-37"></a><span class="co">#> [6,] 5 177446.4</span></span>
<span id="cb7-38"><a href="#cb7-38"></a><span class="co">#> =======================================================</span></span>
<span id="cb7-39"><a href="#cb7-39"></a><span class="kw">plot</span>(model2);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAA21BMVEUAAAAAADoAAGYAOjoAOmYAOpAAZpAAZrY6AAA6ADo6AGY6OgA6Ojo6OmY6OpA6ZmY6ZpA6ZrY6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZmZmZpBmkJBmkLZmkNtmtrZmtttmtv+QOgCQZgCQZjqQZmaQkDqQtpCQttuQ29uQ2/+2ZgC2Zjq2ZpC2kDq2kGa2kJC2ttu227a229u22/+2/7a2/9u2///bkDrbkGbbtmbbtpDbtrbb27bb29vb2//b////tmb/25D/27b/29v//7b//9v///+6Ao7pAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAOUklEQVR4nO2dCXvbtgGG6SSelcbZ4lpbsqVLZ2vZ2lrZ0cWduWRNTXkS//8vGgiAh3gBJAF/PL73eeJQJgBSeImTIB3EBEqAPoGlQwFgKAAMBYChADAUAIYCwFAAGAoAQwFgKAAMBYChADAUAIYCwFAAGAoAQwFgKAAMBYChADAjELANMn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bvLc/FRoBquBpQ7VlrkNjuMBVQjXBsISC0yn9D1tl2z1vbWXWqhqa4Vw2E64aaBYTGXFNZby5J7ZemRSrqCWnDgUwFpB7cQMz8fcwXVJKzhaeX24K17U4uldZGWLYkpgP1agI8TUVYzRAYBOjKoz2drdXg31Q5W6QSWUyL9OiEcjIODgWAoQAwFACGAsBQABgKADNDAVGfASmM+QkovFhtClAAmNkJSJYv9JkWRjE7ASwBaCgADAWAoQAwFACGAsD0uzcOY34C4i3HAcQeCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAMz/AcegS5kERGEXAAAAAElFTkSuQmCC" /><!-- --></p>
<p>The highest number of interior knots in the range is selected. If the resulting plot looks overly complicated and uninterpretable, it might be reasonable to use a lower number of interior knots anyway. On the other hand, if it looks interpretable, it would be reasonable to run the function again with a higher value of max_knots, to see whether there might be an even better fit that had not been found yet. Note that the individual knots and their locations do not have a special interpretation (as they do in some ``changepoint’’ models); they are just a tool to make the fitted function more flexible than a simple polynomial would be. Let us try again with a maximum of ten interior knots.</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1"></a>model2 <-<span class="st"> </span><span class="kw">select_tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb8-2"><a href="#cb8-2"></a> <span class="dt">formula =</span> x1<span class="op">~</span><span class="dv">1</span>,</span>
<span id="cb8-3"><a href="#cb8-3"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb8-4"><a href="#cb8-4"></a> <span class="dt">time=</span>time,</span>
<span id="cb8-5"><a href="#cb8-5"></a> <span class="dt">max_knots=</span><span class="dv">10</span>);</span>
<span id="cb8-6"><a href="#cb8-6"></a><span class="co">#> knots ic</span></span>
<span id="cb8-7"><a href="#cb8-7"></a><span class="co">#> [1,] 0 177522.9</span></span>
<span id="cb8-8"><a href="#cb8-8"></a><span class="co">#> [2,] 1 177475.0</span></span>
<span id="cb8-9"><a href="#cb8-9"></a><span class="co">#> [3,] 2 177453.0</span></span>
<span id="cb8-10"><a href="#cb8-10"></a><span class="co">#> [4,] 3 177452.0</span></span>
<span id="cb8-11"><a href="#cb8-11"></a><span class="co">#> [5,] 4 177448.3</span></span>
<span id="cb8-12"><a href="#cb8-12"></a><span class="co">#> [6,] 5 177446.4</span></span>
<span id="cb8-13"><a href="#cb8-13"></a><span class="co">#> [7,] 6 177445.6</span></span>
<span id="cb8-14"><a href="#cb8-14"></a><span class="co">#> [8,] 7 177445.5</span></span>
<span id="cb8-15"><a href="#cb8-15"></a><span class="co">#> [9,] 8 177447.3</span></span>
<span id="cb8-16"><a href="#cb8-16"></a><span class="co">#> [10,] 9 177444.9</span></span>
<span id="cb8-17"><a href="#cb8-17"></a><span class="co">#> [11,] 10 177441.9</span></span>
<span id="cb8-18"><a href="#cb8-18"></a><span class="co">#> [1] "Selected 10 interior knots."</span></span>
<span id="cb8-19"><a href="#cb8-19"></a><span class="kw">print</span>(model2);</span>
<span id="cb8-20"><a href="#cb8-20"></a><span class="co">#> ======================================================= </span></span>
<span id="cb8-21"><a href="#cb8-21"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb8-22"><a href="#cb8-22"></a><span class="co">#> ======================================================= </span></span>
<span id="cb8-23"><a href="#cb8-23"></a><span class="co">#> Response variable: x1 </span></span>
<span id="cb8-24"><a href="#cb8-24"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb8-25"><a href="#cb8-25"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb8-26"><a href="#cb8-26"></a><span class="co">#> Effects specified as time-varying: (Intercept)</span></span>
<span id="cb8-27"><a href="#cb8-27"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb8-28"><a href="#cb8-28"></a><span class="co">#> ======================================================= </span></span>
<span id="cb8-29"><a href="#cb8-29"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb8-30"><a href="#cb8-30"></a><span class="co">#> Method fREML</span></span>
<span id="cb8-31"><a href="#cb8-31"></a><span class="co">#> Formula:</span></span>
<span id="cb8-32"><a href="#cb8-32"></a><span class="co">#> x1 ~ s(time, bs = "ps", by = NA, pc = 0, k = 14, fx = FALSE)</span></span>
<span id="cb8-33"><a href="#cb8-33"></a><span class="co">#> Pseudolikelihood AIC: 177441.89</span></span>
<span id="cb8-34"><a href="#cb8-34"></a><span class="co">#> Pseudolikelihood BIC: 177481.16 </span></span>
<span id="cb8-35"><a href="#cb8-35"></a><span class="co">#> Model selection table for number of interior knots:</span></span>
<span id="cb8-36"><a href="#cb8-36"></a><span class="co">#> knots ic</span></span>
<span id="cb8-37"><a href="#cb8-37"></a><span class="co">#> [1,] 0 177522.9</span></span>
<span id="cb8-38"><a href="#cb8-38"></a><span class="co">#> [2,] 1 177475.0</span></span>
<span id="cb8-39"><a href="#cb8-39"></a><span class="co">#> [3,] 2 177453.0</span></span>
<span id="cb8-40"><a href="#cb8-40"></a><span class="co">#> [4,] 3 177452.0</span></span>
<span id="cb8-41"><a href="#cb8-41"></a><span class="co">#> [5,] 4 177448.3</span></span>
<span id="cb8-42"><a href="#cb8-42"></a><span class="co">#> [6,] 5 177446.4</span></span>
<span id="cb8-43"><a href="#cb8-43"></a><span class="co">#> [7,] 6 177445.6</span></span>
<span id="cb8-44"><a href="#cb8-44"></a><span class="co">#> [8,] 7 177445.5</span></span>
<span id="cb8-45"><a href="#cb8-45"></a><span class="co">#> [9,] 8 177447.3</span></span>
<span id="cb8-46"><a href="#cb8-46"></a><span class="co">#> [10,] 9 177444.9</span></span>
<span id="cb8-47"><a href="#cb8-47"></a><span class="co">#> [11,] 10 177441.9</span></span>
<span id="cb8-48"><a href="#cb8-48"></a><span class="co">#> =======================================================</span></span>
<span id="cb8-49"><a href="#cb8-49"></a><span class="kw">plot</span>(model2);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAA21BMVEUAAAAAADoAAGYAOjoAOmYAOpAAZpAAZrY6AAA6ADo6AGY6OgA6Ojo6OmY6OpA6ZmY6ZpA6ZrY6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZmZmZpBmkJBmkLZmkNtmtrZmtttmtv+QOgCQZgCQZjqQZmaQkDqQtpCQttuQ29uQ2/+2ZgC2Zjq2ZpC2kDq2kGa2kJC2ttu227a229u22/+2/7a2/9u2///bkDrbkGbbtmbbtpDbtrbb27bb29vb2//b////tmb/25D/27b/29v//7b//9v///+6Ao7pAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAOsUlEQVR4nO2dC3vbthlG6SSelcZZ41pbsqVrG2vZ2prZpYu7cMmamvIk/v9fNAIgRYoXACQBvby853maSiYA0jjEheBHOkgIlAB9AEuHAsBQABgKAEMBYCgADAWAoQAwFACGAsBQABgKAEMBYCgADAWAoQAwFACGAsBQABgKAEMBYEYgIAwO/GYTnN3KH25Xwfl9dNhwfi+Tpf/LMlxYF79/twqCK/nvi01WQr6p8r2S8W8f+v1CnRiXgIu0yq/lD+WHmgBpZ7fuJCAW+a/lv191EfDw8tECBaSVK6skrZr0168JkHbSxtFFQCS1RXnT6nBYSxEgSCtcVWmoKiqt5AtRa6U6SAVcSjtSSyHg47MgePxGfvz0Kv34jTyn9+/S9nJ1n+k9eyb//TE74/MsWQs4pBWafky/PLnN8j36oO+lHDA2AbE6y9NKvqkL+HKV2tlvHv21JCBrP1mmrLWobiqtyQ9NAg5ZVO0WaQ9NLt3NUgWktZFWuuqBSl3Qjay2N5u01rar878XAlJhX90/yExy3FaDh+g/7sQPro+6IFWbRRb1/Sht8OSuKOKDaZx2wNgEiHq+yaqjJuAmTGsjDq6jQoCqpujF+0OnLf6XalT55UBSEVBkkd+P04o9qf5voWOAOD8v8p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YCgADAWAoQAwFACGAsBQABgKAEMBYCgADAWAoQAwFACGAsBQABgKAEMBYCgADAWAoQAwFACGAsBQABgKAPN/GDJC86+1w3YAAAAASUVORK5CYII=" /><!-- --></p>
<p>The fit seems to continue slightly improving with higher and higher numbers of knots, but the differences in fit are so small no further insight would be gained by trying more. The use_bic option can be set to TRUE in order to use a more parsimonious criterion that might be minimized at a smaller number of knots.</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1"></a>model2 <-<span class="st"> </span><span class="kw">select_tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb9-2"><a href="#cb9-2"></a> <span class="dt">formula =</span> x1<span class="op">~</span><span class="dv">1</span>,</span>
<span id="cb9-3"><a href="#cb9-3"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb9-4"><a href="#cb9-4"></a> <span class="dt">use_bic=</span><span class="ot">TRUE</span>,</span>
<span id="cb9-5"><a href="#cb9-5"></a> <span class="dt">time=</span>time,</span>
<span id="cb9-6"><a href="#cb9-6"></a> <span class="dt">max_knots=</span><span class="dv">10</span>);</span>
<span id="cb9-7"><a href="#cb9-7"></a><span class="co">#> knots ic</span></span>
<span id="cb9-8"><a href="#cb9-8"></a><span class="co">#> [1,] 0 177541.3</span></span>
<span id="cb9-9"><a href="#cb9-9"></a><span class="co">#> [2,] 1 177497.2</span></span>
<span id="cb9-10"><a href="#cb9-10"></a><span class="co">#> [3,] 2 177478.9</span></span>
<span id="cb9-11"><a href="#cb9-11"></a><span class="co">#> [4,] 3 177481.2</span></span>
<span id="cb9-12"><a href="#cb9-12"></a><span class="co">#> [5,] 4 177477.1</span></span>
<span id="cb9-13"><a href="#cb9-13"></a><span class="co">#> [6,] 5 177476.7</span></span>
<span id="cb9-14"><a href="#cb9-14"></a><span class="co">#> [7,] 6 177477.7</span></span>
<span id="cb9-15"><a href="#cb9-15"></a><span class="co">#> [8,] 7 177479.6</span></span>
<span id="cb9-16"><a href="#cb9-16"></a><span class="co">#> [9,] 8 177481.4</span></span>
<span id="cb9-17"><a href="#cb9-17"></a><span class="co">#> [10,] 9 177480.7</span></span>
<span id="cb9-18"><a href="#cb9-18"></a><span class="co">#> [11,] 10 177481.2</span></span>
<span id="cb9-19"><a href="#cb9-19"></a><span class="co">#> [1] "Selected 5 interior knots."</span></span>
<span id="cb9-20"><a href="#cb9-20"></a><span class="kw">print</span>(model2);</span>
<span id="cb9-21"><a href="#cb9-21"></a><span class="co">#> ======================================================= </span></span>
<span id="cb9-22"><a href="#cb9-22"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb9-23"><a href="#cb9-23"></a><span class="co">#> ======================================================= </span></span>
<span id="cb9-24"><a href="#cb9-24"></a><span class="co">#> Response variable: x1 </span></span>
<span id="cb9-25"><a href="#cb9-25"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb9-26"><a href="#cb9-26"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb9-27"><a href="#cb9-27"></a><span class="co">#> Effects specified as time-varying: (Intercept)</span></span>
<span id="cb9-28"><a href="#cb9-28"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb9-29"><a href="#cb9-29"></a><span class="co">#> ======================================================= </span></span>
<span id="cb9-30"><a href="#cb9-30"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb9-31"><a href="#cb9-31"></a><span class="co">#> Method fREML</span></span>
<span id="cb9-32"><a href="#cb9-32"></a><span class="co">#> Formula:</span></span>
<span id="cb9-33"><a href="#cb9-33"></a><span class="co">#> x1 ~ s(time, bs = "ps", by = NA, pc = 0, k = 9, fx = FALSE)</span></span>
<span id="cb9-34"><a href="#cb9-34"></a><span class="co">#> Pseudolikelihood AIC: 177446.42</span></span>
<span id="cb9-35"><a href="#cb9-35"></a><span class="co">#> Pseudolikelihood BIC: 177476.67 </span></span>
<span id="cb9-36"><a href="#cb9-36"></a><span class="co">#> Model selection table for number of interior knots:</span></span>
<span id="cb9-37"><a href="#cb9-37"></a><span class="co">#> knots ic</span></span>
<span id="cb9-38"><a href="#cb9-38"></a><span class="co">#> [1,] 0 177541.3</span></span>
<span id="cb9-39"><a href="#cb9-39"></a><span class="co">#> [2,] 1 177497.2</span></span>
<span id="cb9-40"><a href="#cb9-40"></a><span class="co">#> [3,] 2 177478.9</span></span>
<span id="cb9-41"><a href="#cb9-41"></a><span class="co">#> [4,] 3 177481.2</span></span>
<span id="cb9-42"><a href="#cb9-42"></a><span class="co">#> [5,] 4 177477.1</span></span>
<span id="cb9-43"><a href="#cb9-43"></a><span class="co">#> [6,] 5 177476.7</span></span>
<span id="cb9-44"><a href="#cb9-44"></a><span class="co">#> [7,] 6 177477.7</span></span>
<span id="cb9-45"><a href="#cb9-45"></a><span class="co">#> [8,] 7 177479.6</span></span>
<span id="cb9-46"><a href="#cb9-46"></a><span class="co">#> [9,] 8 177481.4</span></span>
<span id="cb9-47"><a href="#cb9-47"></a><span class="co">#> [10,] 9 177480.7</span></span>
<span id="cb9-48"><a href="#cb9-48"></a><span class="co">#> [11,] 10 177481.2</span></span>
<span id="cb9-49"><a href="#cb9-49"></a><span class="co">#> =======================================================</span></span>
<span id="cb9-50"><a href="#cb9-50"></a><span class="kw">plot</span>(model2);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAA21BMVEUAAAAAADoAAGYAOjoAOmYAOpAAZpAAZrY6AAA6ADo6AGY6OgA6Ojo6OmY6OpA6ZmY6ZpA6ZrY6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZmZmZpBmkJBmkLZmkNtmtrZmtttmtv+QOgCQZgCQZjqQZmaQkDqQtpCQttuQ29uQ2/+2ZgC2Zjq2ZpC2kDq2kGa2kJC2ttu227a229u22/+2/7a2/9u2///bkDrbkGbbtmbbtpDbtrbb27bb29vb2//b////tmb/25D/27b/29v//7b//9v///+6Ao7pAAA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4nTb9R6LhJziAo+BFhkXfKEgfl7x7HF9W0UEJ7+uV1AaLxsLcqIDLYy1Jg1eBEgT9eYv1YCtuarzqBInI2hDdiemxqb6MlPa2N7ZHOyVXwIUFet8dq1EdD4KE4ewBAiqT3aBezXye5tWzJhUl1a1HXdC8DIBUSGvknCYdOavcnTbIYSYD4bVcpMJ2yuLmsYdRVkvP5VqLbvLc/FRoBquBpQ7VlrkNjuMBVQjXBsISC0yn9D1tl2z1vbWXWqhqa4Vw2E64aaBYTGXFNZby5J7ZemRSrqCWnDgUwFpB7cQMz8fcwXVJKzhaeX24K17U4uldZGWLYkpgP1agI8TUVYzRAYBOjKoz2drdXg31Q5W6QSWUyL9OiEcjIODgWAoQAwFACGAsBQABgKADNDAVGfASmM+QkovFhtClAAmNkJSJYv9JkWRjE7ASwBaCgADAWAoQAwFACGAsD0uzcOY34C4i3HAcQeCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAAwFgKEAMBQAhgLAUAAYCgBDAWAoAMz/AcegS5kERGEXAAAAAElFTkSuQmCC" /><!-- --></p>
<p>Now five knots have been selected.</p>
<p>It would be good to repeat this analysis to plot the mean of <span class="math inline">\(x_2\)</span> over time also in the same way.</p>
</div>
<div id="time-varying-effects-of-covariates" class="section level2">
<h2>Time-varying effects of covariates</h2>
<p>After this and further exploration of the data, we can go ahead to fit a nontrivial TVEM model, with covariates. We allow both <span class="math inline">\(x_1\)</span> and <span class="math inline">\(x_2\)</span> to potentially have ``time-varying effects’’ (regression relationships with the response that change over time, that is, a potential interaction between time and the covariate, specified without the assumption of linearity). The implied mean model is <span class="math inline">\(E(y|t, x_1(t),x_2(t)) =\beta_0(t)+\beta_1(t)x_1(t)+\beta_2(t)x_2(t)\)</span> where <span class="math inline">\(t\)</span> is time in days.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1"></a>model3 <-<span class="st"> </span><span class="kw">tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb10-2"><a href="#cb10-2"></a> <span class="dt">formula=</span>y<span class="op">~</span>x1<span class="op">+</span>x2,</span>
<span id="cb10-3"><a href="#cb10-3"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb10-4"><a href="#cb10-4"></a> <span class="dt">time=</span>time);</span>
<span id="cb10-5"><a href="#cb10-5"></a><span class="kw">print</span>(model3);</span>
<span id="cb10-6"><a href="#cb10-6"></a><span class="co">#> ======================================================= </span></span>
<span id="cb10-7"><a href="#cb10-7"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb10-8"><a href="#cb10-8"></a><span class="co">#> ======================================================= </span></span>
<span id="cb10-9"><a href="#cb10-9"></a><span class="co">#> Response variable: y </span></span>
<span id="cb10-10"><a href="#cb10-10"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb10-11"><a href="#cb10-11"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb10-12"><a href="#cb10-12"></a><span class="co">#> Effects specified as time-varying: (Intercept), x1, x2</span></span>
<span id="cb10-13"><a href="#cb10-13"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb10-14"><a href="#cb10-14"></a><span class="co">#> ======================================================= </span></span>
<span id="cb10-15"><a href="#cb10-15"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb10-16"><a href="#cb10-16"></a><span class="co">#> Method fREML</span></span>
<span id="cb10-17"><a href="#cb10-17"></a><span class="co">#> Formula:</span></span>
<span id="cb10-18"><a href="#cb10-18"></a><span class="co">#> y ~ x1 + x2 + s(time, bs = "ps", by = NA, pc = 0, k = 24, fx = FALSE) + </span></span>
<span id="cb10-19"><a href="#cb10-19"></a><span class="co">#> s(time, bs = "ps", by = x1, pc = 0, m = c(2, 1), k = 24, </span></span>
<span id="cb10-20"><a href="#cb10-20"></a><span class="co">#> fx = FALSE) + s(time, bs = "ps", by = x2, pc = 0, m = c(2, </span></span>
<span id="cb10-21"><a href="#cb10-21"></a><span class="co">#> 1), k = 24, fx = FALSE)</span></span>
<span id="cb10-22"><a href="#cb10-22"></a><span class="co">#> Pseudolikelihood AIC: 44272.03</span></span>
<span id="cb10-23"><a href="#cb10-23"></a><span class="co">#> Pseudolikelihood BIC: 44373.9 </span></span>
<span id="cb10-24"><a href="#cb10-24"></a><span class="co">#> Note: Used listwise deletion for missing data.</span></span>
<span id="cb10-25"><a href="#cb10-25"></a><span class="co">#> =======================================================</span></span>
<span id="cb10-26"><a href="#cb10-26"></a><span class="kw">plot</span>(model3);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAA8FBMVEUAAAAAADoAAGYAOjoAOmYAOpAAZmYAZrY6AAA6ADo6AGY6OgA6Ojo6OpA6ZmY6ZpA6ZrY6kJA6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZjpmZmZmZpBmkLZmkNtmtttmtv+QOgCQOjqQOmaQZjqQZmaQZraQkGaQkLaQkNuQtpCQttuQ27aQ29uQ2/+2ZgC2Zjq2Zma2kDq2kGa2kJC2tpC2tra2ttu225C22/+2/7a2/9u2///bkDrbkGbbtmbbtpDbtrbb25Db29vb2//b/7bb/9vb////tmb/25D/27b/29v//7b//9v///+fiBzCAAAACXBIWXMAAA7DAAAOwwHHb6hkAAASWklEQVR4nO2di3/btBbH1W69yVjZgJTnStf2Mp6DBjoodNTtuAWSbLH////m6mE5ki3Zsiz5OPH5fbbUduSjI331lpOQDAUqAu3A2IUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgIUAgNUOwJwUmuQXDv45yq/s36zp4YxeTQjZu2g09vZTQj5gL4fT/Zvi6ko9UfTu57NWrm6L/94AeDTU49laT8DBggdzSAA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KgKmtghKkgKie3EutzcOtDOCgEAq10iLbuUCMBfWAOAhQCA1X0pomwEAbRS96WIshEE0ErdZ8JlIwiglYIAQPnLfykCFUTdlyJQnTSKdnbIQgDAQgDAQgDAQgDAQgDAQgDAQgDA6g4gPbd8jIN964rlZ7PNSxrU0vimet0BzCfmRdL10Vm23Lsw32NENj9YrB6Zb9hddQbAFuqMBXp5sKBl2lgFlg9MN7T6kpydUWcALPOtWcdqQVXpV3+YAIxzqTUugMS4eprMLFXm0tpp7K6iAkiMTf36i4UZAO2BbZ3G7iomgMScm/Oz1p3GDiteJ2wp/+wnsZiqGc2sIID2sg1D60aUZmJz2gQ5fDJ8t9QdAC3R5lG9paAz2SdiY6sAuBQBLQQALAQALAQALAQALAQALAQArH4BjHPBs1YIAFi9AqCT5tGtdjYJawCwEACwEACwEACwegYwuqdOGtUvgPR8fAv+DcKZMLAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALAQALD+Dxp3oK8ZrCAsAAAAAElFTkSuQmCC" /><!-- --></p>
<p>The output involved includes the formula which is sent to the back-end calculation package, the mgcv package by Simon Wood. It is not necessary to interpret the pieces of the formula in order to understand the output; it is mainly supplied for advanced users and potential debugging use. In summary, it says that y depends on x1 and x2 as parametric terms, on a set of spline terms based on time, on a set of spline terms based on time multiplied by x1, and on a set of spline terms based on time multiplied by x2. Further details on interpreting the formula can be found in the mgcv documentation.</p>
<p>Holding <span class="math inline">\(x_1\)</span> and <span class="math inline">\(x_2\)</span> constant, the mean of <span class="math inline">\(y\)</span> seems to decline over time. The penalty function causes the the relationship to be estimated as linear because there is no clear evidence of nonlinearity. From the results, <span class="math inline">\(x_1\)</span> appears to have an increasingly positive relationship with <span class="math inline">\(y\)</span> over time. <span class="math inline">\(x_2\)</span> also seems to predict <span class="math inline">\(y\)</span>, but the strength of the relationship does not change over time. Thus, we could reasonably fit a similar but simpler model, but with <span class="math inline">\(x_2\)</span> having a non-time-varying effect even though it has time-varying values.</p>
</div>
<div id="time-invariant-effects-of-covariates" class="section level2">
<h2>Time-invariant effects of covariates</h2>
<p>The following code fits a model where <span class="math inline">\(x_2\)</span> is assumed to have a time-invariant effect but <span class="math inline">\(x_1\)</span> is allowed to have a time-varying effect.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1"></a>model4 <-<span class="st"> </span><span class="kw">tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb11-2"><a href="#cb11-2"></a> <span class="dt">formula=</span>y<span class="op">~</span>x1,</span>
<span id="cb11-3"><a href="#cb11-3"></a> <span class="dt">invar_effect=</span><span class="op">~</span>x2,</span>
<span id="cb11-4"><a href="#cb11-4"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb11-5"><a href="#cb11-5"></a> <span class="dt">time=</span>time);</span>
<span id="cb11-6"><a href="#cb11-6"></a><span class="kw">print</span>(model4);</span>
<span id="cb11-7"><a href="#cb11-7"></a><span class="co">#> ======================================================= </span></span>
<span id="cb11-8"><a href="#cb11-8"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb11-9"><a href="#cb11-9"></a><span class="co">#> ======================================================= </span></span>
<span id="cb11-10"><a href="#cb11-10"></a><span class="co">#> Response variable: y </span></span>
<span id="cb11-11"><a href="#cb11-11"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb11-12"><a href="#cb11-12"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb11-13"><a href="#cb11-13"></a><span class="co">#> Effects specified as time-varying: (Intercept), x1</span></span>
<span id="cb11-14"><a href="#cb11-14"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb11-15"><a href="#cb11-15"></a><span class="co">#> ======================================================= </span></span>
<span id="cb11-16"><a href="#cb11-16"></a><span class="co">#> Effects specified as non-time-varying: </span></span>
<span id="cb11-17"><a href="#cb11-17"></a><span class="co">#> estimate standard_error</span></span>
<span id="cb11-18"><a href="#cb11-18"></a><span class="co">#> x2 0.1753651 0.01585862</span></span>
<span id="cb11-19"><a href="#cb11-19"></a><span class="co">#> ======================================================= </span></span>
<span id="cb11-20"><a href="#cb11-20"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb11-21"><a href="#cb11-21"></a><span class="co">#> Method fREML</span></span>
<span id="cb11-22"><a href="#cb11-22"></a><span class="co">#> Formula:</span></span>
<span id="cb11-23"><a href="#cb11-23"></a><span class="co">#> y ~ x1 + x2 + s(time, bs = "ps", by = NA, pc = 0, k = 24, fx = FALSE) + </span></span>
<span id="cb11-24"><a href="#cb11-24"></a><span class="co">#> s(time, bs = "ps", by = x1, pc = 0, m = c(2, 1), k = 24, </span></span>
<span id="cb11-25"><a href="#cb11-25"></a><span class="co">#> fx = FALSE)</span></span>
<span id="cb11-26"><a href="#cb11-26"></a><span class="co">#> Pseudolikelihood AIC: 44285.93</span></span>
<span id="cb11-27"><a href="#cb11-27"></a><span class="co">#> Pseudolikelihood BIC: 44370.6 </span></span>
<span id="cb11-28"><a href="#cb11-28"></a><span class="co">#> Note: Used listwise deletion for missing data.</span></span>
<span id="cb11-29"><a href="#cb11-29"></a><span class="co">#> =======================================================</span></span>
<span id="cb11-30"><a href="#cb11-30"></a><span class="kw">plot</span>(model4);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYAAAAGACAMAAACTGUWNAAAA2FBMVEUAAAAAADoAAGYAOjoAOmYAOpAAZpAAZrY6AAA6ADo6AGY6OgA6Ojo6OmY6ZmY6ZpA6ZrY6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZmZmZpBmkJBmkLZmkNtmtrZmtttmtv+QOgCQZjqQZmaQkDqQtpCQttuQ29uQ2/+2ZgC2Zjq2ZpC2kDq2kGa2kJC2ttu227a229u22/+2/7a2/9u2///bkDrbkGbb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/hgokB2LX8ErAv2y+1znnkAkLrSOAmrBT/Hi6wkgDYry7LYrw+LhZA9Hu44EoBQJXgw7aMfqkA5jOXAkDThu1OrhjAbIY0tW3Ybr14AH7vEw5QmjagjrqsSUcWaKC4mrkEONbZxv1LoKYX4Xwl4FIB4Jsj/j1gPkMMII3bZQ/GYRqyK582LMvBOExDRG6XPRSBaYjI7bIH4zANwbxZX6cYZRE94fEulIAM2zDswTg0+cQFj3+yDZs9fuTBuJ5FMfLV6M5E9Q+gBE/Fgx8/fl7zBQBow6biYQAx4hJADADQhk3FwwDilGEbdr8AQMQAUgPQmgKbGAADiBMDIAaQvSYAzCwGQKx7CCAvMQBiMQBiMQBiMQBiMQBiMQBiMQBiMQBiMQBiMQBiIQMoRPWqQ4f2YzuvN+o1lbSaP35cAEX1oklXkNWEWOfO4uSqXj9HKYL4UQGoccadPQz5BtbD1r5THemaszyXKOJHBaDe9jwWhisD1185y/aMoogfF4AMY2TSk1ypa1Px4MdzMTJXZB5RxI8KQNWQznqyauIcMe6r+bKuy2s2UcQ/K4B6lbRF6soau/bmEEX8M1dBrtypelfVwXSiiH/mRtgVo8o0dVNMEf983VAVuuPyUhOXqasgivhnvBHbldWnc0bCfmznbCKIH3koYvRufSeEcFaSZQ+DvBtKET8PxhGLARCLARCLARCLARCLARCLARCLAEBBO+ATLdz45wegLZdepJDjZwC+WjqAavbA2DLd3IUdP5cAXy29BDCAvhiArxgAsRgAsRgAse4AAPofvmKEHD/BUMRu0fcB2PHzYByxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACxGACx/g8nPw8EZOsnNQAAAABJRU5ErkJggg==" /><!-- --></p>
<p>The implied mean model is <span class="math inline">\(E(y|t) =\beta_0(t)+\beta_1(t)x_1(t) + \beta_2 x_2(t).\)</span> Notice that only covariates with time-varying effects are listed in the formula argument. The invar_effect argument is used for covariates with time-invariant effects. Also notice that a tilde (~) sign is needed before the list of covariates with time-invariant effects, because it is treated by R as the right side of a formula. If there were multiple covariates with time-invariant effects, they would be listed as follows: “~x2+x3+x4.”</p>
<p>In the output, there is no longer a plot for the coefficient of <span class="math inline">\(x_2\)</span> as a function of time, because <span class="math inline">\(\beta_2\)</span> is considered constant over time even if <span class="math inline">\(x_2\)</span> varies. Instead, the estimate and estimated standard error of <span class="math inline">\(\beta_2\)</span> are given as text in the output from print_tvem.</p>
<p>The seeming oscillations in confidence interval width in the plot do not have any particular interpretation; they are just an artifact of the locations of the knots. Also, the confidence intervals shown are approximate pointwise confidence intervals, much like those in the Methodology Center’s %TVEM SAS macro. They are not simultaneous confidence bands, so they do not directly correct for multiple comparisons.</p>
<p>The main takeaway message from the analysis is the increasing <span class="math inline">\(\beta_1(t)\)</span> over time, suggesting an increasing association between <span class="math inline">\(x_1\)</span> and <span class="math inline">\(y\)</span>, that is, some kind of interaction between <span class="math inline">\(t\)</span> and <span class="math inline">\(x_1\)</span> in predicting <span class="math inline">\(y\)</span>.</p>
</div>
</div>
<div id="example-with-a-binary-outcome-variable" class="section level1">
<h1>Example with a binary outcome variable</h1>
<p>This example will be similar to the previous one, but with a binary response variable. The tvem library’s simulation function will also simulate binary <span class="math inline">\(y\)</span> if requested by an optional argument.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1"></a><span class="kw">set.seed</span>(<span class="dv">123</span>);</span>
<span id="cb12-2"><a href="#cb12-2"></a>the_data <-<span class="st"> </span><span class="kw">simulate_tvem_example</span>(<span class="dt">simulate_binary=</span><span class="ot">TRUE</span>);</span></code></pre></div>
<p>The simulated dataset is similar to the previous example, but with binary <span class="math inline">\(y\)</span> (0=no, 1=yes) generated from a logistic model.</p>
<div id="plotting-the-average-log-odds-over-time" class="section level2">
<h2>Plotting the average log odds over time</h2>
<p>We can plot the expected log odds over time, using an time-varying-intercept-only logistic model. The model assumes <span class="math inline">\(\mathrm{logit}(E(Y|t))=\beta_0(t)\)</span>. The binary outcome is specified using the family argument as in R’s glm function.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1"></a>model1 <-<span class="st"> </span><span class="kw">tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb13-2"><a href="#cb13-2"></a> <span class="dt">formula=</span>y<span class="op">~</span><span class="dv">1</span>,</span>
<span id="cb13-3"><a href="#cb13-3"></a> <span class="dt">family=</span><span class="kw">binomial</span>(),</span>
<span id="cb13-4"><a href="#cb13-4"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb13-5"><a href="#cb13-5"></a> <span class="dt">time=</span>time);</span></code></pre></div>
<p>As before, you can use the default option of an automatic penalty function, or you could specify the number of knots, or you could use automatic selection for the number of knots without a penalty.</p>
</div>
<div id="including-covariates" class="section level2">
<h2>Including covariates</h2>
<p>A model with covariates works similarly to the previous example also.</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1"></a>model2 <-<span class="st"> </span><span class="kw">tvem</span>(<span class="dt">data=</span>the_data,</span>
<span id="cb14-2"><a href="#cb14-2"></a> <span class="dt">formula=</span>y<span class="op">~</span>x1,</span>
<span id="cb14-3"><a href="#cb14-3"></a> <span class="dt">invar_effect=</span><span class="op">~</span>x2,</span>
<span id="cb14-4"><a href="#cb14-4"></a> <span class="dt">id=</span>subject_id,</span>
<span id="cb14-5"><a href="#cb14-5"></a> <span class="dt">family=</span><span class="kw">binomial</span>(),</span>
<span id="cb14-6"><a href="#cb14-6"></a> <span class="dt">time=</span>time);</span>
<span id="cb14-7"><a href="#cb14-7"></a><span class="kw">print</span>(model2);</span>
<span id="cb14-8"><a href="#cb14-8"></a><span class="co">#> ======================================================= </span></span>
<span id="cb14-9"><a href="#cb14-9"></a><span class="co">#> Time-Varying Effects Modeling (TVEM) Function Output </span></span>
<span id="cb14-10"><a href="#cb14-10"></a><span class="co">#> ======================================================= </span></span>
<span id="cb14-11"><a href="#cb14-11"></a><span class="co">#> Response variable: y </span></span>
<span id="cb14-12"><a href="#cb14-12"></a><span class="co">#> Time interval: 0 to 7 </span></span>
<span id="cb14-13"><a href="#cb14-13"></a><span class="co">#> Number of subjects: 300</span></span>
<span id="cb14-14"><a href="#cb14-14"></a><span class="co">#> Effects specified as time-varying: (Intercept), x1</span></span>
<span id="cb14-15"><a href="#cb14-15"></a><span class="co">#> You can use the plot_tvem function to view their plots.</span></span>
<span id="cb14-16"><a href="#cb14-16"></a><span class="co">#> ======================================================= </span></span>
<span id="cb14-17"><a href="#cb14-17"></a><span class="co">#> Effects specified as non-time-varying: </span></span>
<span id="cb14-18"><a href="#cb14-18"></a><span class="co">#> estimate standard_error</span></span>
<span id="cb14-19"><a href="#cb14-19"></a><span class="co">#> x2 0.2074037 0.04819109</span></span>
<span id="cb14-20"><a href="#cb14-20"></a><span class="co">#> ======================================================= </span></span>
<span id="cb14-21"><a href="#cb14-21"></a><span class="co">#> Back-end model fitted in mgcv::bam function: </span></span>
<span id="cb14-22"><a href="#cb14-22"></a><span class="co">#> Method fREML</span></span>
<span id="cb14-23"><a href="#cb14-23"></a><span class="co">#> Formula:</span></span>
<span id="cb14-24"><a href="#cb14-24"></a><span class="co">#> y ~ x1 + x2 + s(time, bs = "ps", by = NA, pc = 0, k = 24, fx = FALSE) + </span></span>
<span id="cb14-25"><a href="#cb14-25"></a><span class="co">#> s(time, bs = "ps", by = x1, pc = 0, m = c(2, 1), k = 24, </span></span>
<span id="cb14-26"><a href="#cb14-26"></a><span class="co">#> fx = FALSE)</span></span>
<span id="cb14-27"><a href="#cb14-27"></a><span class="co">#> Pseudolikelihood AIC: 8609.09</span></span>
<span id="cb14-28"><a href="#cb14-28"></a><span class="co">#> Pseudolikelihood BIC: 8657.7 </span></span>
<span id="cb14-29"><a href="#cb14-29"></a><span class="co">#> Note: Used listwise deletion for missing data.</span></span>
<span id="cb14-30"><a href="#cb14-30"></a><span class="co">#> =======================================================</span></span>
<span id="cb14-31"><a href="#cb14-31"></a><span class="kw">plot</span>(model2);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASAAAAEgCAMAAAAjXV6yAAAA2FBMVEUAAAAAADoAAGYAOjoAOmYAOpAAZpAAZrY6AAA6ADo6AGY6OgA6Ojo6OmY6ZmY6ZpA6ZrY6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZmZmZpBmkJBmkLZmkNtmtrZmtttmtv+QOgCQZjqQZmaQkDqQtpCQttuQ29uQ2/+2ZgC2Zjq2ZpC2kDq2kGa2kJC2ttu227a229u22/+2/7a2/9u2///bkDrbkGbbtmbbtpDbtrbb27bb29vb2//b/7bb////tmb/25D/27b/29v//7b//9v///9ma166AAAACXBIWXMAAA7DAAAOwwHHb6hkAAALz0lEQVR4nO2dDXvbthWFITeetNlZXKtNNndpY2VNa2Xduniz6qxpTbuS/v8/GgiAJEgAugAJkHB9zvPEoQTy4uglAOJDpNgeOig2tYHcBUCEAIgQABECIEIARAiACAEQIQAiBECEAIgQABECIEIARAiACAEQIQAiBECEAIgQABECIEIaoDWr9YcVm12JN+8X7PhuUycc34nd+H/qgLl3Trv3C8bOxN8XKxWhSuq8zsmPHdCcWzgXb4oNw5Bwu10GGSrK48/F389DAU3oxwGIZyZ24bse3ZiGhFt+MkMMbcTH2FRFgVYefjptEDcwV+7EgTzTeRmFu9J8nwq3wmZj6PaEsc/eiM2Pr/jma3EOdu/5+T27Ux93diL+fq/OUHWIOmNi39z8uAAV8qzwTC9NQ39ecLe71dG3miF1vtVB6uzKYs/YsxubofoQaUjum5sflyGeyk3IEq0V6UsR5s2KR7lfHP/QGOIf4PO7B3GQaEdlY8F3Prou3zhvFWlpoDlEvpb75ubHBajM91KFMgxdrnmAgp1vGkNr6fzFh2pT/Mc/ljxeNBwdQ80h4rXaNzc/TkBFmZmq+Yah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BAAEUoJaJKpeqd6+kk41Fhn1Q/q6weDVUIARAiACAEQIQAiBECEAIgQABECIEIARAiACAEQIQAiBECEAIgQABECIEIARAiACAEQIQAiBECEAIgQABECIEIARAiACAEQIQAiBECEAIgQABECIEIARAiACAEQIQAiBECEAIgQABECIEIARAiACAEQof8DONtsbREUtuQAAAAASUVORK5CYII=" /><!-- --></p>
<p>The implied mean model is <span class="math inline">\(\mathrm{logit}(E(y|t)) =\beta_0(t)+\beta_1(t)x_1(t) + \beta_2 x_2(t).\)</span> In this simulated example, it isn’t very clear whether the effect of <span class="math inline">\(x_1\)</span> changes over time or not, although it seems to do so. Logistic regression analyses often have wider confidence intervals than ordinary regression analyses with the same sample size.</p>
<p>With logistic TVEM, we can additionally plot exponentiated coefficients to get odds and odds ratios. In the plots below, the exponentiated intercept function shows the estimated odds of <span class="math inline">\(Y=1\)</span> at a given time, assuming <span class="math inline">\(x_1=x_2=0\)</span>. The exponentiated beta function for <span class="math inline">\(x_1\)</span> shows the estimated odds ratio for <span class="math inline">\(Y=1\)</span> given a 1-unit increase in <span class="math inline">\(x_1\)</span> at a given time.</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1"></a><span class="kw">plot</span>(model2, <span class="dt">exponentiate=</span><span class="ot">TRUE</span>);</span></code></pre></div>
<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASAAAAEgCAMAAAAjXV6yAAAA5FBMVEUAAAAAADoAAGYAOjoAOmYAOpAAZpAAZrY6AAA6ADo6AGY6OgA6Ojo6OmY6OpA6ZmY6ZpA6ZrY6kLY6kNtmAABmADpmAGZmOgBmOjpmOmZmOpBmZmZmZpBmkJBmkLZmkNtmtrZmtttmtv+QOgCQOmaQZjqQZmaQkDqQkLaQtpCQttuQ29uQ2/+2ZgC2Zjq2ZpC2kDq2kGa2kJC2ttu227a229u22/+2/7a2/9u2///bkDrbkGbbtmbbtpDbtrbb25Db27bb29vb2//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ERIEAECBABAkSAABEgQAQIUEZA5SzjZ7/G+ckHyPsFSTNppB8CBCgboP0m/quKcmqsHypBgAgQIAIEiAABIkCACBCgnIBmWar3aqSfjFONYlHjoLF+aLIKiAABIkCACBAgAgSIAAEiQIAIECACBIgAASJAgAgQIAIEiAABIkCACBAgAgSIAAEiQIAIECACBIgAASJAgAgQIAIEiAABIkCACBAgAgSIAAEiQIAIECACBIgAASJAgAgQIAIEiAABIkCACBAgAgSIAAEiQIAIEKD/A4mow9m/kRN/AAAAAElFTkSuQmCC" /><!-- --></p>
<p>Binary outcomes are not the only non-normal outcomes which can be modeled using TVEM. For example, Poisson count outcomes with a log link can be modeled by specifying family=poisson() instead of family=binomial(). However, for overdispersed or zero-inflated counts, the Poisson distribution might not be appropriate.<br />
We also do not currently have a simulation function in the tvem package for Poisson data, because generating realistic longitudinal count data is beyond the scope of this package.</p>
</div>
</div>
<div id="references" class="section level1">
<h1>References</h1>
<ul>
<li><p>de Boor, C. (1972). On calculating with B-splines. Journal of Approximation Theory, 6: 50–62.</p></li>
<li><p>Eilers, P. H. C., & Marx, B. D. (1996). Flexible smoothing with B-splines and penalties. Statistical Science, 11: 89-121.</p></li>
<li><p>Hastie, T, Tibshirani, R. (1993). Varying-coefficient models. Journal of the Royal Statistical Socety, B, 55:757-796.</p></li>
<li><p>Li, R., Dziak, J. J., Tan, X., Huang, L., Wagner, A. T., & Yang, J. (2017). TVEM (time-varying effect model) SAS macro users’ guide (Version 3.1.1). University Park: The Methodology Center, Penn State. Available online at [<a href="https://www.methodology.psu.edu/downloads/tvem/" class="uri">https://www.methodology.psu.edu/downloads/tvem/</a>])(<a href="https://www.methodology.psu.edu/downloads/tvem/" class="uri">https://www.methodology.psu.edu/downloads/tvem/</a>) and archived at <a href="https://github.com/dziakj1/MethodologyCenterTVEMmacros">https://github.com/dziakj1/MethodologyCenterTVEMmacros</a> and <a href="https://scholarsphere.psu.edu/collections/v41687m23q">https://scholarsphere.psu.edu/collections/v41687m23q</a>.</p></li>
<li><p>Ruppert, D., Wand, M. P., & Carroll, R. J. (2003). Semiparametric regression. Cambridge: Cambridge University Press.</p></li>
<li><p>Tan, X., Shiyko, M. P., Li, R., Li, Y., & Dierker, L. (2012). A time-varying effect model for intensive longitudinal data. Psychological Methods, 17: 61-77.</p></li>
</ul>
</div>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>