---
title: "AFE"
author: "R Goto"
date: "2023-11-22"
output: html_document
---
## 2.1. Settings
```{r eval=FALSE}
library(tidyverse)
options(scipen = 10, digits=3) 
```
## 2.2. Dataset

2.2.1. Download the files from 'National Panel Survey 2014-2015, Wave 4'
https://microdata.worldbank.org/index.php/catalog/2862
(accessed 15 November 2023)

**hh_sec_v.csv** - anthropometric measurements for male below 15 yrs and females below 50 yrs of age
**hh_sec_b.csv** - sex, age and identification of the biological mothers
**npsy4.child.anthro.csv** - including age in months 


## 2.3. Estimating energy requirement for aged >2y
- Individual energy requirement was estimated according to their age, sex as well as the lifecycle and it is estimated using formula in Table 5.2 (page 37) in FAO/WHO/UNU (2004) 
- Mean weight by age was obtained from anthropometric measurements (hh_sec_v.csv) for male below 15 yrs and females below 50 yrs of age
- For other age/gender groups, their weight was obtained from the estimated height and BMI values in Tanzania in 2014 from NCD-RiSC (https://ncdrisc.org/)

2.3.1. calculate mean weight and height by age
```{r eval=FALSE}
dataV <- read_csv("hh_sec_v.csv")

dataV <- dataV %>% 
  rename(weight = hh_v05, height = hh_v06) %>% 
  select(y4_hhid, indidy4, weight, height)

dataB <- read_csv("hh_sec_b.csv")

dataB <- dataB %>% 
  rename(sex = hh_b02, age = hh_b04, eat7d = hh_b07, moid = hh_b15) %>% 
  select(y4_hhid, indidy4, sex, age, eat7d, moid)   # eat7d (to select individual who consumed food in the HHs in the last 7 days), moid (identify biological mothers of children below 2 yrs of age)

write_csv(dataB, "dataB.csv")

dataVB <- left_join(dataV, dataB, by= c('y4_hhid', 'indidy4'))

options(scipen = 10, digits=3) # set up the decimal places of the calculated values

meanwh <- dataVB %>% 
  group_by(sex, age) %>% 
  summarise(meanw = mean(weight, na.rm=TRUE),
            meanh = mean(height, na.rm=TRUE))

write_csv(meanwh, 'whbyage.csv')  
```

# 2.3.2. whbyage.csv was used to calculate energy requirement by age and sex - the calculation is shown in  energyreq_2yrs-above.xlsx
# AFE was calculate the mean energy requirement of females aged 18-29 yrs (1AFE = 2346kcal)
**afeabove2y.csv** - AFE calculated by age and sex was organised for individual above 2 yrs of age

## 4. energy requirement for lactating women - identify lactating women (i.e. the biological mother of children below 24 months of age)
2.4.1. identify biological mothers of children 0-23 months of age
```{r eval=FALSE}
dataB <- read_csv("dataB.csv")

dataB_lact <- dataB %>% 
  select(y4_hhid, indidy4, sex, age, moid, eat7d) %>% 
  filter(eat7d == 1) %>%     # select only women who ate foods in the HHs in the last 7 days
  mutate(lact_m = ifelse(age<=1, moid, NA))  # list up mother's y4_hhid in lact_m
```

2.4.2. make a list of lactating mothers with hhid and indid
```{r eval=FALSE}
df_lactm <- dataB_lact %>% 
  count(y4_hhid, lact_m) %>% 
  filter(lact_m<=32) %>%   # excluding mother living outside (#98)
  select(-n) %>% 
  mutate(lact=1) %>% 
  rename(indidy4=lact_m)  # rename lact_m to indidy4 before merging
```

## 2.5. energy requirement of children below 24 months
2.5.1. Use energy need from complementary foods in developing countries - see Table 10 (p 51), Brown et al., 1998
```{r eval=FALSE}
u2 <-  read_csv("npsy4.child.anthro.csv")

u2 <- u2 %>%
  mutate(kcalreq = case_when(
    age_months <= 2 ~ 0,   # only breast feeding - no food intake
    age_months >= 3 & age_months <= 5 ~ 76,  # energy from food is 76kcal per day for 3-5 months of age
    age_months >= 6 & age_months <= 8 ~ 269,  # 269kcal per day for 6-8 months of age
    age_months >= 9 & age_months <= 11 ~ 451,   # 451kcal per day for 9-11 months of age
    age_months >= 12 & age_months <= 23 ~ 746)) %>%  # 746kcal per day for 12-23 months of age
  filter(age_months<=23)  # select children below 24 months
```

2.5.2. AFE calculation for children below 24 months
```{r eval=FALSE}
afeu2 <- u2 %>% 
  mutate(afeu2 = kcalreq/2346) %>% # 1AFE = 2346kcal
  select(y4_hhid, indidy4, afeu2)

write_csv(afeu2, "afeu2.csv")
```

## 2.6. calculate total AFE in the HHs
**afebove2y.csv** - AFE by age and sex for individuals above 2 yrs of age were organised (in 'files')
**AFEbylact.csv** - AFE for lactating women (afe_1) was organised (in 'files')
**dataB_lact** - list of lactating women with selected those who ate in the HH in the last 7 days (see 2.4.1)
**df_lactm** - list of lactating women (see 2.4.2)
**afeu2** - AFE calculatd for children below 24 months (see 2.5.2)

2.6.1. open AFE file for abeve 2y of age and merge the values with individuals
```{r eval=FALSE}
df_afe <- read_csv("afeabove2y.csv")

dataB_afe <- left_join(dataB_lact, df_afe, by = c('sex', 'age'))  # merge with AFE except children below 24 months

eathh <- dataB_afe %>% 
  distinct(y4_hhid)
```

2.6.2. merge with the list of lactating women and merge with AFE value by age
```{r eval=FALSE}
dataB_afe2 <- left_join(dataB_afe, df_lactm, by = c('y4_hhid', 'indidy4'))  # merge with df_lactm (list of lactating mothers, see 4.2) and dataB_afe to identify lactating women with 1 in lact

afebylact <- read_csv("AFEbylact.csv") 

dataB_afe3 <- left_join(dataB_afe2, afebylact, by = c('lact', 'sex', 'age'))  # merge with AFE of lactating women by age
```

2.6.3. oepn AFE values for below 24 months and merge in the file
```{r eval=FALSE}
df_u2 <- read_csv("afeu2.csv")

dataB_afe4 <- left_join(dataB_afe3, df_u2, by = c('y4_hhid', 'indidy4'))  # merge with df_u2
```

2.6.4. replace AFE for lactating women (lact_1) and AFE for below 24 months (afeu2)
```{r eval=FALSE}
dataB_afe5 <- dataB_afe4 %>%
  mutate(afe = if_else(lact==1, afe_l, afe, missing=afe))

dataB_afe6 <- dataB_afe5 %>%
  mutate(afe = if_else(afeu2>=0, afeu2, afe, missing=afe))

dataB_afe6 <-  dataB_afe6 %>% 
  select(y4_hhid, indidy4, afe)
```

2.6.5. Calculate total AFE in each HH
```{r eval=FALSE}
dataB_afe7 <- dataB_afe6 %>% 
  group_by(y4_hhid) %>% 
  summarise(afehh = sum(afe))

write_csv(dataB_afe7, "hhafe.csv")
```





