Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated FCS Indicator Calculation in Python, R, Stata, and SPSS #183

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,5 +80,6 @@ rsconnect/
# CUSTOM
Indicators/Food-consumption-score/.Rhistory
*.xlsx
*.csv
*.sav
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#------------------------------------------------------------------------------#
# WFP APP Standardized Scripts
# Calculating Food Consumption Score Nutrition (FCSN) in R
#------------------------------------------------------------------------------#
# 1. Purpose:
# This script calculates the Food Consumption Score Nutrition (FCSN) by aggregating
# food consumption data for different nutrients. It assumes that the required variables
# are available in your dataset and correctly formatted for use in the calculation.
#
# 2. Assumptions:
# - The dataset contains standardized variables from Survey Designer.
# - The variables used for the FCSN calculation are properly named and cleaned.
# - The dataset is loaded into a data frame before executing the script.
#
# 3. Requirements:
# - Required variables in the dataset:
# - FCSDairy : Consumption of dairy (past 7 days)
# - FCSPulse : Consumption of pulse (past 7 days)
# - FCSNPrMeatF : Consumption of flesh meat (past 7 days)
# - FCSNPrMeatO : Consumption of organ meat (past 7 days)
# - FCSNPrFish : Consumption of fish/shellfish (past 7 days)
# - FCSNPrEggs : Consumption of eggs (past 7 days)
# - FCSNVegOrg : Consumption of orange vegetables (vitamin A-rich)
# - FCSNVegGre : Consumption of green leafy vegetables
# - FCSNFruiOrg : Consumption of orange fruits (vitamin A-rich)
#
# - Required Packages:
# - No specific packages needed.
#
# 4. R Version:
# - R 3.x or higher is recommended for compatibility with modern libraries.
#------------------------------------------------------------------------------#

# Function to compute key nutrient aggregates (vitamin A, protein, and iron)
compute_nutrient_aggregates <- function(df) {
# Compute aggregates of key micronutrient consumption (Vitamin A, Protein, Hem Iron)

# Parameters:
# - df: DataFrame, the dataset containing the FCS and FCSN variables

# Returns:
# - df: DataFrame, the dataset with calculated micronutrient aggregates

# Sum the columns and treat NA as 0 for vitamin A-rich foods
df$FGVitA <- rowSums(df[, c('FCSDairy', 'FCSNPrMeatO', 'FCSNPrEggs',
'FCSNVegOrg', 'FCSNVegGre', 'FCSNFruiOrg')], na.rm = TRUE)

# Sum the columns and treat NA as 0 for protein-rich foods
df$FGProtein <- rowSums(df[, c('FCSPulse', 'FCSDairy', 'FCSNPrMeatF',
'FCSNPrMeatO', 'FCSNPrFish', 'FCSNPrEggs')], na.rm = TRUE)

# Sum the columns and treat NA as 0 for hem iron-rich foods
df$FGHIron <- rowSums(df[, c('FCSNPrMeatF', 'FCSNPrMeatO', 'FCSNPrFish')], na.rm = TRUE)

return(df)
}

