diff --git a/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.do b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.do new file mode 100644 index 0000000..0b2b4d7 --- /dev/null +++ b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.do @@ -0,0 +1,60 @@ +*------------------------------------------------------------------------------ +* WFP Standardized Scripts +* Post-Harvest Loss Calculation (SAMS Indicator 30) +*------------------------------------------------------------------------------ + +* This script calculates the Post-Harvest Loss (PHL) based on SAMS Indicator 30 guidelines. +* Detailed guidelines can be found in the SAMS documentation. + +* This syntax is based on SPSS download version from MoDA. +* More details can be found in the background document at: +* https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + +*------------------------------------------------------------------------------ +* Load dataset +*------------------------------------------------------------------------------ + +import delimited using "~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_30_PHL_Sample_Survey/SAMS_module_Indicator30_submodule_RepeatSAMSPHL.csv", clear + +*------------------------------------------------------------------------------ +* Rename variables +*------------------------------------------------------------------------------ + +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommName_A PSAMSPHLCommName +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommName PSAMSPHLCommName_oth +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommClas PSAMSPHLCommClass +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntH_B PSAMSPHLCommQntHand +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntH_A PSAMSPHLCommQntHandUnit +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntH PSAMSPHLCommQntHandUnit_oth +rename SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntL PSAMSPHLCommQntLost +rename _parent_index PSAMSPHLFarmerNum + +*------------------------------------------------------------------------------ +* Define variable and value labels +*------------------------------------------------------------------------------ + +label var PSAMSPHLCommName "What is the name of commodity?" +label var PSAMSPHLCommClass "Which of the following groups does this commodity belong to?" +label var PSAMSPHLCommQntHand "What is the amount of this commodity initially stored?" +label var PSAMSPHLCommQntHandUnit "Enter unit of measure." +label var PSAMSPHLCommQntLost "Of the total quantity you stored how much was lost?" + +*------------------------------------------------------------------------------ +* Calculate % loss per row +*------------------------------------------------------------------------------ + +gen perc_loss = (PSAMSPHLCommQntLost / PSAMSPHLCommQntHand) * 100 + +*------------------------------------------------------------------------------ +* Average loss per farmer +*------------------------------------------------------------------------------ + +collapse (mean) perc_loss, by(PSAMSPHLFarmerNum) + +*------------------------------------------------------------------------------ +* Average across farmers +*------------------------------------------------------------------------------ + +summarize perc_loss + +* End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.py b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.py new file mode 100644 index 0000000..5411472 --- /dev/null +++ b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.py @@ -0,0 +1,48 @@ +#------------------------------------------------------------------------------ +# WFP Standardized Scripts +# Post-Harvest Loss Calculation (SAMS Indicator 30) +#------------------------------------------------------------------------------ + +# This script calculates the Post-Harvest Loss (PHL) based on SAMS Indicator 30 guidelines. +# Detailed guidelines can be found in the SAMS documentation. + +# This syntax is based on SPSS download version from MoDA. +# More details can be found in the background document at: +# https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + +import pandas as pd + +# add sample data +data = pd.read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_30_PHL_Sample_Survey/SAMS_module_Indicator30_submodule_RepeatSAMSPHL.csv") + +# rename to remove group names +data.rename(columns={ + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommName': 'PSAMSPHLCommName', + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommName_oth': 'PSAMSPHLCommName_oth', + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommClass': 'PSAMSPHLCommClass', + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHand': 'PSAMSPHLCommQntHand', + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHandUnit': 'PSAMSPHLCommQntHandUnit', + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHandUnit_oth': 'PSAMSPHLCommQntHandUnit_oth', + 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntLost': 'PSAMSPHLCommQntLost', + '_parent_index': 'PSAMSPHLFarmerNum' +}, inplace=True) + +# assign variable and value labels +data['PSAMSPHLCommName'].attrs['label'] = "What is the name of commodity?" +data['PSAMSPHLCommClass'].attrs['label'] = "Which of the following groups does this commodity belong to?" +data['PSAMSPHLCommQntHand'].attrs['label'] = "What is the amount of this commodity initially stored?" +data['PSAMSPHLCommQntHandUnit'].attrs['label'] = "Enter unit of measure." +data['PSAMSPHLCommQntLost'].attrs['label'] = "Of the total quantity you stored how much was lost?" + +# Calculate % loss per row +data['perc_loss'] = (data['PSAMSPHLCommQntLost'] / data['PSAMSPHLCommQntHand']) * 100 + +# Average loss per farmer +avglossperfarmer_table = data.groupby('PSAMSPHLFarmerNum')['perc_loss'].mean().reset_index() + +# Average across farmers +average_phl_loss = avglossperfarmer_table['perc_loss'].mean() + +print(f"Average Post-Harvest Loss per Farmer: {average_phl_loss:.2f}%") + +# End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.sps b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.sps index a3211bc..ceefb2c 100644 --- a/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.sps +++ b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.sps @@ -1,7 +1,18 @@ -* Encoding: UTF-8. +*------------------------------------------------------------------------------ +* WFP Standardized Scripts +* Post-Harvest Loss Calculation (SAMS Indicator 30) +*------------------------------------------------------------------------------ -*can only download repeat csv data as zip file from moda with group names - will update this code to remove group names -*rename to remove group names - because of the variable length SPSS changes name slightly +* This script calculates the Post-Harvest Loss (PHL) based on SAMS Indicator 30 guidelines. +* Detailed guidelines can be found in the SAMS documentation. + +* This syntax is based on SPSS download version from MoDA. +* More details can be found in the background document at: +* https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + +*------------------------------------------------------------------------------ +* Rename variables +*------------------------------------------------------------------------------ RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommName_A = PSAMSPHLCommName). RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommName = PSAMSPHLCommName_oth). @@ -9,53 +20,42 @@ RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommClas RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntH_B = PSAMSPHLCommQntHand). RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntH_A = PSAMSPHLCommQntHandUnit). RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntH = PSAMSPHLCommQntHandUnit_oth). -RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntL= PSAMSPHLCommQntLost ). - -*also rename the _parent_index variable to farmer number - +RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommQntL = PSAMSPHLCommQntLost). RENAME VARIABLES (@_parent_index = PSAMSPHLFarmerNum). -* define variable and value labels - -Variable labels PNutProgPartic_yn ‘Is participant enrolled in the ((insert name/description of program, to be adapted locally)) programme?’. +*------------------------------------------------------------------------------ +* Define variable and value labels +*------------------------------------------------------------------------------ +Variable labels PSAMSPHLCommName "What is the name of commodity?". +Variable labels PSAMSPHLCommClass "Which of the following groups does this commodity belong to?". +Variable labels PSAMSPHLCommQntHand "What is the amount of this commodity initially stored?". +Variable labels PSAMSPHLCommQntHandUnit "Enter unit of measure.". +Variable labels PSAMSPHLCommQntLost "Of the total quantity you stored how much was lost?". -Variable labels PSAMSPHLCommName "What is the name of commodity?". -Variable labels PSAMSPHLCommClass "Which of the following groups does this commodity belong to?". -Variable labels PSAMSPHLCommQntHand "What is the amount of this commodity initially stored?". -Variable labels PSAMSPHLCommQntHandUnit "Enter unit of measure.". -Variable labels PSAMSPHLCommQntLost "Of the total quantity you stored how much was lost?". +*------------------------------------------------------------------------------ +* Calculate % loss per row +*------------------------------------------------------------------------------ - -*Calculate % loss per row - -COMPUTE perc_loss=(PSAMSPHLCommQntLost / PSAMSPHLCommQntHand) * 100. +COMPUTE perc_loss = (PSAMSPHLCommQntLost / PSAMSPHLCommQntHand) * 100. EXECUTE. - -*Average loss per farmer +*------------------------------------------------------------------------------ +* Average loss per farmer +*------------------------------------------------------------------------------ DATASET DECLARE avglossperfarmer_table. AGGREGATE /OUTFILE='avglossperfarmer_table' /BREAK=PSAMSPHLFarmerNum - /perc_loss_mean=MEAN(perc_loss). + /perc_loss_mean = MEAN(perc_loss). - -*Average across farmers +*------------------------------------------------------------------------------ +* Average across farmers +*------------------------------------------------------------------------------ DATASET ACTIVATE avglossperfarmer_table. DESCRIPTIVES VARIABLES=perc_loss_mean /STATISTICS=MEAN. - - - - - - - - - - - +* End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL_tidyverse.R b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL_tidyverse.R index 6190d8c..8751f5f 100644 --- a/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL_tidyverse.R +++ b/Indicators/SAMS-CRF-30-PHL/SAMS30_PHL_tidyverse.R @@ -1,55 +1,60 @@ +#------------------------------------------------------------------------------ +# WFP Standardized Scripts +# Post-Harvest Loss Calculation (SAMS Indicator 30) +#------------------------------------------------------------------------------ + +# This script calculates the Post-Harvest Loss (PHL) based on SAMS Indicator 30 guidelines. +# Detailed guidelines can be found in the SAMS documentation. + +# This syntax is based on SPSS download version from MoDA. +# More details can be found in the background document at: +# https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + library(tidyverse) library(labelled) library(expss) -#add sample data +# add sample data data <- read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_30_PHL_Sample_Survey/SAMS_module_Indicator30_submodule_RepeatSAMSPHL.csv") -#can only download repeat csv data as zip file from moda with group names - will update this code to remove group names -#rename to remove group names -data <- data %>% rename(PSAMSPHLCommName = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommName') -data <- data %>% rename(PSAMSPHLCommName_oth = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommName_oth') -data <- data %>% rename(PSAMSPHLCommClass = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommClass') -data <- data %>% rename(PSAMSPHLCommQntHand = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHand') -data <- data %>% rename(PSAMSPHLCommQntHandUnit = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHandUnit') -data <- data %>% rename(PSAMSPHLCommQntHandUnit_oth = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHandUnit_oth') -data <- data %>% rename(PSAMSPHLCommQntLost = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntLost') -#also rename the _parent_index variable to farmer number -data <- data %>% rename(PSAMSPHLFarmerNum = '_parent_index') - -#assign variable and value labels -var_label(data$PSAMSPHLCommName) <- "What is the name of commodity?" -var_label(data$PSAMSPHLCommClass) <- "Which of the following groups does this commodity belong to?" -var_label(data$PSAMSPHLCommQntHand) <- "What is the amount of this commodity initially stored?" +# rename to remove group names +data <- data %>% rename(PSAMSPHLCommName = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommName', + PSAMSPHLCommName_oth = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommName_oth', + PSAMSPHLCommClass = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommClass', + PSAMSPHLCommQntHand = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHand', + PSAMSPHLCommQntHandUnit = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHandUnit', + PSAMSPHLCommQntHandUnit_oth = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntHandUnit_oth', + PSAMSPHLCommQntLost = 'SAMS_module/Indicator30_submodule/RepeatSAMSPHL/PSAMSPHLCommQntLost', + PSAMSPHLFarmerNum = '_parent_index') + +# assign variable and value labels +var_label(data$PSAMSPHLCommName) <- "What is the name of commodity?" +var_label(data$PSAMSPHLCommClass) <- "Which of the following groups does this commodity belong to?" +var_label(data$PSAMSPHLCommQntHand) <- "What is the amount of this commodity initially stored?" var_label(data$PSAMSPHLCommQntHandUnit) <- "Enter unit of measure." -var_label(data$PSAMSPHLCommQntLost) <- "Of the total quantity you stored how much was lost?" - -#Calculate % loss per row -data <- data %>% mutate(perc_loss = round((PSAMSPHLCommQntLost /(PSAMSPHLCommQntHand) * 100),1)) -#Average loss per farmer -avglossperfarmer_table <- data %>% group_by(PSAMSPHLFarmerNum) %>% summarise(avglossperfarmer = mean(perc_loss)) -#Average across farmers -average_phl_loss_table <- avglossperfarmer_table %>% summarise(average_phl_loss = mean(avglossperfarmer)) - - - - - +var_label(data$PSAMSPHLCommQntLost) <- "Of the total quantity you stored how much was lost?" +# Calculate % loss per row +data <- data %>% mutate(perc_loss = round((PSAMSPHLCommQntLost / PSAMSPHLCommQntHand) * 100, 1)) +# Average loss per farmer +avglossperfarmer_table <- data %>% group_by(PSAMSPHLFarmerNum) %>% summarise(avglossperfarmer = mean(perc_loss)) +# Average across farmers +average_phl_loss_table <- avglossperfarmer_table %>% summarise(average_phl_loss = mean(avglossperfarmer)) -#creates a table of the weighted percentage of NutProgPartic_yn by +#creates a table of the weighted percentage of nutprogpartic_yn by #creating a temporary variable to display value labels #and providing the option to use weights if needed - -NutProgPartic_yn_table_wide <- data %>% - drop_na(PNutProgPartic_yn) %>% - count(PNutProgPartic_yn_lab = as.character(PNutProgPartic_yn)) %>% # if weights are needed use instead the row below - #count(PNutProgPartic_yn_lab = as.character(PNutProgPartic_yn), wt = nameofweightvariable) - mutate(Percentage = 100 * n / sum(n)) %>% +nutprogpartic_yn_table_wide <- data %>% + drop_na(pnutprogpartic_yn) %>% + count(pnutprogpartic_yn_lab = as.character(pnutprogpartic_yn)) %>% # if weights are needed use instead the row below + #count(pnutprogpartic_yn_lab = as.character(pnutprogpartic_yn), wt = nameofweightvariable) + mutate(percentage = 100 * n / sum(n)) %>% ungroup() %>% select(-n) %>% - pivot_wider(names_from = PNutProgPartic_yn_lab, - values_from = Percentage, - values_fill = 0) \ No newline at end of file + pivot_wider(names_from = pnutprogpartic_yn_lab, + values_from = percentage, + values_fill = 0) + +# End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.do b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.do new file mode 100644 index 0000000..ff8a1f4 --- /dev/null +++ b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.do @@ -0,0 +1,71 @@ +*------------------------------------------------------------------------------ +* WFP Standardized Scripts +* Nutritious Crop Production Increase Calculation (SAMS Indicator 31) +*------------------------------------------------------------------------------ + +* This script calculates the proportion of farmers reporting an increase in +* nutritious crop production based on SAMS Indicator 31 guidelines. +* Detailed guidelines can be found in the SAMS documentation. + +* This syntax is based on SPSS download version from MoDA. +* More details can be found in the background document at: +* https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + +*------------------------------------------------------------------------------ +* Import Dataset 1 +*------------------------------------------------------------------------------ + +import delimited "C:\Users\b\Desktop\demo\SAMS31\data.csv", clear +save temp_data1, replace + +*------------------------------------------------------------------------------ +* Import Dataset 2 +*------------------------------------------------------------------------------ + +import delimited "C:\Users\b\Desktop\demo\SAMS31\SAMS_module_Indicator31_submodule_RepeatNutCrop.csv", clear +rename _parent_index index +save temp_data2, replace + +*------------------------------------------------------------------------------ +* Merge Datasets +*------------------------------------------------------------------------------ + +use temp_data1, clear +rename _index index +merge 1:1 index using temp_data2 + +*------------------------------------------------------------------------------ +* Rename Variables +*------------------------------------------------------------------------------ + +rename SAMS_moduleIndicator31_submoduleRepeatNutCropPSAMSNutCropName_A PSAMSNutCropName +rename SAMS_moduleIndicator31_submoduleRepeatNutCropPSAMSNutCropIncr PSAMSNutCropIncr +rename Demographic_moduleDemographicBasic_submoduleRespSex RespSex + +*------------------------------------------------------------------------------ +* Define Variable and Value Labels +*------------------------------------------------------------------------------ + +label variable PSAMSNutCropName "What is the name of crop?" +label variable PSAMSNutCropIncr "Did you produce more, less or the same amount of this nutritious crop in the last 12 months compared to the 12 months before that?" +label variable RespSex "Sex of the Respondent" + +label define PSAMSNutCropName_lbl 1 "Crop 1" 2 "Crop 2" 3 "Crop 3" 4 "Crop 4" 5 "Crop 5" 999 "Other" +label values PSAMSNutCropName PSAMSNutCropName_lbl + +label define PSAMSNutCropIncr_lbl 1 "More" 2 "Less" 3 "The same" 9999 "Not applicable" +label values PSAMSNutCropIncr PSAMSNutCropIncr_lbl + +label define RespSex_lbl 0 "Female" 1 "Male" +label values RespSex RespSex_lbl + +*------------------------------------------------------------------------------ +* Selecting Farmers that Grew "Crop 1" and Show Proportion Reporting Increase, Decrease, or Same Production +*------------------------------------------------------------------------------ + +keep if PSAMSNutCropName == 1 & PSAMSNutCropIncr != 9999 + +* Create table +tabulate RespSex PSAMSNutCropIncr, row nofreq + +* End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.py b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.py new file mode 100644 index 0000000..727b555 --- /dev/null +++ b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.py @@ -0,0 +1,82 @@ +#------------------------------------------------------------------------------ +# WFP Standardized Scripts +# Nutritious Crop Production Increase Calculation (SAMS Indicator 31) +#------------------------------------------------------------------------------ + +# This script calculates the proportion of farmers reporting an increase in +# nutritious crop production based on SAMS Indicator 31 guidelines. +# Detailed guidelines can be found in the SAMS documentation. + +# This syntax is based on SPSS download version from MoDA. +# More details can be found in the background document at: +# https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + +import pandas as pd + +# Add sample data +data = pd.read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_31_NUT_Sample_Survey/SAMS_module_Indicator31_submodule_RepeatNutCrop.csv") +data2 = pd.read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_31_NUT_Sample_Survey/data.csv") + +# Rename to remove group names +data = data.rename(columns={ + 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropName': 'PSAMSNutCropName', + 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropName_oth': 'PSAMSNutCropName_oth', + 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropQuant': 'PSAMSNutCropQuant', + 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropQuantUnit': 'PSAMSNutCropQuantUnit', + 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropIncr': 'PSAMSNutCropIncr', + '_parent_index': 'index' +}) + +data2 = data2.rename(columns={ + 'Demographic_module/DemographicBasic_submodule/RespSex': 'RespSex', + '_index': 'index' +}) + +# Assign variable and value labels +data2['RespSex'] = data2['RespSex'].astype('category') +data2['RespSex'].cat.rename_categories({ + 0: 'Female', + 1: 'Male' +}, inplace=True) + +data['PSAMSNutCropName'] = data['PSAMSNutCropName'].astype('category') +data['PSAMSNutCropName'].cat.rename_categories({ + 1: 'Crop 1', + 2: 'Crop 2', + 3: 'Crop 3', + 4: 'Crop 4', + 5: 'Crop 5', + 999: 'Other' +}, inplace=True) + +data['PSAMSNutCropIncr'] = data['PSAMSNutCropIncr'].astype('category') +data['PSAMSNutCropIncr'].cat.rename_categories({ + 1: 'More', + 2: 'Less', + 3: 'The same', + 9999: 'Not applicable' +}, inplace=True) + +# Join dataset "data" & "data2" +data = data.merge(data2, on="index") + +# Selecting farmers that grew "Crop 1" show proportion reporting an increase, decrease or the same amount of production as the year before +SAMS31_table_total_wide = data[data['PSAMSNutCropName'] == 'Crop 1'] +SAMS31_table_total_wide = SAMS31_table_total_wide[SAMS31_table_total_wide['PSAMSNutCropIncr'] != 'Not applicable'] +SAMS31_table_total_wide = SAMS31_table_total_wide.dropna(subset=['PSAMSNutCropIncr']) +SAMS31_table_total_wide = SAMS31_table_total_wide.groupby('PSAMSNutCropIncr').size().reset_index(name='counts') +SAMS31_table_total_wide['Percentage'] = 100 * SAMS31_table_total_wide['counts'] / SAMS31_table_total_wide['counts'].sum() +SAMS31_table_total_wide = SAMS31_table_total_wide.pivot(index=None, columns='PSAMSNutCropIncr', values='Percentage').fillna(0) + +SAMS31_table_bysex_wide = data[data['PSAMSNutCropName'] == 'Crop 1'] +SAMS31_table_bysex_wide = SAMS31_table_bysex_wide[SAMS31_table_bysex_wide['PSAMSNutCropIncr'] != 'Not applicable'] +SAMS31_table_bysex_wide = SAMS31_table_bysex_wide.dropna(subset=['PSAMSNutCropIncr']) +SAMS31_table_bysex_wide = SAMS31_table_bysex_wide.groupby(['RespSex', 'PSAMSNutCropIncr']).size().reset_index(name='counts') +SAMS31_table_bysex_wide['Percentage'] = 100 * SAMS31_table_bysex_wide['counts'] / SAMS31_table_bysex_wide.groupby('RespSex')['counts'].transform('sum') +SAMS31_table_bysex_wide = SAMS31_table_bysex_wide.pivot(index='RespSex', columns='PSAMSNutCropIncr', values='Percentage').fillna(0) + +# Print results +print(SAMS31_table_total_wide) +print(SAMS31_table_bysex_wide) + +# End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.sps b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.sps index 169e44b..95e826c 100644 --- a/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.sps +++ b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.sps @@ -1,6 +1,19 @@ -* Encoding: UTF-8. +*------------------------------------------------------------------------------ +* WFP Standardized Scripts +* Nutritious Crop Production Increase Calculation (SAMS Indicator 31) +*------------------------------------------------------------------------------ -* import dataset 1 +* This script calculates the proportion of farmers reporting an increase in +* nutritious crop production based on SAMS Indicator 31 guidelines. +* Detailed guidelines can be found in the SAMS documentation. + +* This syntax is based on SPSS download version from MoDA. +* More details can be found in the background document at: +* https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + +*------------------------------------------------------------------------------ +* Import Dataset 1 +*------------------------------------------------------------------------------ PRESERVE. SET DECIMAL DOT. @@ -24,7 +37,9 @@ CACHE. EXECUTE. DATASET NAME DataSet1 WINDOW=FRONT. -* import dataset 2 +*------------------------------------------------------------------------------ +* Import Dataset 2 +*------------------------------------------------------------------------------ GET DATA /TYPE=TXT /FILE="C:\Users\b\Desktop\demo\SAMS31\SAMS_module_Indicator31_submodule_RepeatNutCrop.csv" @@ -49,7 +64,9 @@ CACHE. EXECUTE. DATASET NAME DataSet2 WINDOW=FRONT. -*join two datasets +*------------------------------------------------------------------------------ +* Join Two Datasets +*------------------------------------------------------------------------------ DATASET ACTIVATE DataSet2. RENAME VARIABLES (@_parent_index = index). @@ -66,23 +83,31 @@ MATCH FILES /TABLE=* /BY index. EXECUTE. +*------------------------------------------------------------------------------ +* Rename Variables +*------------------------------------------------------------------------------ -*can only download repeat csv data as zip file from moda with group names - will update this code to remove group names -*rename to remove group names - because of the variable length SPSS changes name slightly +RENAME VARIABLES (SAMS_moduleIndicator31_submoduleRepeatNutCropPSAMSNutCropName_A = PSAMSNutCropName). +RENAME VARIABLES (SAMS_moduleIndicator31_submoduleRepeatNutCropPSAMSNutCropIncr = PSAMSNutCropIncr). +RENAME VARIABLES (Demographic_moduleDemographicBasic_submoduleRespSex = RespSex). -DATASET ACTIVATE DataSet1. +*------------------------------------------------------------------------------ +* Define Variable and Value Labels +*------------------------------------------------------------------------------ -RENAME VARIABLES (SAMS_moduleIndicator31_submoduleRepeatNutCropPSAMSNutCropName_A = PSAMSNutCropName). -RENAME VARIABLES (SAMS_moduleIndicator31_submoduleRepeatNutCropPSAMSNutCropIncr = PSAMSNutCropIncr). -RENAME VARIABLES (Demographic_moduleDemographicBasic_submoduleRespSex = RespSex). +Variable labels PSAMSNutCropName "What is the name of crop?". +Variable labels PSAMSNutCropIncr "Did you produce more, less or the same amount of this nutritious crop in the last 12 months compared to the 12 months before that?". +Variable labels RespSex "Sex of the Respondent". Value labels PSAMSNutCropName 1 'Crop 1' 2 'Crop 2' 3 'Crop 3' 4 'Crop 4' 5 'Crop 5' 999 'Other'. -Value labels PSAMSNutCropIncr 1 'More' 2 'Less ' 3 'The same ' 9999 'Not applicable'. +Value labels PSAMSNutCropIncr 1 'More' 2 'Less' 3 'The same' 9999 'Not applicable'. Value labels RespSex 0 'Female' 1 'Male'. -*#selecting farmers that grew "Crop 1" show proportion reporting an increase, decrease or the same amount of production as the year before +*------------------------------------------------------------------------------ +* Select Farmers that Grew "Crop 1" and Show Proportion Reporting Increase, Decrease, or Same Production +*------------------------------------------------------------------------------ -select if(PSAMSNutCropIncr ~= 9999 & PSAMSNutCropName = 1). +SELECT IF (PSAMSNutCropIncr ~= 9999 & PSAMSNutCropName = 1). CROSSTABS /TABLES=RespSex BY PSAMSNutCropIncr @@ -90,3 +115,4 @@ CROSSTABS /CELLS=ROW /COUNT ROUND CELL. +* End of Scripts \ No newline at end of file diff --git a/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT_tidyverse.R b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT_tidyverse.R index 006e3c4..cea50f0 100644 --- a/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT_tidyverse.R +++ b/Indicators/SAMS-CRF-31-NUT/SAMS31_NUT_tidyverse.R @@ -1,31 +1,48 @@ +#------------------------------------------------------------------------------ +# WFP Standardized Scripts +# Nutritious Crop Production Increase Calculation (SAMS Indicator 31) +#------------------------------------------------------------------------------ + +# This script calculates the proportion of farmers reporting an increase in +# nutritious crop production based on SAMS Indicator 31 guidelines. +# Detailed guidelines can be found in the SAMS documentation. + +# This syntax is based on SPSS download version from MoDA. +# More details can be found in the background document at: +# https://wfp.sharepoint.com/sites/CRF2022-2025/CRF%20Outcome%20indicators/Forms/AllItems.aspx + library(tidyverse) library(labelled) library(expss) library(janitor) -#add sample data -data <- read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_31_NUT_Sample_Survey/SAMS_module_Indicator31_submodule_RepeatNutCrop.csv") +# Add sample data +data <- read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_31_NUT_Sample_Survey/SAMS_module_Indicator31_submodule_RepeatNutCrop.csv") data2 <- read_csv("~/GitHub/RAMResourcesScripts/Static/SAMS_CRF_31_NUT_Sample_Survey/data.csv") -#can only download repeat csv data as zip file from moda with group names - will update this code to remove group names -#rename to remove group names -data <- data %>% rename(PSAMSNutCropName = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropName') -data <- data %>% rename(PSAMSNutCropName_oth = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropName_oth') -data <- data %>% rename(PSAMSNutCropQuant = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropQuant') -data <- data %>% rename(PSAMSNutCropQuantUnit = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropQuantUnit') -data <- data %>% rename(PSAMSNutCropIncr = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropIncr') -data <- data %>% rename(index = '_parent_index') +# Rename to remove group names +data <- data %>% + rename( + PSAMSNutCropName = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropName', + PSAMSNutCropName_oth = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropName_oth', + PSAMSNutCropQuant = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropQuant', + PSAMSNutCropQuantUnit = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropQuantUnit', + PSAMSNutCropIncr = 'SAMS_module/Indicator31_submodule/RepeatNutCrop/PSAMSNutCropIncr', + index = '_parent_index' + ) -data2 <- data2 %>% rename(RespSex = 'Demographic_module/DemographicBasic_submodule/RespSex') -data2 <- data2 %>% rename(index = '_index') +data2 <- data2 %>% + rename( + RespSex = 'Demographic_module/DemographicBasic_submodule/RespSex', + index = '_index' + ) -#assign variable and value labels -var_label(data2$RespSex) <- "Sex of the Respondent" -var_label(data$PSAMSNutCropName) <- "What is the name of crop?" +# Assign variable and value labels +var_label(data2$RespSex) <- "Sex of the Respondent" +var_label(data$PSAMSNutCropName) <- "What is the name of crop?" var_label(data$PSAMSNutCropQuant) <- "How much of this commodity did you produce in the last 12 months?" var_label(data$PSAMSNutCropQuantUnit) <- "Enter unit of quantity produced" -var_label(data$PSAMSNutCropIncr) <- "Did you produce more, less or the same amount of this nutritious crop in the last 12 months compared to the 12 months before that?" - +var_label(data$PSAMSNutCropIncr) <- "Did you produce more, less or the same amount of this nutritious crop in the last 12 months compared to the 12 months before that?" val_lab(data2$RespSex) = num_lab(" 0 Female @@ -46,28 +63,37 @@ val_lab(data$PSAMSNutCropIncr) = num_lab(" 9999 Not applicable ") -#join dataset "data" & "data2" +# Join dataset "data" & "data2" data <- data %>% left_join(data2, by = "index") -#selecting farmers that grew "Crop 1" show proportion reporting an increase, decrease or the same amount of production as the year before -SAMS31_table_total_wide <- data %>% filter(PSAMSNutCropName == 1) %>% filter(PSAMSNutCropIncr != 9999) %>% +# Selecting farmers that grew "Crop 1" show proportion reporting an increase, decrease or the same amount of production as the year before +SAMS31_table_total_wide <- data %>% + filter(PSAMSNutCropName == 1) %>% + filter(PSAMSNutCropIncr != 9999) %>% drop_na(PSAMSNutCropIncr) %>% - count(PSAMSNutCropIncr_lab = as.character(PSAMSNutCropIncr)) %>% # if weights are needed use instead the row below - mutate(Percentage = 100 * n / sum(n)) %>% - ungroup() %>% select(-n) %>% + count(PSAMSNutCropIncr_lab = as.character(PSAMSNutCropIncr)) %>% + mutate(Percentage = 100 * n / sum(n)) %>% + ungroup() %>% + select(-n) %>% pivot_wider(names_from = PSAMSNutCropIncr_lab, values_from = Percentage, - values_fill = 0) + values_fill = 0) -#selecting farmers that grew "Crop 1" show proportion reporting an increase, decrease or the same amount of production as the year before -SAMS31_table_bysex_wide <- data %>% filter(PSAMSNutCropName == 1) %>% filter(PSAMSNutCropIncr != 9999) %>% group_by(RespSex_lab = as.character(RespSex)) %>% +SAMS31_table_bysex_wide <- data %>% + filter(PSAMSNutCropName == 1) %>% + filter(PSAMSNutCropIncr != 9999) %>% + group_by(RespSex_lab = as.character(RespSex)) %>% drop_na(PSAMSNutCropIncr) %>% - count(PSAMSNutCropIncr_lab = as.character(PSAMSNutCropIncr)) %>% # if weights are needed use instead the row below + count(PSAMSNutCropIncr_lab = as.character(PSAMSNutCropIncr)) %>% mutate(Percentage = 100 * n / sum(n)) %>% - ungroup() %>% select(-n) %>% + ungroup() %>% + select(-n) %>% pivot_wider(names_from = PSAMSNutCropIncr_lab, values_from = Percentage, - values_fill = 0) - + values_fill = 0) +# Print results +print(SAMS31_table_total_wide) +print(SAMS31_table_bysex_wide) +# End of Scripts \ No newline at end of file