22#
33# ' Age ratio test
44# '
5- # ' Age Ratio Test is an age-related test of survey and data quality.
5+ # ' Age ratio test is an age-related test of survey and data quality. In this
6+ # ' test, the ratio of the number of children aged from 6 to 29 months to the
7+ # ' number of children aged from 30 to 59 months is calculated. This ratio is
8+ # ' then compared to an expected ratio (usually set at 0.85). The difference
9+ # ' of the observed ratio to the expected ratio is then compared statistically
10+ # ' using Chi-squared test.
611# '
7- # ' @param x Numeric vector (age)
8- # ' @param ratio Expected age ratio
12+ # ' @param x A vector for age. Should either be in whole months (integer) or in
13+ # ' calculated decimal months (numeric).
14+ # ' @param ratio Expected age ratio. Default is 0.85.
915# '
10- # ' @return A lit of class `"ageRatioTest"` with:
16+ # ' @returns A lit of class `"ageRatioTest"` with:
1117# '
1218# ' | **Variable** | **Description** |
1319# ' | :--- | :--- |
2026# ' | *p* | `p-value` for Chi-squared test |
2127# '
2228# ' @examples
23- # ' # Age-ratio test on survey dataset from Kabul, Afghanistan (dp.ex02)
29+ # ' # Age-ratio test on survey dataset from Kabul, Afghanistan (` dp.ex02` )
2430# ' # with an age ratio of 0.85
2531# ' svy <- dp.ex02
2632# ' ageRatioTest(svy$age, ratio = 0.85)
3440# ###############################################################################
3541
3642ageRatioTest <- function (x , ratio = 0.85 ) {
37- g <- recode(x , " 6:29=1; 30:59=2" )
43+ # # If x is numeric ----
44+ if (is.numeric(x )) x <- floor(x )
45+
46+ # # If x is integer ----
47+ if (is.integer(x )) x <- x
48+
49+ # # If x is not numeric or integer ----
50+ if (! is.numeric(x ) & ! is.integer(x ))
51+ stop(" Age should be of class integer or numeric. Try again." )
52+
53+ # # Calculate age ratio ----
54+ g <- ifelse(x < 30 , 1 , 2 )
3855 expectedP <- ratio / (ratio + 1 )
39- observedP <- sum(g == 1 )/ sum(table(g ))
56+ observedP <- sum(g == 1 , na.rm = TRUE )/ sum(table(g ))
4057 observedR <- observedP / (1 - observedP )
41- X2 <- prop.test(sum(g == 1 ), sum(table(g )), p = expectedP )
58+ X2 <- prop.test(sum(g == 1 , na.rm = TRUE ), sum(table(g )), p = expectedP )
4259 result <- list (expectedR = ratio ,
4360 expectedP = expectedP ,
4461 observedR = observedR ,
@@ -61,7 +78,7 @@ ageRatioTest <- function(x, ratio = 0.85) {
6178# ' @return Printed output of [ageRatioTest()] function
6279# '
6380# ' @examples
64- # ' # Print age-ratio test results for survey dataset from Kabul, Afghanistan (dp.ex02)
81+ # ' # Print age-ratio test results for survey dataset from Kabul, Afghanistan
6582# ' svy <- dp.ex02
6683# ' print(ageRatioTest(svy$age, ratio = 0.85))
6784# '
0 commit comments