-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathch9_measurement_solutions.html
497 lines (469 loc) · 35.6 KB
/
ch9_measurement_solutions.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.2.335">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="author" content="Bria Long, 10/23/24">
<title>Measurement Simulations: Test-Retest Reliability</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > 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 {
pre > 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 { color: #008000; } /* 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 { color: #008000; font-weight: bold; } /* 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 src="ch9_measurement_solutions_files/libs/clipboard/clipboard.min.js"></script>
<script src="ch9_measurement_solutions_files/libs/quarto-html/quarto.js"></script>
<script src="ch9_measurement_solutions_files/libs/quarto-html/popper.min.js"></script>
<script src="ch9_measurement_solutions_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="ch9_measurement_solutions_files/libs/quarto-html/anchor.min.js"></script>
<link href="ch9_measurement_solutions_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="ch9_measurement_solutions_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="ch9_measurement_solutions_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="ch9_measurement_solutions_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="ch9_measurement_solutions_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
</head>
<body class="fullcontent">
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Measurement Simulations: Test-Retest Reliability</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Author</div>
<div class="quarto-title-meta-contents">
<p>Bria Long, 10/23/24 </p>
</div>
</div>
</div>
</header>
<section id="setup" class="level1">
<h1>Setup</h1>
<section id="learning-goals" class="level2">
<h2 class="anchored" data-anchor-id="learning-goals">Learning goals</h2>
<ol type="1">
<li>Simulate data for two experiments and compute test-retest reliability</li>
<li>Practice some tidyverse (<code>pivot_longer</code>, <code>mutate</code>, <code>select</code>, and add onto existing base ggplot skills (<code>geom_point</code>, <code>geom_jitter</code>, <code>facet_wrap</code>, <code>geom_line</code>)</li>
<li>Run a basic correlation (<code>cor.test</code> and interpret differences in observed reliability based on differences in the simulated data)</li>
</ol>
</section>
<section id="import-the-libraries-we-need" class="level2">
<h2 class="anchored" data-anchor-id="import-the-libraries-we-need">Import the libraries we need</h2>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.2 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.3 ✔ tibble 3.2.1
✔ lubridate 1.9.2 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors</code></pre>
</div>
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2) <span class="co"># plotting</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="define-the-simulation-function---same-as-before" class="level2">
<h2 class="anchored" data-anchor-id="define-the-simulation-function---same-as-before">Define the simulation function - same as before</h2>
<p>This makes “tea data”, a tibble (dataframe) where there are a certain number of people in each condition (default = 48, i.e., n_total, with n_total/2 in each half)</p>
<p>The averages of the two conditions are separated by a known effect (“delta”) with some variance (“sigma”). You can change these around since we’re simulating data!</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>) <span class="co"># good practice to set a random seed, or else different runs get you different results</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>make_tea_data <span class="ot"><-</span> <span class="cf">function</span>(<span class="at">n_total =</span> <span class="dv">48</span>,</span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="at">sigma =</span> <span class="fl">1.25</span>,</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="at">delta =</span> <span class="fl">1.5</span>) {</span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a> n_half <span class="ot"><-</span> n_total <span class="sc">/</span> <span class="dv">2</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">tibble</span>(<span class="at">condition =</span> <span class="fu">c</span>(<span class="fu">rep</span>(<span class="st">"milk first"</span>, n_half), <span class="fu">rep</span>(<span class="st">"tea first"</span>, n_half)),</span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="at">rating =</span> <span class="fu">c</span>(<span class="fu">round</span>(</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">rnorm</span>(n_half, <span class="at">mean =</span> <span class="fl">3.5</span> <span class="sc">+</span> delta, <span class="at">sd =</span> sigma)</span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a> ),</span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">round</span>(<span class="fu">rnorm</span>(</span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> n_half, <span class="at">mean =</span> <span class="fl">3.5</span>, <span class="at">sd =</span> sigma</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a> )))) <span class="sc">|></span></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rating =</span> <span class="fu">if_else</span>(rating <span class="sc">></span> <span class="dv">10</span>, <span class="dv">10</span>, rating),</span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="co"># truncate if greater than max/min of rating scale</span></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a> <span class="at">rating =</span> <span class="fu">if_else</span>(rating <span class="sc"><</span> <span class="dv">1</span>, <span class="dv">1</span>, rating)) <span class="sc">|></span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">rownames_to_column</span>() <span class="sc">%>%</span> <span class="co"># added for this excercise</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="at">sub_num =</span> rowname)</span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="make-a-dataframe-with-our-simulated-data" class="level2">
<h2 class="anchored" data-anchor-id="make-a-dataframe-with-our-simulated-data">Make a dataframe with our simulated data</h2>
<p>Input more participants (60 per condition) with a bigger average difference between conditions (2 points), with variance between participants at 2 points (sigma)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># YOUR CODE HERE</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>this_tea_data <span class="ot"><-</span> <span class="fu">make_tea_data</span>(<span class="at">n_total =</span> <span class="dv">60</span>, <span class="at">delta =</span> <span class="dv">2</span>, <span class="at">sigma =</span> <span class="dv">2</span>) </span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="creating-the-second-experiment" class="level2">
<h2 class="anchored" data-anchor-id="creating-the-second-experiment">Creating the second experiment</h2>
<p>Now create a new column in your tibble for the second experiment.</p>
<p>Here, the rating of the <em>simulated</em> second experiment data is each participants first rating with some variance (people are likely to not say exactly the same thing)</p>
<p>TIPS:</p>
<p>Strongly recommend running <code>rowwise()</code> in your pipe before creating the new condition to force tidyverse to sample a new random value for each row</p>
<p>Make your next dataframe A NEW NAME so that you’re not rewriting old dataframes with new ones and getting confused</p>
<p>Hint: you can use <code>to_sample = -3:3</code> with the <code>sample</code> function and specifies the possible values you want to sample from</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># YOUR CODE HERE</span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>to_sample <span class="ot">=</span> <span class="sc">-</span><span class="dv">3</span><span class="sc">:</span><span class="dv">3</span></span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a>all_tea_data <span class="ot"><-</span> this_tea_data <span class="sc">%>%</span></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="at">rating_exp_1 =</span> rating) <span class="sc">%>%</span></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">difference =</span> <span class="fu">sample</span>(to_sample,<span class="dv">1</span>)) <span class="sc">%>%</span></span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rating_exp_2 =</span> rating_exp_1 <span class="sc">+</span> difference) <span class="sc">%>%</span></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># make sure ratings can't go above/below limits of scale</span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rating_exp_2 =</span> <span class="fu">if_else</span>(rating_exp_2 <span class="sc">></span> <span class="dv">10</span>, <span class="dv">10</span>, rating_exp_2),</span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="co"># truncate if greater than max/min of rating scale</span></span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a> <span class="at">rating_exp_2 =</span> <span class="fu">if_else</span>(rating_exp_2 <span class="sc"><</span> <span class="dv">1</span>, <span class="dv">1</span>, rating_exp_2))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="make-a-plot" class="level2">
<h2 class="anchored" data-anchor-id="make-a-plot">Make a plot</h2>
<p>Examine how the ratings are correlated across these simulations</p>
<ol type="1">
<li>Make a plot relating these two variables</li>
<li>Try out <code>geom_point</code> which shows you the exact values</li>
<li>Then try out <code>geom_jitter</code> which shows you the same data with some jitter around height / width</li>
</ol>
<p>Extra: 3. Use <code>alpha</code> to make your dots transparent 4. Use <code>ylab</code> and <code>xlab</code> to make nice axes labels 5. Use geom_smooth() to look at the trend_line 6. Try making each dot different by condition</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># YOUR CODE HERE</span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(all_tea_data, <span class="fu">aes</span>(<span class="at">x=</span>rating_exp_1, <span class="at">y=</span>rating_exp_2)) <span class="sc">+</span></span>
<span id="cb8-3"><a href="#cb8-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_jitter</span>(<span class="at">alpha=</span>.<span class="dv">8</span>, <span class="at">height=</span>.<span class="dv">1</span>, <span class="at">width=</span>.<span class="dv">1</span>) <span class="sc">+</span></span>
<span id="cb8-4"><a href="#cb8-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">'Experiment 2'</span>) <span class="sc">+</span></span>
<span id="cb8-5"><a href="#cb8-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">'Experiment 1'</span>) <span class="sc">+</span></span>
<span id="cb8-6"><a href="#cb8-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggtitle</span>(<span class="st">'Test-retest reliability'</span>) <span class="sc">+</span></span>
<span id="cb8-7"><a href="#cb8-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_smooth</span>(<span class="at">method=</span><span class="st">'lm'</span>) <span class="sc">+</span></span>
<span id="cb8-8"><a href="#cb8-8" aria-hidden="true" tabindex="-1"></a> ggpubr<span class="sc">::</span><span class="fu">stat_cor</span>(<span class="at">method=</span><span class="st">'pearson'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>`geom_smooth()` using formula = 'y ~ x'</code></pre>
</div>
<div class="cell-output-display">
<p><img src="ch9_measurement_solutions_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>Hint: use geom_point in ggplot2 or you can use qqplot</p>
<p>Now examine – how correlated are your responses? What is your test-retest reliability? Hint: You’ll need a correlation function - cor.test You can also find a ggplot function to overlay the correlation on top of your plot</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="fu">cor.test</span>(all_tea_data<span class="sc">$</span>rating_exp_1, all_tea_data<span class="sc">$</span>rating_exp_2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Pearson's product-moment correlation
data: all_tea_data$rating_exp_1 and all_tea_data$rating_exp_2
t = 5.3632, df = 58, p-value = 1.487e-06
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.3769631 0.7238674
sample estimates:
cor
0.5757745 </code></pre>
</div>
</div>
</section>
<section id="make-another-plot-like-the-one-from-the-chapter-where-each-line-should-connect-an-individual-subject" class="level2">
<h2 class="anchored" data-anchor-id="make-another-plot-like-the-one-from-the-chapter-where-each-line-should-connect-an-individual-subject">Make another plot – like the one from the chapter, where each line should connect an individual subject</h2>
<ol type="1">
<li>First, Use pivot_longer to make the dataframe longer</li>
<li>Use facet_wrap(~condition) to make two plots, one for <code>milk_first</code> and one for <code>tea_first</code></li>
<li>Use geom_line – with grouping specification by sub_id – to connect individual datapoints for each condition</li>
</ol>
<p>Hint aes(group = sub_id))</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Make new, longer dataframe - your code here</span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>all_tea_data_longer <span class="ot"><-</span> all_tea_data <span class="sc">%>%</span></span>
<span id="cb12-3"><a href="#cb12-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">pivot_longer</span>(<span class="at">cols =</span> <span class="fu">c</span>(<span class="st">'rating_exp_1'</span>,<span class="st">'rating_exp_2'</span>), <span class="at">names_to =</span> <span class="st">"experiment"</span>, <span class="at">values_to=</span><span class="st">'rating'</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(all_tea_data_longer, <span class="fu">aes</span>(<span class="at">x=</span>experiment, <span class="at">y=</span>rating, <span class="at">col=</span>condition)) <span class="sc">+</span></span>
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">alpha=</span>.<span class="dv">8</span>) <span class="sc">+</span></span>
<span id="cb13-3"><a href="#cb13-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="fu">aes</span>(<span class="at">group=</span>sub_num)) <span class="sc">+</span></span>
<span id="cb13-4"><a href="#cb13-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ylab</span>(<span class="st">'Experiment 2'</span>) <span class="sc">+</span></span>
<span id="cb13-5"><a href="#cb13-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">xlab</span>(<span class="st">'Experiment 1'</span>) <span class="sc">+</span></span>
<span id="cb13-6"><a href="#cb13-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggtitle</span>(<span class="st">'Test-retest reliability'</span>) <span class="sc">+</span></span>
<span id="cb13-7"><a href="#cb13-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>condition)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="ch9_measurement_solutions_files/figure-html/unnamed-chunk-9-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
</section>
<section id="ok-now-go-back-and-change-things-and-test-your-intuition-about-how-this-works." class="level1">
<h1>OK, now go back and change things and test your intuition about how this works.</h1>
<p>How does reliability change if you increase the variance between participants (sigma) in the first experiment simulated data?</p>
<p>How does reliability change if you change how much variation you allow between the first and second experiment?</p>
<p>How does reliability change if you increase sample size, holding those things constant?</p>
<p>Hint: copy the code from above where you made your new dataframe with experiment number 2, copy the correlation computation code, and just run this block over an over, modifying the code (command-shift-enter on Mac runs the block)</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>to_sample <span class="ot">=</span> <span class="sc">-</span><span class="dv">4</span><span class="sc">:</span><span class="dv">4</span></span>
<span id="cb14-2"><a href="#cb14-2" aria-hidden="true" tabindex="-1"></a>simulated_data <span class="ot"><-</span> <span class="fu">make_tea_data</span>(<span class="at">n_total =</span> <span class="dv">100</span>, <span class="at">delta =</span> <span class="dv">2</span>, <span class="at">sigma =</span> <span class="dv">5</span>) <span class="sc">%>%</span></span>
<span id="cb14-3"><a href="#cb14-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">rowwise</span>() <span class="sc">%>%</span></span>
<span id="cb14-4"><a href="#cb14-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">rename</span>(<span class="at">rating_exp_1 =</span> rating) <span class="sc">%>%</span></span>
<span id="cb14-5"><a href="#cb14-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">difference_between_experiments =</span> <span class="fu">sample</span>(to_sample,<span class="dv">1</span>)) <span class="sc">%>%</span></span>
<span id="cb14-6"><a href="#cb14-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rating_exp_2 =</span> rating_exp_1 <span class="sc">+</span> difference_between_experiments) <span class="sc">%>%</span></span>
<span id="cb14-7"><a href="#cb14-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># make sure ratings can't go above/below limits of scale</span></span>
<span id="cb14-8"><a href="#cb14-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rating_exp_2 =</span> <span class="fu">if_else</span>(rating_exp_2 <span class="sc">></span> <span class="dv">10</span>, <span class="dv">10</span>, rating_exp_2),</span>
<span id="cb14-9"><a href="#cb14-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># truncate if greater than max/min of rating scale</span></span>
<span id="cb14-10"><a href="#cb14-10" aria-hidden="true" tabindex="-1"></a> <span class="at">rating_exp_2 =</span> <span class="fu">if_else</span>(rating_exp_2 <span class="sc"><</span> <span class="dv">1</span>, <span class="dv">1</span>, rating_exp_2))</span>
<span id="cb14-11"><a href="#cb14-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-12"><a href="#cb14-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb14-13"><a href="#cb14-13" aria-hidden="true" tabindex="-1"></a><span class="fu">cor.test</span>(simulated_data<span class="sc">$</span>rating_exp_1, simulated_data<span class="sc">$</span>rating_exp_2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>
Pearson's product-moment correlation
data: simulated_data$rating_exp_1 and simulated_data$rating_exp_2
t = 10.846, df = 98, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.6341714 0.8165485
sample estimates:
cor
0.7385889 </code></pre>
</div>
</div>
</section>
<section id="now-lets-compute-split-half-reliability-in-one-experiment" class="level1">
<h1>Now let’s compute split-half reliability in one experiment</h1>
<p>Go back to the first dataframe you created (before you added experiment number 2) Split it in two halves and correlate the rating scores from each half (hint: use <code>sample_frac</code> and <code>anti_join</code>)</p>
<p>What drives reliability up or down here? Is it the same thing?</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="do">##</span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>first_half <span class="ot"><-</span> this_tea_data <span class="sc">%>%</span></span>
<span id="cb16-3"><a href="#cb16-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(condition) <span class="sc">%>%</span></span>
<span id="cb16-4"><a href="#cb16-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">sample_frac</span>(.<span class="dv">5</span>)</span>
<span id="cb16-5"><a href="#cb16-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb16-6"><a href="#cb16-6" aria-hidden="true" tabindex="-1"></a>second_half <span class="ot"><-</span> this_tea_data <span class="sc">%>%</span></span>
<span id="cb16-7"><a href="#cb16-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(condition) <span class="sc">%>%</span></span>
<span id="cb16-8"><a href="#cb16-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">anti_join</span>(first_half)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Joining with `by = join_by(sub_num, condition, rating)`</code></pre>
</div>
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>split_half_correlation <span class="ot">=</span> <span class="fu">cor.test</span>(second_half<span class="sc">$</span>rating, first_half<span class="sc">$</span>rating)</span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>split_half_correlation_out <span class="ot">=</span> split_half_correlation<span class="sc">$</span>estimate</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="extra-sections-as-reference" class="level1">
<h1>Extra sections as reference</h1>
<section id="spearman-brown-correction-for-split-half-reliability" class="level2">
<h2 class="anchored" data-anchor-id="spearman-brown-correction-for-split-half-reliability">Spearman brown correction for split half reliability</h2>
<p>This corrects for the fact that we have fewer items when we split the text</p>
<p>Formula from: https://en.wikipedia.org/wiki/Spearman%E2%80%93Brown_prediction_formula</p>
<p>Hint: to get out the actual value from cor.test, you need to get the <code>estimate</code> field from the output</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>corrected_split_half <span class="ot">=</span> (<span class="dv">2</span><span class="sc">*</span>split_half_correlation_out) <span class="sc">/</span> (<span class="dv">1</span><span class="sc">+</span>split_half_correlation_out)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const clipboard = new window.ClipboardJS('.code-copy-button', {
target: function(trigger) {
return trigger.previousElementSibling;
}
});
clipboard.on('success', function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
});
function tippyHover(el, contentFn) {
const config = {
allowHTML: true,
content: contentFn,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start'
};
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
return note.innerHTML;
});
}
const findCites = (el) => {
const parentEl = el.parentElement;
if (parentEl) {
const cites = parentEl.dataset.cites;
if (cites) {
return {
el,
cites: cites.split(' ')
};
} else {
return findCites(el.parentElement)
}
} else {
return undefined;
}
};
var bibliorefs = window.document.querySelectorAll('a[role="doc-biblioref"]');
for (var i=0; i<bibliorefs.length; i++) {
const ref = bibliorefs[i];
const citeInfo = findCites(ref);
if (citeInfo) {
tippyHover(citeInfo.el, function() {
var popup = window.document.createElement('div');
citeInfo.cites.forEach(function(cite) {
var citeDiv = window.document.createElement('div');
citeDiv.classList.add('hanging-indent');
citeDiv.classList.add('csl-entry');
var biblioDiv = window.document.getElementById('ref-' + cite);
if (biblioDiv) {
citeDiv.innerHTML = biblioDiv.innerHTML;
}
popup.appendChild(citeDiv);
});
return popup.innerHTML;
});
}
}
});
</script>
</div> <!-- /content -->
</body></html>