@@ -351,34 +351,65 @@ test_that("epi_slide outputs list columns when desired, and unpacks unnamed comp
351
351
})
352
352
353
353
test_that(" epi_slide can use sequential data masking expressions including NULL" , {
354
- edf <- tibble :: tibble(
354
+ edf_A <- tibble :: tibble(
355
355
geo_value = 1 ,
356
356
time_value = 1 : 10 ,
357
357
value = 1 : 10
358
358
) %> %
359
359
as_epi_df(as_of = 12L )
360
360
361
- noisiness1 <- edf %> %
361
+ noisiness_A1 <- edf_A %> %
362
362
group_by(geo_value ) %> %
363
363
epi_slide(
364
364
before = 1L , after = 2L ,
365
- valid = length(.x $ value ) == 4L ,
366
- pred = mean(.x $ value [1 : 2 ]),
367
- noisiness = sqrt(sum((.x $ value [3 : 4 ] - pred )^ 2 )),
365
+ valid = nrow(.x ) == 4L , # not the best approach...
366
+ m = mean(.x $ value [1 : 2 ]),
367
+ noisiness = sqrt(mean((value [3 : 4 ] - m )^ 2 )),
368
+ m = NULL
369
+ ) %> %
370
+ ungroup() %> %
371
+ filter(valid ) %> %
372
+ select(- valid )
373
+
374
+ noisiness_A0 <- edf_A %> %
375
+ filter(
376
+ time_value > = min(time_value ) + 1L ,
377
+ time_value < = max(time_value ) - 2L
378
+ ) %> %
379
+ mutate(noisiness = sqrt((3 - 1.5 )^ 2 + (4 - 1.5 )^ 2 ) / sqrt(2 ))
380
+
381
+ expect_identical(noisiness_A1 , noisiness_A0 )
382
+
383
+ edf_B <- tibble :: tibble(
384
+ geo_value = 1 ,
385
+ time_value = 1 : 10 ,
386
+ value = rep(1 : 2 , 5L )
387
+ ) %> %
388
+ as_epi_df(as_of = 12L )
389
+
390
+ noisiness_B1 <- edf_B %> %
391
+ group_by(geo_value ) %> %
392
+ epi_slide(
393
+ before = 1L , after = 2L ,
394
+ valid = nrow(.x ) == 4L , # not the best approach...
395
+ model = list (lm(value ~ time_value , .x [1 : 2 , ])),
396
+ pred = list (predict(model [[1L ]], newdata = .x [3 : 4 , " time_value" ])),
397
+ model = NULL ,
398
+ noisiness = sqrt(mean((.data $ value [3 : 4 ] - .data $ pred [[1L ]])^ 2 )),
368
399
pred = NULL
369
400
) %> %
370
401
ungroup() %> %
371
402
filter(valid ) %> %
372
403
select(- valid )
373
404
374
- noisiness0 <- edf %> %
405
+ noisiness_B0 <- edf_B %> %
375
406
filter(
376
407
time_value > = min(time_value ) + 1L ,
377
408
time_value < = max(time_value ) - 2L
378
409
) %> %
379
- mutate(noisiness = sqrt((3 - 1.5 )^ 2 + (4 - 1.5 ) ^ 2 ))
410
+ mutate(noisiness = sqrt((1 - 3 )^ 2 + (2 - 4 ) ^ 2 ) / sqrt( 2 ))
380
411
381
- expect_identical( noisiness1 , noisiness0 )
412
+ expect_equal( noisiness_B1 , noisiness_B0 )
382
413
})
383
414
384
415
test_that(" epi_slide can use {nm} :=" , {
0 commit comments