Skip to content

Commit 3654bc1

Browse files
committed
Reformat code; use Chairmarks for benchmarking.
1 parent a90a2b3 commit 3654bc1

7 files changed

+73
-45
lines changed

LDT_accuracy.qmd

+24-12
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,23 @@ and define some constants
2727
```{julia}
2828
#| code-fold: true
2929
#| output: false
30-
@isdefined(contrasts) || const contrasts = Dict{Symbol, Any}()
30+
@isdefined(contrasts) || const contrasts = Dict{Symbol,Any}()
3131
@isdefined(progress) || const progress = false
3232
```
3333

3434
## Create the dataset
3535

36-
3736
```{julia}
3837
#| output: false
3938
trials = innerjoin(
40-
DataFrame(dataset(:ELP_ldt_trial)),
41-
select(DataFrame(dataset(:ELP_ldt_item)), :item, :isword, :wrdlen),
42-
on = :item
39+
DataFrame(dataset(:ELP_ldt_trial)),
40+
select(
41+
DataFrame(dataset(:ELP_ldt_item)),
42+
:item,
43+
:isword,
44+
:wrdlen,
45+
),
46+
on=:item,
4347
)
4448
```
4549

@@ -54,20 +58,28 @@ This takes about ten to fifteen minutes on a recent laptop
5458
```{julia}
5559
contrasts[:isword] = EffectsCoding()
5660
contrasts[:wrdlen] = Center(8)
57-
@time gm1 = let f = @formula(acc ~ 1 + isword * wrdlen + (1|item) + (1|subj))
58-
fit(MixedModel, f, trials, Bernoulli(); contrasts, progress, init_from_lmm=(:β, :θ))
59-
end
61+
@time gm1 =
62+
let f =
63+
@formula(acc ~ 1 + isword * wrdlen + (1 | item) + (1 | subj))
64+
fit(
65+
MixedModel,
66+
f,
67+
trials,
68+
Bernoulli();
69+
contrasts,
70+
progress,
71+
init_from_lmm=(:β, :θ),
72+
)
73+
end
6074
```
6175

62-
6376
```{julia}
6477
print(gm1)
6578
```
6679

