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

Indicator sams #179

Open
wants to merge 2 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
60 changes: 60 additions & 0 deletions Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.do
Original file line number Diff line number Diff line change
@@ -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
48 changes: 48 additions & 0 deletions Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.py
Original file line number Diff line number Diff line change
@@ -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
70 changes: 35 additions & 35 deletions Indicators/SAMS-CRF-30-PHL/SAMS30_PHL.sps
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
* 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).
RENAME VARIABLES (SAMS_moduleIndicator30_submoduleRepeatSAMSPHLPSAMSPHLCommClas = PSAMSPHLCommClass).
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
85 changes: 45 additions & 40 deletions Indicators/SAMS-CRF-30-PHL/SAMS30_PHL_tidyverse.R
Original file line number Diff line number Diff line change
@@ -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)
pivot_wider(names_from = pnutprogpartic_yn_lab,
values_from = percentage,
values_fill = 0)

# End of Scripts
71 changes: 71 additions & 0 deletions Indicators/SAMS-CRF-31-NUT/SAMS31_NUT.do
Original file line number Diff line number Diff line change
@@ -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
Loading