# Function to categorize nutrient consumption groups based on thresholds
categorize_nutrient_consumption <- function(df, vita_column = 'FGVitA', fgprotein_column = 'FGProtein', fghiron_column = 'FGHIron') {
# Categorize nutrient consumption into groups (Never, Sometimes, At least 7 times)

# Parameters:
# - df: DataFrame, the dataset containing the nutrient data
# - vita_column: str, the name of vitamin A-rich nutrient column
# - fgprotein_column: str, the name of the protein-rich nutrient column
# - fghiron_column: str, the name of the iron-rich nutrient column

# Returns:
# - df: DataFrame, the dataset with categorized nutrient groups

# Define categories for vitamin A-rich foods
df$FGVitACat <- cut(df[[vita_column]],
breaks = c(-Inf, 0, 6, Inf),
labels = c("Never consumed", "Consumed sometimes", "Consumed at least 7 times"),
right = TRUE)

# Define categories for protein-rich foods
df$FGProteinCat <- cut(df[[fgprotein_column]],
breaks = c(-Inf, 0, 6, Inf),
labels = c("Never consumed", "Consumed sometimes", "Consumed at least 7 times"),
right = TRUE)

# Define categories for iron-rich foods
df$FGHIronCat <- cut(df[[fghiron_column]],
breaks = c(-Inf, 0, 6, Inf),
labels = c("Never consumed", "Consumed sometimes", "Consumed at least 7 times"),
right = TRUE)

return(df)
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,77 @@
*------------------------------------------------------------------------------*
* WFP APP Standardized Scripts
* Calculating Food Consumption Score Nutrition (FCSN) in Stata
*------------------------------------------------------------------------------*
* 1. Purpose:
* This script calculates the Food Consumption Score Nutrition (FCSN) by aggregating
* food consumption data for different nutrients. It assumes that the required variables
* are available in your dataset and correctly formatted for use in the calculation.
*
* 2. Assumptions:
* - The dataset contains standardized variables from Survey Designer.
* - The variables used for the FCSN calculation are properly named and cleaned.
* - The dataset is loaded into Stata before executing the script.
*
* 3. Requirements:
* - Required variables in the dataset:
* - FCSDairy : Consumption of dairy (past 7 days)
* - FCSPulse : Consumption of pulse (past 7 days)
* - FCSNPrMeatF : Consumption of flesh meat (past 7 days)
* - FCSNPrMeatO : Consumption of organ meat (past 7 days)
* - FCSNPrFish : Consumption of fish/shellfish (past 7 days)
* - FCSNPrEggs : Consumption of eggs (past 7 days)
* - FCSNVegOrg : Consumption of orange vegetables (vitamin A-rich)
* - FCSNVegGre : Consumption of green leafy vegetables
* - FCSNFruiOrg : Consumption of orange fruits (vitamin A-rich)
*
* 4. Stata Version:
* - Stata 13 or higher is recommended for compatibility with modern syntax.
*------------------------------------------------------------------------------*

* WFP RAM Standardized Scripts
* Calculating Food Consumption Score - Nutrition (FCSN)
*------------------------------------------------------------------------------*
* Step 1: Compute Nutrient Aggregates (Vitamin A, Protein, and Hem Iron)
*------------------------------------------------------------------------------*

* -----------------------------------------------------------------------------*
*
* Load data
* ---------
import delim using "../GitHub/RAMResourcesScripts/Static/FCSN_Sample_Survey.csv", ///
clear case(preserve)

** check and recode missing values as 0
sum FCS*
sum FCSN*
recode FCS* (. = 0)

** assign variable labels
label var FCSNPrMeatF "Consumption in past 7 days: Flesh meat"
label var FCSNPrMeatO "Consumption in past 7 days: Organ meat"
label var FCSNPrFish "Consumption in past 7 days: Fish/shellfish"
label var FCSNPrEggs "Consumption in past 7 days: Eggs"
label var FCSNVegOrg "Consumption in past 7 days: Orange vegetables (vegetables rich in Vitamin A)"
label var FCSNVegGre "Consumption in past 7 days: Green leafy vegetables"
label var FCSNFruiOrg "Consumption in past 7 days: Orange fruits (Fruits rich in Vitamin A)"

** create aggregates of key micronutrient consumption of vitamin, iron and protein
gen FGVitA = FCSDairy + FCSNPrMeatO + FCSNPrEggs + FCSNVegOrg + FCSNVegGre + FCSNFruiOrg
gen FGProtein = FCSPulse + FCSDairy + FCSNPrMeatF + FCSNPrMeatO + FCSNPrFish + FCSNPrEggs
gen FGHIron = FCSNPrMeatF + FCSNPrMeatO + FCSNPrFish

label var FGVitA "Consumption of vitamin A-rich foods"
label var FGProtein "Consumption of protein-rich foods"
label var FGHIron "Consumption of hem iron-rich foods"

** recode into nutritious groups
gen FGVitACat = cond(FGVitA < 1,1,cond(FGVitA <= 6,2,3))
gen FGProteinCat = cond(FGProtein < 1,1,cond(FGProtein <= 6,2,3))
gen FGHIronCat = cond(FGHIron < 1,1,cond(FGHIron <= 6,2,3))

** define variables labels and properties for FGVitACat FGProteinCat FGHIronCat
label def FGN_l 1 "Never consumed" ///
2 "Consumed sometimes" ///
3 "Consumed at least 7 times"
label val FGVitACat FGProteinCat FGHIron_Cat FGHIronCat

label var FGVitACat "Consumption group of vitamin A-rich foods"
label var FGProteinCat "Consumption group of protein-rich foods"
label var FGHIronCat "Consumption group of heme iron-rich foods"

* End of scripts
* Use rowtotal() to sum the relevant columns and treat missing values as 0
* Compute Vitamin A-rich foods aggregate
gen FGVitA = rowtotal(FCSDairy FCSNPrMeatO FCSNPrEggs FCSNVegOrg FCSNVegGre FCSNFruiOrg)
label variable FGVitA "Consumption of vitamin A-rich foods"

* Compute Protein-rich foods aggregate
gen FGProtein = rowtotal(FCSPulse FCSDairy FCSNPrMeatF FCSNPrMeatO FCSNPrFish FCSNPrEggs)
label variable FGProtein "Consumption of protein-rich foods"

* Compute Hem Iron-rich foods aggregate
gen FGHIron = rowtotal(FCSNPrMeatF FCSNPrMeatO FCSNPrFish)
label variable FGHIron "Consumption of hem iron-rich foods"

*------------------------------------------------------------------------------*
* Step 2: Categorize Nutrient Consumption Groups Based on Thresholds
*------------------------------------------------------------------------------*

* Categorize consumption of Vitamin A-rich foods
gen FGVitACat = .
replace FGVitACat = 1 if FGVitA == 0
replace FGVitACat = 2 if FGVitA > 0 & FGVitA <= 6
replace FGVitACat = 3 if FGVitA > 6
label define FGVitACat_lbl 1 "Never consumed" 2 "Consumed sometimes" 3 "Consumed at least 7 times"
label values FGVitACat FGVitACat_lbl
label variable FGVitACat "Consumption group of vitamin A-rich foods"

* Categorize consumption of Protein-rich foods
gen FGProteinCat = .
replace FGProteinCat = 1 if FGProtein == 0
replace FGProteinCat = 2 if FGProtein > 0 & FGProtein <= 6
replace FGProteinCat = 3 if FGProtein > 6
label define FGProteinCat_lbl 1 "Never consumed" 2 "Consumed sometimes" 3 "Consumed at least 7 times"
label values FGProteinCat FGProteinCat_lbl
label variable FGProteinCat "Consumption group of protein-rich foods"

* Categorize consumption of Hem Iron-rich foods
gen FGHIronCat = .
replace FGHIronCat = 1 if FGHIron == 0
replace FGHIronCat = 2 if FGHIron > 0 & FGHIron <= 6
replace FGHIronCat = 3 if FGHIron > 6
label define FGHIronCat_lbl 1 "Never consumed" 2 "Consumed sometimes" 3 "Consumed at least 7 times"
label values FGHIronCat FGHIronCat_lbl
label variable FGHIronCat "Consumption group of hem iron-rich foods"
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#------------------------------------------------------------------------------#
# WFP APP Standardized Scripts
# Calculating Food Consumption Score Nutrition (FCSN) in Python
#------------------------------------------------------------------------------#
# 1. Purpose:
# This script calculates the Food Consumption Score Nutrition (FCSN) by aggregating
# food consumption data for different nutrients. It assumes that the required variables
# are available in your dataset and correctly formatted for use in the calculation.
#
# 2. Assumptions:
# - The dataset contains standardized variables from Survey Designer.
# - The variables used for the FCSN calculation are properly named and cleaned.
# - The dataset is loaded into a pandas DataFrame before executing the script.
#
# 3. Requirements:
# - Required variables in the dataset:
# - FCSDairy : Consumption of dairy (past 7 days)
# - FCSPulse : Consumption of pulse (past 7 days)
# - FCSNPrMeatF : Consumption of flesh meat (past 7 days)
# - FCSNPrMeatO : Consumption of organ meat (past 7 days)
# - FCSNPrFish : Consumption of fish/shellfish (past 7 days)
# - FCSNPrEggs : Consumption of eggs (past 7 days)
# - FCSNVegOrg : Consumption of orange vegetables (vitamin A-rich)
# - FCSNVegGre : Consumption of green leafy vegetables
# - FCSNFruiOrg : Consumption of orange fruits (vitamin A-rich)
#
# - Required Packages:
# - pandas (Install with `pip install pandas`)
#
# 4. Python Version:
# - Python 3.x is recommended for compatibility with the pandas package.
#------------------------------------------------------------------------------#

# Load necessary libraries
import pandas as pd

# Function to compute key nutrient aggregates (vitamin A, protein, and iron)
def compute_nutrient_aggregates(df):
"""
Compute aggregates of key micronutrient consumption (Vitamin A, Protein, Hem Iron).

Parameters:
- df: pandas.DataFrame, the dataset containing the FCS and FCSN variables

Returns:
- df: pandas.DataFrame, the dataset with calculated micronutrient aggregates
"""
# Sum the columns and treat NaN as 0 for vitamin A-rich foods
df['FGVitA'] = df[['FCSDairy', 'FCSNPrMeatO', 'FCSNPrEggs', 'FCSNVegOrg',
'FCSNVegGre', 'FCSNFruiOrg']].sum(axis=1, skipna=True)

# Sum the columns and treat NaN as 0 for protein-rich foods
df['FGProtein'] = df[['FCSPulse', 'FCSDairy', 'FCSNPrMeatF',
'FCSNPrMeatO', 'FCSNPrFish', 'FCSNPrEggs']].sum(axis=1, skipna=True)

# Sum the columns and treat NaN as 0 for hem iron-rich foods
df['FGHIron'] = df[['FCSNPrMeatF', 'FCSNPrMeatO', 'FCSNPrFish']].sum(axis=1, skipna=True)

return df

# Function to categorize nutrient consumption groups based on thresholds
def categorize_nutrient_consumption(df, vita_column='FGVitA', fgprotein_column='FGProtein', fghiron_column='FGHIron'):
"""
Categorize nutrient consumption into groups (Never, Sometimes, At least 7 times).

Parameters:
- df: pandas.DataFrame, the dataset containing the nutrient data
- vita_column: str, the name of vitamin A-rich nutrient column
- fgprotein_column: str, the name of the protein-rich nutrient column
- fghiron_column: str, the name of the iron-rich nutrient column

Returns:
- df: pandas.DataFrame, the dataset with categorized nutrient groups
"""
for column in [vita_column, fgprotein_column, fghiron_column]:
df[column + 'Cat'] = pd.cut(
df[column],
bins=[-float('inf'), 0, 6, 42],
labels=["Never consumed", "Consumed sometimes", "Consumed at least 7 times"],
right=True
)

return df

# Example usage of the functions:
df = pd.DataFrame({'FCSDairy': [5, 0, 1], 'FCSNPrMeatF': [1, 0, 1], 'FCSNPrMeatO': [0, 0, 1],
'FCSNPrFish': [5, 0, 0], 'FCSNPrEggs': [1, 0, 0], 'FCSNVegOrg': [0, 0, 1],
'FCSNVegGre': [5, 0, 1], 'FCSNFruiOrg': [1, 0, 1], 'FCSPulse': [1, 0, 0]})
Loading