@@ -351,34 +351,65 @@ test_that("epi_slide outputs list columns when desired, and unpacks unnamed comp
351351})
352352
353353test_that(" epi_slide can use sequential data masking expressions including NULL" , {
354- edf <- tibble :: tibble(
354+ edf_A <- tibble :: tibble(
355355 geo_value = 1 ,
356356 time_value = 1 : 10 ,
357357 value = 1 : 10
358358 ) %> %
359359 as_epi_df(as_of = 12L )
360360
361- noisiness1 <- edf %> %
361+ noisiness_A1 <- edf_A %> %
362362 group_by(geo_value ) %> %
363363 epi_slide(
364364 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 )),
368399 pred = NULL
369400 ) %> %
370401 ungroup() %> %
371402 filter(valid ) %> %
372403 select(- valid )
373404
374- noisiness0 <- edf %> %
405+ noisiness_B0 <- edf_B %> %
375406 filter(
376407 time_value > = min(time_value ) + 1L ,
377408 time_value < = max(time_value ) - 2L
378409 ) %> %
379- mutate(noisiness = sqrt((3 - 1.5 )^ 2 + (4 - 1.5 ) ^ 2 ))
410+ mutate(noisiness = sqrt((1 - 3 )^ 2 + (2 - 4 ) ^ 2 ) / sqrt( 2 ))
380411
381- expect_identical( noisiness1 , noisiness0 )
412+ expect_equal( noisiness_B1 , noisiness_B0 )
382413})
383414
384415test_that(" epi_slide can use {nm} :=" , {
0 commit comments