Skip to content

Commit 816983b

Browse files
committed
move code inside testhat, where possible
1 parent 56655d3 commit 816983b

File tree

1 file changed

+32
-27
lines changed

1 file changed

+32
-27
lines changed

tests/testthat/test-get_variance.R

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ suppressMessages({
2525
Reaction ~ Days + (1 | grp / subgrp) + (1 | Subject),
2626
data = study_data
2727
)
28-
fm6 <-
29-
lme4::lmer(diameter ~ 0 + sample + (1 | plate), data = Penicillin)
28+
fm6 <- lme4::lmer(diameter ~ 0 + sample + (1 | plate), data = Penicillin)
3029
})
3130

3231
v1 <- suppressWarnings(get_variance(fm1))
@@ -108,10 +107,10 @@ test_that("get_variance-6", {
108107

109108
# further examples
110109

111-
model <- lme4::lmer(Reaction ~ Days + (1 + Days || Subject), data = sleepstudy)
112-
vmodel <- get_variance(model)
113-
114110
test_that("get_variance-7", {
111+
model <- lme4::lmer(Reaction ~ Days + (1 + Days || Subject), data = sleepstudy)
112+
vmodel <- get_variance(model)
113+
115114
expect_equal(
116115
vmodel,
117116
list(
@@ -127,10 +126,11 @@ test_that("get_variance-7", {
127126
)
128127
})
129128

130-
model <- lme4::lmer(Reaction ~ Days + (0 + Days || Subject), data = study_data)
131-
vmodel <- get_variance(model)
132129

133130
test_that("get_variance-8", {
131+
model <- lme4::lmer(Reaction ~ Days + (0 + Days || Subject), data = study_data)
132+
vmodel <- get_variance(model)
133+
134134
expect_equal(
135135
vmodel,
136136
list(
@@ -148,14 +148,14 @@ test_that("get_variance-8", {
148148

149149
# categorical rnd slope
150150

151-
data(sleepstudy, package = "lme4")
152-
sleepstudy$Days2 <- cut(sleepstudy$Days, breaks = c(-1, 3, 6, 10))
153-
study_data2 <<- sleepstudy
151+
test_that("get_variance-9", {
152+
data(sleepstudy, package = "lme4")
153+
sleepstudy$Days2 <- cut(sleepstudy$Days, breaks = c(-1, 3, 6, 10))
154+
study_data2 <<- sleepstudy
154155

155-
model <- lme4::lmer(Reaction ~ Days2 + (1 + Days2 | Subject), data = study_data2)
156-
vmodel <- get_variance(model)
156+
model <- lme4::lmer(Reaction ~ Days2 + (1 + Days2 | Subject), data = study_data2)
157+
vmodel <- get_variance(model)
157158

158-
test_that("get_variance-9", {
159159
expect_equal(
160160
vmodel,
161161
list(
@@ -180,10 +180,11 @@ test_that("get_variance-9", {
180180
)
181181
})
182182

183-
model <- suppressMessages(lme4::lmer(Reaction ~ Days2 + (1 + Days2 || Subject), data = study_data2))
184-
vmodel <- suppressWarnings(get_variance(model))
185183

186184
test_that("get_variance-10", {
185+
model <- suppressMessages(lme4::lmer(Reaction ~ Days2 + (1 + Days2 || Subject), data = study_data2))
186+
vmodel <- suppressWarnings(get_variance(model))
187+
187188
expect_equal(
188189
vmodel,
189190
list(
@@ -203,10 +204,11 @@ test_that("get_variance-10", {
203204
)
204205
})
205206

206-
model <- lme4::lmer(Reaction ~ Days2 + (0 + Days2 | Subject), data = study_data2)
207-
vmodel <- get_variance(model)
208207

209208
test_that("get_variance-11", {
209+
model <- lme4::lmer(Reaction ~ Days2 + (0 + Days2 | Subject), data = study_data2)
210+
vmodel <- get_variance(model)
211+
210212
expect_equal(
211213
vmodel,
212214
list(
@@ -230,10 +232,11 @@ test_that("get_variance-11", {
230232
)
231233
})
232234

233-
model <- lme4::lmer(Reaction ~ Days2 + (0 + Days2 || Subject), data = study_data2)
234-
vmodel <- get_variance(model)
235235

236236
test_that("get_variance-12", {
237+
model <- lme4::lmer(Reaction ~ Days2 + (0 + Days2 || Subject), data = study_data2)
238+
vmodel <- get_variance(model)
239+
237240
expect_equal(
238241
vmodel,
239242
list(
@@ -260,12 +263,13 @@ test_that("get_variance-12", {
260263

261264
# test random slope correlation for categorical random slope
262265

263-
data(cake, package = "lme4")
264-
suppressMessages({
265-
m <- lme4::lmer(angle ~ temperature + (temperature | recipe), data = cake)
266-
})
267266
test_that("get_variance-cat_random_slope", {
267+
data(cake, package = "lme4")
268+
suppressMessages({
269+
m <- lme4::lmer(angle ~ temperature + (temperature | recipe), data = cake)
270+
})
268271
vc <- suppressWarnings(get_variance(m))
272+
269273
expect_equal(
270274
vc$cor.slopes,
271275
c(
@@ -328,13 +332,14 @@ test_that("random effects CIs, simple slope", {
328332
)
329333
})
330334

331-
data(cake, package = "lme4")
332-
suppressMessages({
333-
m <- lme4::lmer(angle ~ poly(temp, 2) + (poly(temp, 2) | replicate) + (1 | recipe), data = cake)
334-
})
335335

336336
test_that("random effects CIs, poly slope", {
337+
data(cake, package = "lme4")
338+
suppressMessages({
339+
m <- lme4::lmer(angle ~ poly(temp, 2) + (poly(temp, 2) | replicate) + (1 | recipe), data = cake)
340+
})
337341
vc <- suppressWarnings(get_variance(m))
342+
338343
expect_equal(
339344
vc$cor.slopes,
340345
c(`replicate.poly(temp, 2)1-poly(temp, 2)2` = 0.940016422944175),

0 commit comments

Comments
 (0)