Every cut-off in the methods paper is scaled to one of eight age × sex groups, because average dietary energy requirement differs across them. So this variable is required by every metric function in the package, and it is the second thing to compute after checking your data.
grp <- eiod_age_sex_group(age = eiod_demo$age, sex = eiod_demo$sex)
table(factor(grp, 1:8, attr(grp, "labels")))
#>
#> Male 0-1.9 yr Male 2-4.9 yr Male 5-9.9 yr Male 10+ yr Female 0-1.9 yr
#> 6 17 25 257 9
#> Female 2-4.9 yr Female 5-9.9 yr Female 10+ yr
#> 11 22 253The result is an integer vector of 1–8 carrying a labels
attribute, so you can always recover what each number means.
Adding it to your data frame
d <- eiod_demo
d$group <- eiod_age_sex_group(d$age, d$sex)
head(d[, c("id", "age", "sex", "group")], 4)
#> id age sex group
#> 1 1 16.4 male 4
#> 2 2 33.0 male 4
#> 3 3 45.5 male 4
#> 4 4 16.9 male 4Which type should sex be?
A factor is the recommended input, and
eiod_demo$sex is one. The levels carry the meaning, so the
data documents itself, table() prints “male” and “female”
rather than 1 and 2, and reiod can resolve it without being
told anything further.
Character works identically, and is what read.csv()
gives you by default:
eiod_age_sex_group(age = c(1.5, 40), sex = c("M", "F"))
#> [1] 1 8
#> attr(,"labels")
#> [1] "Male 0-1.9 yr" "Male 2-4.9 yr" "Male 5-9.9 yr" "Male 10+ yr"
#> [5] "Female 0-1.9 yr" "Female 2-4.9 yr" "Female 5-9.9 yr" "Female 10+ yr"Numeric sex is deliberately refused unless you say which code means male:
eiod_age_sex_group(age = c(1.5, 40), sex = c(1, 0))
#> Error:
#> ! `sex` is numeric, so reiod cannot tell which code means male.
#> Observed codes: 0, 1
#> Survey data uses 1/2 and 0/1 about equally often, and guessing wrong would invert every result without any error.
#> Either state the codes:
#> eiod_age_sex_group(age, sex, male = 1, female = 2)
#> or convert once, which is the recommended route:
#> dat$sex <- eiod_as_sex_factor(dat$sex, male = 1, female = 2)This is not a missing feature. Survey data uses 1/2 and
0/1 about equally often, and the two are indistinguishable
from the values alone — both are just two distinct numbers. If
reiod guessed and guessed wrong, every result would be
silently inverted: no error, nothing visibly odd, every male counted as
female. Rather than carry that risk, it stops and asks.
Say the codes and it works:
eiod_age_sex_group(age = c(1.5, 40), sex = c(1, 0), male = 1, female = 0)
#> [1] 1 8
#> attr(,"labels")
#> [1] "Male 0-1.9 yr" "Male 2-4.9 yr" "Male 5-9.9 yr" "Male 10+ yr"
#> [5] "Female 0-1.9 yr" "Female 2-4.9 yr" "Female 5-9.9 yr" "Female 10+ yr"But the better habit is to convert once, near the top of your script, after which every later call is unambiguous:
sex_f <- eiod_as_sex_factor(c(1, 0, 1, 0), male = 1, female = 0)
sex_f
#> [1] male female male female
#> Levels: male femaleA note on the age boundaries
The paper prints the groups as “0-1.9”, “2-4.9”, “5-9.9” and “10+”.
reiod reads these as half-open intervals:
age < 2, 2 <= age < 5,
5 <= age < 10, age >= 10. So a child
aged 1.95 years is in the first group:
eiod_age_sex_group(c(1.9, 1.95, 1.999, 2.0), rep("male", 4))
#> [1] 1 1 1 2
#> attr(,"labels")
#> [1] "Male 0-1.9 yr" "Male 2-4.9 yr" "Male 5-9.9 yr" "Male 10+ yr"
#> [5] "Female 0-1.9 yr" "Female 2-4.9 yr" "Female 5-9.9 yr" "Female 10+ yr"Testing age <= 1.9 instead would leave everyone
between 1.9 and 2.0 unassigned, and they would then vanish from every
metric with no error raised.
Age must be in years, and may be fractional. If your
data holds age in months, divide by 12 first —
eiod_age_sex_group() cannot tell the difference, and a
sample recorded in months would place everyone over 10 months into the
adult group.
Checking it before you rely on it
eiod_check_demographics(eiod_demo$age, eiod_demo$sex)
#> reiod data quality report
#>
#> ERROR 0
#> WARNING 0
#> NOTE 2
#>
#> NOTE sex sex sex is a factor with levels "male", "female". This is the recommended
#> input type.
#> NOTE group coverage 15 respondent(s) under 2 years. Tables 3 and 4 apply to them; HESD and
#> HDLEI do not, and will be NA. Decide explicitly whether they belong in
#> your denominator.
#>
#> No blocking problems.See also
-
vignette("data-checks")— what to run before this -
vignette("table3-absolute")— the first metric that uses the group -
?eiod_age_sex_group,?eiod_as_sex_factor