-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic_reuse.Rmd
706 lines (540 loc) · 20 KB
/
logic_reuse.Rmd
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
---
title: "Logic Reuse Model"
author: Hampus Broman & William Levén
date: 2021-05
output:
html_document:
pandoc_args: [ "-o", "docs/logic_reuse.html" ]
---
```{r include-setup, include=FALSE}
# Load setup file
source(knitr::purl('setup.Rmd', output = tempfile()))
```
## Looking at the data
There appears to be significant difference in the reuse rate between high and low debt versions.
### Constructor reuse
```{r plot-constructor}
d.completed %>%
mutate_at("high_debt_version",
function(x) case_when(
x == "false" ~ "Low debt",
x == "true" ~ "High debt"
)) %>%
ggplot(aes(high_debt_version)) +
geom_bar(aes(fill = reused_logic_constructor), position = "fill") +
scale_fill_manual("Legend", values = c("lightblue", "darkblue"), labels = c("Reused", "Duplicated")) +
labs(title = "Constructor reuse") +
xlab("Debt version") +
ylab("Ratio of reuse")
```
### Validation reuse
```{r plot-validation}
d.completed %>%
mutate_at("high_debt_version",
function(x) case_when(
x == "false" ~ "Low debt",
x == "true" ~ "High debt"
)) %>%
ggplot(aes(high_debt_version)) +
geom_bar(aes(fill = reused_logic_validation), position = "fill") +
scale_fill_manual("Legend", values = c("lightblue", "darkblue"), labels = c("Reused", "Duplicated")) +
labs(title = "Validation reuse") +
xlab("Debt version") +
ylab("Ratio of reuse")
```
## Initial model
For a boolean outcome, bernoulli is the most suitable family.
We include `high_debt_verison` as a predictor in our model as this variable represent the very effect we want to measure.
We also include a varying intercept for each individual to prevent the model from learning too much from single participants with extreme measurements.
Since they may correlate, constructor and logic reuse are both included in a single multivariate model.
### Selecting priors {.tabset}
We iterate over the model until we have sane priors, in this case a prior giving a 50/50 chance was chosen in both cases.
The prior `lkj(2)` will mean the model is skeptical of strong correlations.
#### Base model with priors
```{r initial-model-definition, class.source = 'fold-show'}
reuse.with <- extendable_model(
base_name = "reuse",
base_formula = "mvbind(
reused_logic_validation,
reused_logic_constructor
) ~ 1 + high_debt_version + (1 |c| session)",
base_priors = c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 1), class = "Intercept"),
prior(exponential(1), class = "sd", resp = reusedlogicconstructor),
prior(exponential(1), class = "sd", resp = reusedlogicvalidation),
prior(lkj(2), class = "L")
),
family = bernoulli(),
data = d.both_completed,
base_control = list(adapt_delta = 0.95)
)
```
#### Default priors
```{r default-priors}
prior_summary(reuse.with(only_priors= TRUE))
```
#### Selected priors
```{r selected-priors, warning=FALSE}
prior_summary(reuse.with(sample_prior = "only"))
```
#### Prior predictive check
##### Constructor reuse
```{r priors-check-constructor, warning=FALSE, message=FALSE}
pp_check(reuse.with(sample_prior = "only"), type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
##### Validation reuse
```{r priors-check-validation, warning=FALSE, message=FALSE}
pp_check(reuse.with(sample_prior = "only"), type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
#### Beta parameter influence
We choose a beta parameter priors allowing for the beta parameter to account for 100% of the effect but that is skeptical to such strong effects from the beta parameter.
```{r priors-beta, warning=FALSE}
sim.size <- 1000
sim.intercept <- rnorm(sim.size, 0, 1)
sim.beta <- rnorm(sim.size, 0, 1)
sim.beta.diff <- (plogis(sim.intercept + sim.beta) / plogis(sim.intercept) * 100) - 100
data.frame(x = sim.beta.diff) %>%
ggplot(aes(x)) +
geom_density() +
xlim(-150, 150) +
labs(
title = "Beta parameter prior influence",
x = "Estimate with beta as % of estimate without beta",
y = "Density"
)
```
### Model fit {.tabset}
We check the posterior distribution and can see that the model seems to have been able to fit the data well.
Sampling seems to also have worked well as Rhat values are close to 1 and the sampling plots look nice.
#### Posterior predictive check
##### Constructor reuse
```{r base-pp-check-constructor, warning=FALSE, message=FALSE}
pp_check(reuse.with(), type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
##### Validation reuse
```{r base-pp-check-validation, warning=FALSE, message=FALSE}
pp_check(reuse.with(), type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
#### Summary
```{r base-summary, warning=FALSE,}
summary(reuse.with())
```
#### Sampling plots
```{r base-plot, warning=FALSE, message=FALSE}
plot(reuse.with(), ask = FALSE)
```
## Model predictor extenstions {.tabset}
```{r mo-priors}
# default prior for monotonic predictor
edlvl_prior <- c(
prior(dirichlet(2), class = "simo", coef = "moeducation_level1", resp = "reusedlogicconstructor"),
prior(dirichlet(2), class = "simo", coef = "moeducation_level1", resp = "reusedlogicvalidation")
)
```
We use `loo` to check some possible extensions on the model.
### One variable {.tabset}
```{r model-extension-1, warning=FALSE, class.source = 'fold-show', message=FALSE}
loo_result <- loo(
# Benchmark model(s)
reuse.with(),
# New model(s)
reuse.with("work_domain"),
reuse.with("work_experience_programming.s"),
reuse.with("work_experience_java.s"),
reuse.with("education_field"),
reuse.with("mo(education_level)", edlvl_prior),
reuse.with("workplace_peer_review"),
reuse.with("workplace_td_tracking"),
reuse.with("workplace_pair_programming"),
reuse.with("workplace_coding_standards"),
reuse.with("scenario"),
reuse.with("group")
)
```
#### Comparison
```{r model-extension-1-sum, warning=FALSE}
loo_result[2]
```
#### Diagnostics
```{r model-extension-1-dig, warning=FALSE}
loo_result[1]
```
### Two variables {.tabset}
```{r model-extension-2, warning=FALSE, class.source = 'fold-show', message=FALSE}
loo_result <- loo(
# Benchmark model(s)
reuse.with(),
reuse.with("scenario"),
reuse.with("workplace_peer_review"),
reuse.with("mo(education_level)", edlvl_prior),
reuse.with("workplace_coding_standards"),
# New model(s)
reuse.with(c("scenario", "workplace_peer_review")),
reuse.with(c("scenario", "mo(education_level)"), edlvl_prior),
reuse.with(c("scenario", "workplace_coding_standards")),
reuse.with(c("workplace_peer_review", "mo(education_level)"), edlvl_prior),
reuse.with(c("workplace_peer_review", "workplace_coding_standards")),
reuse.with(c("workplace_coding_standards", "mo(education_level)"), edlvl_prior)
)
```
#### Comparison
```{r model-extension-2-sum, warning=FALSE}
loo_result[2]
```
#### Diagnostics
```{r model-extension-2-dig, warning=FALSE}
loo_result[1]
```
### Three variables {.tabset}
```{r model-extension-3, warning=FALSE, class.source = 'fold-show', message=FALSE}
loo_result <- loo(
# Benchmark model(s)
reuse.with(),
reuse.with("scenario"),
reuse.with("workplace_peer_review"),
reuse.with("mo(education_level)", edlvl_prior),
reuse.with("workplace_coding_standards"),
reuse.with(c("scenario", "workplace_coding_standards")),
# New model(s)
reuse.with(c("scenario", "workplace_coding_standards", "mo(education_level)"), edlvl_prior)
)
```
#### Comparison
```{r model-extension-3-sum, warning=FALSE}
loo_result[2]
```
#### Diagnostics
```{r model-extension-3-dig, warning=FALSE}
loo_result[1]
```
## Candidate models {.tabset}
We pick some of our top performing models as candidates and inspect them closer.
The candidate models are named and listed in order of complexity.
### Reuse0 {.tabset}
```{r reuse0, class.source = 'fold-show', warning=FALSE, message=FALSE}
reuse0 <- brm(
"mvbind(
reused_logic_validation,
reused_logic_constructor
) ~ 1 + high_debt_version + (1 |c| session)",
prior = c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 1), class = "Intercept"),
prior(exponential(1), class = "sd", resp = reusedlogicconstructor),
prior(exponential(1), class = "sd", resp = reusedlogicvalidation),
prior(lkj(2), class = "L")
),
family = bernoulli(),
data = as.data.frame(d.both_completed),
control = list(adapt_delta = 0.95),
file = "fits/reuse0",
file_refit = "on_change",
seed = 20210421
)
```
#### Summary
```{r reuse0-sum}
summary(reuse0)
```
#### Random effects
```{r reuse0-raneff}
ranef(reuse0)
```
#### Sampling plots
```{r reuse0-plot}
plot(reuse0, ask = FALSE)
```
#### Posterior predictive check
##### Constructor reuse
```{r reuse0-pp-constructor}
pp_check(reuse0, type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
##### Validation reuse
```{r reuse0-pp-validation}
pp_check(reuse0, type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
### Reuse1 {.tabset}
```{r reuse1, class.source = 'fold-show', warning=FALSE, message=FALSE}
reuse1 <- brm(
"mvbind(
reused_logic_validation,
reused_logic_constructor
) ~ 1 + high_debt_version + scenario + (1 |c| session)",
prior = c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 1), class = "Intercept"),
prior(exponential(1), class = "sd", resp = reusedlogicconstructor),
prior(exponential(1), class = "sd", resp = reusedlogicvalidation),
prior(lkj(2), class = "L")
),
family = bernoulli(),
data = as.data.frame(d.both_completed),
control = list(adapt_delta = 0.95),
file = "fits/reuse1",
file_refit = "on_change",
seed = 20210421
)
```
#### Summary
```{r reuse1-sum}
summary(reuse1)
```
#### Random effects
```{r reuse1-raneff}
ranef(reuse1)
```
#### Sampling plots
```{r reuse1-plot}
plot(reuse1, ask = FALSE)
```
#### Posterior predictive check
##### Constructor reuse
```{r reuse1-pp-constructor}
pp_check(reuse1, type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
##### Validation reuse
```{r reuse1-pp-validation}
pp_check(reuse1, type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
### Reuse2 {.tabset}
```{r reuse2, class.source = 'fold-show', warning=FALSE, message=FALSE}
reuse2 <- brm(
"mvbind(
reused_logic_validation,
reused_logic_constructor
) ~ 1 + high_debt_version + scenario + workplace_coding_standards + (1 |c| session)",
prior = c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 1), class = "Intercept"),
prior(exponential(1), class = "sd", resp = reusedlogicconstructor),
prior(exponential(1), class = "sd", resp = reusedlogicvalidation),
prior(lkj(2), class = "L")
),
family = bernoulli(),
data = as.data.frame(d.both_completed),
control = list(adapt_delta = 0.95),
file = "fits/reuse2",
file_refit = "on_change",
seed = 20210421
)
```
#### Summary
```{r reuse2-sum}
summary(reuse2)
```
#### Random effects
```{r reuse2-raneff}
ranef(reuse2)
```
#### Sampling plots
```{r reuse2-plot}
plot(reuse2, ask = FALSE)
```
#### Posterior predictive check
##### Constructor reuse
```{r reuse2-pp-constructor}
pp_check(reuse2, type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
##### Validation reuse
```{r reuse2-pp-validation}
pp_check(reuse2, type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
## Final model
All candidate models look nice, none is significantly better than the others, we will proceed the simplest model: `reuse0`
### Variations {.tabset}
We will try a few different variations of the selected candidate model.
#### All data points {.tabset}
Some participants did only complete one scenario. Those has been excluded from the initial dataset to improve sampling of the models. We do however want to use all data we can and will therefore try to fit the model with the complete dataset.
```{r variation.all, class.source = 'fold-show', warning=FALSE, message=FALSE}
reuse0.all <- brm(
"mvbind(
reused_logic_validation,
reused_logic_constructor
) ~ 1 + high_debt_version + (1 |c| session)",
prior = c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 1), class = "Intercept"),
prior(exponential(1), class = "sd", resp = reusedlogicconstructor),
prior(exponential(1), class = "sd", resp = reusedlogicvalidation),
prior(lkj(2), class = "L")
),
family = bernoulli(),
data = as.data.frame(d.completed),
control = list(adapt_delta = 0.95),
file = "fits/reuse0.all",
file_refit = "on_change",
seed = 20210421
)
```
##### Summary
```{r variation.all-sum, warning=FALSE}
summary(reuse0.all)
```
##### Random effects
```{r variation.all-raneff, warning=FALSE}
ranef(reuse0.all)
```
##### Sampling plots
```{r variation.all-plot, message=FALSE, warning=FALSE}
plot(reuse0.all, ask = FALSE)
```
##### Posterior predictive check
###### Constructor reuse
```{r variation.all-pp-constructor, warning=FALSE, message=FALSE}
pp_check(reuse0.all, type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
###### Validation reuse
```{r variation.all-pp-validation, warning=FALSE, message=FALSE}
pp_check(reuse0.all, type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
#### With experience predictor {.tabset}
As including all data points didn't harm the model we will create this variant with all data points as well.
This variation includes `work_experience_programming.s` predictors as it can give further insight into how experience play a factor in the effect we try to measure. This is especially important as our sampling shewed towards containing less experienced developer than the population at large.
```{r variation.all.exp, class.source = 'fold-show', message=FALSE, warning=FALSE}
reuse0.all.exp <- brm(
"mvbind(
reused_logic_validation,
reused_logic_constructor
) ~ 1 + high_debt_version + work_experience_programming.s + (1 |c| session)",
prior = c(
prior(normal(0, 1), class = "b"),
prior(normal(0, 1), class = "Intercept"),
prior(exponential(1), class = "sd", resp = reusedlogicconstructor),
prior(exponential(1), class = "sd", resp = reusedlogicvalidation),
prior(lkj(2), class = "L")
),
family = bernoulli(),
data = as.data.frame(d.completed),
control = list(adapt_delta = 0.95),
file = "fits/reuse0.all.exp",
file_refit = "on_change",
seed = 20210421
)
```
##### Summary
```{r variation.all.exp-sum}
summary(reuse0.all.exp)
```
##### Random effects
```{r variation.all.exp-raneff}
ranef(reuse0.all.exp)
```
##### Loo comparison
```{r variation.all.exp-loo, warning=FALSE}
loo(
reuse0.all,
reuse0.all.exp
)
```
##### Sampling plots
```{r variation.all.exp-plot}
plot(reuse0.all.exp, ask = FALSE)
```
##### Posterior predictive check
###### Constructor reuse
```{r variation.all.exp-pp-constructor, warning=FALSE, message=FALSE}
pp_check(reuse0.all.exp, type = "bars", nsamples = 200, resp = "reusedlogicconstructor")
```
###### Validation reuse
```{r variation.all.exp-pp-validation, warning=FALSE, message=FALSE}
pp_check(reuse0.all.exp, type = "bars", nsamples = 200, resp = "reusedlogicvalidation")
```
### Final model
* Fitting the model to all data point did not significantly damage the model and will be used as is a more fair representation of reality.
* Adding the experience predictors did not significantly damage the model and will be used as it provides useful insight.
This means that our final model, with all data points and experience predictors, is `time0.all.exp`
## Interpreting the model
To begin interpreting the model we look at how it's parameters were estimated. As our research is focused on how the outcome of the model is effected we will mainly analyze the $\beta$ parameters.
### $\beta$ parameters
#### Constructor reuse
```{r interpret-beta-plot-constructor, warning=FALSE, message=FALSE}
mcmc_areas(reuse0.all.exp, pars = c("b_reusedlogicconstructor_high_debt_versionfalse", "b_reusedlogicconstructor_work_experience_programming.s"), prob = 0.95) + scale_y_discrete() +
scale_y_discrete(labels=c("High debt version: false", "Professional programming experience")) +
ggtitle("Beta parameters densities in reuse constructor model", subtitle = "Shaded region marks 95% of the density. Line marks the median")
```
#### Validation reuse
```{r interpret-beta-plot-validation, warning=FALSE, message=FALSE}
mcmc_areas(reuse0.all.exp, pars = c("b_reusedlogicvalidation_high_debt_versionfalse", "b_reusedlogicvalidation_work_experience_programming.s"), prob = 0.95) + scale_y_discrete() +
scale_y_discrete(labels=c("High debt version: false", "Professional programming experience")) +
ggtitle("Beta parameters densities in reuse validation model", subtitle = "Shaded region marks 95% of the density. Line marks the median")
```
### Effects sizes
When we look at effect size we will look at both outcomes combined
```{r effect-size, fig.width=8}
scale_programming_experience <- function(x) {
(x - mean(d.completed$work_experience_programming))/ sd(d.completed$work_experience_programming)
}
unscale_programming_experience <- function(x) {
x * sd(d.completed$work_experience_programming) + mean(d.completed$work_experience_programming)
}
post_settings <- expand.grid(
high_debt_version = c("false", "true"),
session = NA,
work_experience_programming.s = sapply(c(0, 3, 10, 25, 40), scale_programming_experience)
)
post <- posterior_predict(reuse0.all.exp, newdata = post_settings) %>%
melt(value.name = "estimate", varnames = c("sample_number", "settings_id", "model")) %>%
left_join(
rowid_to_column(post_settings, var= "settings_id"),
by = "settings_id"
) %>%
mutate(work_experience_programming = unscale_programming_experience(work_experience_programming.s)) %>%
select(
estimate,
high_debt_version,
work_experience_programming,
model
)
post %>%
mutate_at("high_debt_version",
function(x) case_when(
x == "false" ~ "Low debt",
x == "true" ~ "High debt"
)) %>%
mutate_at("estimate",
function(x) case_when(
x == 0 ~ "Reused",
x == 1 ~ "Duplicated"
)) %>%
ggplot(aes(high_debt_version)) +
geom_bar(aes(fill = estimate), position = "fill") +
facet_grid(rows = vars(work_experience_programming)) +
scale_fill_manual("Legend", values = c("lightblue", "darkblue")) +
labs(title = "Reuse / programming experience") +
xlab("Debt version") +
ylab("Ratio of reuse")
```
We can then proceed to calculate some likelihoods of duplication:
```{r es-a, class.source = 'fold-show'}
d <- post
d.high <- d %>% filter(high_debt_version == "true") %>% pull(estimate)
d.low <- d %>% filter(high_debt_version == "false") %>% pull(estimate)
x <- sum(d.high) / sum(d.low)
x
```
Given all the simulated cases we find that developers are `r scales::label_percent()(x - 1)` more likely to duplicate code in our high debt scenario.
```{r es-10, class.source = 'fold-show'}
d <- post %>% filter(work_experience_programming == 10)
d.high <- d %>% filter(high_debt_version == "true") %>% pull(estimate)
d.low <- d %>% filter(high_debt_version == "false") %>% pull(estimate)
x <- sum(d.high) / sum(d.low)
x
```
Considering developers with 10 years of professional programming experience we find that they are `r scales::label_percent()(x - 1)` more likely to duplicate code in our high debt scenario.
```{r es-0, class.source = 'fold-show'}
d <- post %>% filter(work_experience_programming == 0)
d.high <- d %>% filter(high_debt_version == "true") %>% pull(estimate)
d.low <- d %>% filter(high_debt_version == "false") %>% pull(estimate)
x <- sum(d.high) / sum(d.low)
x
```
Considering developers with no professional programming experience we find that they are `r scales::label_percent()(x - 1)` more likely to duplicate code in our high debt scenario.
```{r es-25, class.source = 'fold-show'}
d <- post %>% filter(work_experience_programming == 25)
d.high <- d %>% filter(high_debt_version == "true") %>% pull(estimate)
d.low <- d %>% filter(high_debt_version == "false") %>% pull(estimate)
x <- sum(d.high) / sum(d.low)
x
```
Considering developers with 25 years of professional programming experience we find that they are `r scales::label_percent()(x - 1)` more likely to duplicate code in our high debt scenario.