@@ -360,3 +360,60 @@ test_that("tidy prob_dist critical region follows the alternative direction", {
360360 ts <- tidy(exp_one_sample_prop_test(df , outcome , p = 0.4 , alternative = " two.sided" , method = " approximate" )$ model [[1 ]], type = " prob_dist" )
361361 expect_true(any(ts $ x [which(ts $ critical )] > 0 ) && any(ts $ x [which(ts $ critical )] < 0 ))
362362})
363+
364+ # --- tidy(type = "prob_dist_prop") ---
365+
366+ test_that(" tidy prob_dist_prop marks the observed proportion on the proportion scale" , {
367+ df <- data.frame (outcome = c(rep(TRUE , 50 ), rep(FALSE , 50 )))
368+ model <- exp_one_sample_prop_test(df , outcome , p = 0.4 , method = " approximate" )$ model [[1 ]]
369+ pd <- tidy(model , type = " prob_dist_prop" )
370+ expect_true(all(c(" x" , " y" , " statistic" , " critical" , " p.value" ) %in% names(pd )))
371+ # SE = sqrt(p0*(1-p0)/n); the marked point sits at the observed proportion (NOT z).
372+ se <- sqrt(0.4 * 0.6 / 100 )
373+ marked <- pd [which(pd $ statistic ), ]
374+ expect_equal(nrow(marked ), 1 )
375+ expect_equal(marked $ x , 0.5 )
376+ expect_equal(marked $ p.value , model $ htest $ p.value )
377+ # Normal centered at the benchmark proportion p0 with sd = SE.
378+ expect_equal(unique(pd $ mean ), 0.4 )
379+ expect_equal(unique(pd $ sd ), se )
380+ expect_true(max(pd $ y , na.rm = TRUE ) < = dnorm(0.4 , mean = 0.4 , sd = se ) + 1e-9 )
381+ })
382+
383+ test_that(" tidy prob_dist_prop always uses the proportion-scale normal even for the exact method" , {
384+ df <- data.frame (outcome = c(rep(TRUE , 12 ), rep(FALSE , 88 )))
385+ model <- exp_one_sample_prop_test(df , outcome , p = 0.1 , method = " exact" )$ model [[1 ]]
386+ expect_equal(model $ method_used , " Exact Binomial Test" )
387+ pd <- tidy(model , type = " prob_dist_prop" )
388+ se <- sqrt(0.1 * 0.9 / 100 )
389+ marked <- pd [which(pd $ statistic ), ]
390+ expect_equal(nrow(marked ), 1 )
391+ expect_equal(marked $ x , 0.12 )
392+ expect_equal(unique(pd $ mean ), 0.1 )
393+ expect_equal(unique(pd $ sd ), se )
394+ })
395+
396+ test_that(" tidy prob_dist_prop critical region follows the alternative direction" , {
397+ df <- data.frame (outcome = c(rep(TRUE , 50 ), rep(FALSE , 50 )))
398+ p0 <- 0.4
399+ # greater -> rejection region is the upper tail only (above the benchmark proportion).
400+ gt <- tidy(exp_one_sample_prop_test(df , outcome , p = p0 , alternative = " greater" , method = " approximate" )$ model [[1 ]], type = " prob_dist_prop" )
401+ expect_true(all(gt $ x [which(gt $ critical )] > p0 ))
402+ # less -> rejection region is the lower tail only.
403+ lt <- tidy(exp_one_sample_prop_test(df , outcome , p = p0 , alternative = " less" , method = " approximate" )$ model [[1 ]], type = " prob_dist_prop" )
404+ expect_true(all(lt $ x [which(lt $ critical )] < p0 ))
405+ # two.sided -> both tails relative to the benchmark proportion.
406+ ts <- tidy(exp_one_sample_prop_test(df , outcome , p = p0 , alternative = " two.sided" , method = " approximate" )$ model [[1 ]], type = " prob_dist_prop" )
407+ expect_true(any(ts $ x [which(ts $ critical )] > p0 ) && any(ts $ x [which(ts $ critical )] < p0 ))
408+ })
409+
410+ test_that(" tidy prob_dist_prop returns an empty tibble when the SE is 0/NA" , {
411+ # n*p0 produces a valid SE normally; build a degenerate model object directly to
412+ # exercise the guard (the benchmark SE is 0 only for a degenerate p0).
413+ model <- structure(
414+ list (htest = list (p.value = NA_real_ ), p = 0.4 , observed_prop = 0.5 ,
415+ se = 0 , sig.level = 0.05 , alternative = " two.sided" ),
416+ class = c(" one_sample_prop_test_exploratory" , " list" ))
417+ pd <- tidy(model , type = " prob_dist_prop" )
418+ expect_equal(nrow(pd ), 0 )
419+ })
0 commit comments