67-
6880
```{julia}
6981
#| fig-cap: Conditional modes and 95% prediction intervals on random effects for subject in model gm1
7082
#| label: fig-gm1condmodesubj
7183
#| code-fold: true
72-
qqcaterpillar!(Figure(; size=(800,800)), gm1, :subj)
73-
```
84+
qqcaterpillar!(Figure(; size=(800, 800)), gm1, :subj)
85+
```

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ version = "0.1.0"
66
[deps]
77
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
88
Arrow = "69666777-d1a9-59fb-9406-91d4454c9d45"
9-
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
109
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
1110
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
1211
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
1312
Chain = "8be319e6-bccf-4806-a6f7-6fae938471bc"
13+
Chairmarks = "0ca39b1e-fe0b-4e98-acfc-b1656634c4de"
1414
DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
1515
DataFrameMacros = "75880514-38bc-4a95-a458-c2aea5a3a702"
1616
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"

aGHQ.qmd

+10-9
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ Load the packages to be used
136136
#| output: false
137137
#| label: packagesA03
138138
using AlgebraOfGraphics
139-
using BenchmarkTools
140139
using CairoMakie
140+
using Chairmarks # a more modern BenchmarkTools
141141
using DataFrames
142142
using EmbraceUncertainty: dataset
143143
using FreqTables
@@ -404,8 +404,7 @@ Each evaluation of the deviance is fast, requiring only a fraction of a millisec
404404
405405
```{julia}
406406
βopt = copy(com05fe.β)
407-
@benchmark deviance(setβ!(m, β)) seconds = 1 setup =
408-
(m = com05fe; β = βopt)
407+
@b deviance(setβ!($com05fe, $βopt))
409408
```
410409
411410
but the already large number of evaluations for these six coefficients would not scale well as this dimension increases.
@@ -637,7 +636,7 @@ The IRLS algorithm has converged in 4 iterations to essentially the same devianc
637636
Each iteration of the IRLS algorithm takes more time than a deviance evaluation, but still only a fraction of a millisecond on a laptop computer.
638637
639638
```{julia}
640-
@benchmark deviance(updateβ!(m)) seconds = 1 setup = (m = com05fe)
639+
@b deviance(updateβ!($com05fe))
641640
```
642641
643642
## GLMMs and the PIRLS algorithm {#sec-PIRLS}
@@ -868,7 +867,7 @@ pirls!(m; verbose=true);
868867
As with IRLS, PIRLS is a fast and stable algorithm for determining the mode of the conditional distribution $(\mcU|\mcY=\bby)$ with $\bbtheta$ and $\bbbeta$ held fixed.
869868
870869
```{julia}
871-
@benchmark pirls!(mm) seconds = 1 setup = (mm = m)
870+
@b pirls!($m)
872871
```
873872
874873
The time taken for the four iterations to determine the conditional mode of $\bbu$ is comparable to the time taken for a single call to `updateβ!`.
@@ -1131,23 +1130,25 @@ The weights and positions for the 9th order rule are shown in @fig-ghnine.
11311130
#| label: fig-ghnine
11321131
df9 = DataFrame(gausshermitenorm(9))
11331132
ggplot(df9, aes(; x=:abscissae, y=:weights)) +
1134-
geom_point() + labs(; x="Positions", y="Weights")
1133+
geom_point() +
1134+
labs(; x="Positions", y="Weights")
11351135
# draw(
11361136
# data(gausshermitenorm(9)) *
11371137
# mapping(:abscissae => "Positions", :weights);
11381138
# figure=(; size=(600,450)),
11391139
# )
11401140
```
11411141
1142-
Notice that the magnitudes of the weights drop quite dramatically away from zero, even on a logarithmic scale (@fig-ghninelog)
1142+
Notice that the magnitudes of the weights drop quite dramatically as the position moves away from zero, even on a logarithmic scale (@fig-ghninelog)
11431143
11441144
```{julia}
11451145
#| code-fold: true
11461146
#| fig-cap: Weights (logarithm base 2) and positions for the 9th order normalized Gauss-Hermite quadrature rule
11471147
#| label: fig-ghninelog
11481148
ggplot(df9, aes(; x=:abscissae, y=:weights)) +
1149-
geom_point() + labs(; x="Positions", y="Weights") +
1150-
scale_y_log2()
1149+
geom_point() +
1150+
labs(; x="Positions", y="Weights") +
1151+
scale_y_log10()
11511152
# draw(
11521153
# data(gausshermitenorm(9)) * mapping(
11531154
# :abscissae => "Positions",

glmmbernoulli.qmd

+8-6
Original file line numberDiff line numberDiff line change
@@ -149,11 +149,12 @@ contrasts[:urban] = HelmertCoding()
149149
com01 =
150150
let d = contra,
151151
ds = Bernoulli(),
152-
f = @formula(use ~
153-
1 + livch + (age + abs2(age)) * urban + (1 | dist))
152+
f = @formula(
153+
use ~ 1 + livch + (age + abs2(age)) * urban + (1 | dist)
154+
)
154155
155-
fit(MixedModel, f, d, ds; contrasts, nAGQ, progress)
156-
end
156+
fit(MixedModel, f, d, ds; contrasts, nAGQ, progress)
157+
end
157158
```
158159
159160
Notice that in the formula language defined by the [StatsModels](https://github.com/JuliaStats/StatsModels.jl) package, an interaction term is written with the `&` operator.
@@ -284,8 +285,9 @@ A series of such model fits led to a model with random effects for the combinati
284285
285286
```{julia}
286287
com05 =
287-
let f = @formula(use ~
288-
1 + urban + ch * age + abs2(age) + (1 | dist & urban)),
288+
let f = @formula(
289+
use ~ 1 + urban + ch * age + abs2(age) + (1 | dist & urban)
290+
),
289291
d = contra,
290292
ds = Bernoulli()
291293

intro.qmd

+3-3
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,10 @@ You can check the details by clicking on the "Code" button in the HTML version o
635635
#| warning: false
636636
ggplot(
637637
filter(==("β") ∘ getproperty(:type), dsm01pars),
638-
aes(x = :value),
639-
) +
638+
aes(; x=:value),
639+
) +
640640
geom_density() +
641-
labs(x = "Bootstrap samples of β₁")
641+
labs(; x="Bootstrap samples of β₁")
642642
```
643643
644644
The distribution of the estimates of `β₁` is more-or-less a Gaussian (or "normal") shape, with a mean value of `{julia} repr(mean(βdf.value), context=:compact=>true)` which is close to the estimated `β₁` of `{julia} repr(only(dsm01.β), context=:compact=>true)`.

largescaledesigned.qmd

+9-6
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ A bar plot of the word length counts, @fig-ldtwrdlenhist, shows that the majorit
156156
#| code-fold: true
157157
#| fig-cap: "Bar plot of word lengths in the items used in the lexical decision task."
158158
#| label: fig-ldtwrdlenhist
159-
ggplot(ldttrial, aes(; x=:wrdlen)) + geom_bar() + labs(; x="Word length")
159+
ggplot(ldttrial, aes(; x=:wrdlen)) +
160+
geom_bar() +
161+
labs(; x="Word length")
160162
# #| warning: false
161163
# let wlen = 1:21
162164
# draw(
@@ -281,7 +283,8 @@ A plot of the median response time versus proportion accurate, @fig-ldtmedianrtv
281283
#| fig-cap: "Median response time versus proportion accurate by subject in the LDT."
282284
#| label: fig-ldtmedianrtvspropacc
283285
ggplot(bysubj, aes(; x=:spropacc, y=:smedianrt)) +
284-
geom_point() + geom_smooth(; method="smooth") +
286+
geom_point() +
287+
geom_smooth(; method="smooth") +
285288
labs(; x="Proportion accurate", y="Median response time (ms)")
286289
```
287290

@@ -338,8 +341,9 @@ A density plot of the pruned response times, @fig-elpldtrtdens, shows they are s
338341
#| code-fold: true
339342
#| fig-cap: Kernel density plot of the pruned response times (ms.) in the LDT.
340343
#| label: fig-elpldtrtdens
341-
ggplot(pruned, aes(; x=:rt)) + geom_density() +
342-
labs(; x = "Response time (ms.) for correct responses")
344+
ggplot(pruned, aes(; x=:rt)) +
345+
geom_density() +
346+
labs(; x="Response time (ms.) for correct responses")
343347
# draw(
344348
# data(pruned) *
345349
# mapping(:rt => "Response time (ms.) for correct responses") *
@@ -540,8 +544,7 @@ condmeans = leftjoin!(
540544
on=:item,
541545
)
542546
draw(
543-
data(condmeans) *
544-
mapping(
547+
data(condmeans) * mapping(
545548
:elm01 => "Conditional means of item random effects for model elm01",
546549
:elm02 => "Conditional means of item random effects for model elm02";
547550
color=:isword,

multiple.qmd

+18-8
Original file line numberDiff line numberDiff line change
@@ -113,23 +113,30 @@ of the data, then plot it
113113
#| fig-cap: "Diameter of inhibition zone by plate and sample. Plates are ordered by increasing mean response."
114114
#| label: fig-penicillindot
115115
let sumry = sort!(
116-
combine(groupby(penicillin, :plate), :diameter => mean => :meandia),
116+
combine(
117+
groupby(penicillin, :plate),
118+
:diameter => mean => :meandia,
119+
),
117120
:meandia,
118121
),
119122
df = sort(
120-
transform(penicillin, :plate => ByRow(sorter(sumry.plate)); renamecols=false),
121-
:plate
123+
transform(
124+
penicillin,
125+
:plate => ByRow(sorter(sumry.plate));
126+
renamecols=false,
127+
),
128+
:plate,
122129
)
130+
123131
mp = mapping(
124132
:diameter => "Diameter of inhibition zone [mm]",
125133
:plate => "Plate",
126134
color=:sample,
127135
)
128136
129137
draw(
130-
data(df) * mp *
131-
visual(ScatterLines; marker='○', markersize=12);
132-
figure=(; size=(600, 450))
138+
data(df) * mp * visual(ScatterLines; marker='○', markersize=12);
139+
figure=(; size=(600, 450)),
133140
)
134141
end
135142
```
@@ -249,7 +256,8 @@ As for model `dsm01` the bootstrap parameter estimates of the fixed-effects para
249256
#| label: fig-pnm01bsbeta
250257
#| warning: false
251258
ggplot(DataFrame(pnm01samp.tbl), aes(x=:β1)) +
252-
geom_density() + labs(x="Bootstrap samples of β₁")
259+
geom_density() +
260+
labs(; x="Bootstrap samples of β₁")
253261
```
254262

255263
and the shortest coverage interval on this parameter is close to the Wald interval
@@ -557,7 +565,9 @@ Although the response, `y`, is on a scale of 1 to 5,
557565
#| fig-cap: "Histogram of instructor ratings in the *insteval* data"
558566
#| label: fig-instevalhist
559567
#| warning: false
560-
ggplot(DataFrame(insteval), aes(x=:y)) + geom_bar() + labs(x="Rating")
568+
ggplot(DataFrame(insteval), aes(x=:y)) +
569+
geom_bar() +
570+
labs(; x="Rating")
561571
```
562572

563573
it is sufficiently diffuse to warrant treating it as if it were a continuous response.

0 commit comments

Comments
 (0)