##1. Download and save your data:
##2. Set up your code file
Open RStudio and start a new r script by clicking the icon in the top left corner with the green plus sign over a white rectangle, then select “R Script”. This is where you will write your code.
Note that i'm writing these instructions in an "R Markdown" file, which is better if you want to write a lot of words that aren't code, but it's a little more confusing to use so just start with a script. The main difference to keep in mind is that in your R script, if you want to write words (not code) you need to start every of text (we refer to this text as "comments") with this symbol: #. It's a good idea to write comments around your code to keep track of what you're doing for when you come back to it later. I will give some examples in the box below - notice the comments are green, and they do not run as part of the code. Save your R script right away, by clicking the save icon in the top left side of the screen. Make sure you save the script INSIDE THE RLABS FOLDER (but not in the Data subfolder).
##3.Set your working directory
getwd() #run this line to check that you saved your code in the right folder. This is your working directory. I can tell you how to set your working directory to a different folder, but for now just make sure you saved this R script in the right place and you should be good to go
## [1] "/Users/violetlasdun/Documents/Tanzania Field Work/RLabs"
##4. Load Data
Bring your csv file into R by running the following code
##5. Plot the GPS coordinates on a map
#install.packages("leaflet") #recall, you only need to install the package the very first time you use it, then comment out this line
library(leaflet)
RLabs_Households <- leaflet() %>%
addTiles() %>%
addMarkers(data$longitude, data$latitude)
RLabs_Households
#here you're using the package leaflet to plot the geocoordinates from the dataframe ("data"). You call the latitude and longitude columns with the command data$latitude and data$longitude (remember we named these columns in the step above - in general use dataframe_name$variable_name to refer to a column in a dataframe). Dont worry too much about what the rest of the code is doing, but its a useful code chunk to have in case you ever want to plot gps points again. Note that it can take a few minutes for this to run
##6.Descriptive Statistics
We want to tell RLabs about the households they surveyed, basically we want to give a summary of the data they collected to paint a picture about who participated. There’s lots of ways we can this.
The first thing we want to do is decide which variables to include in the table, and think about if any of them need to be in a different form from the way they appear in the raw data
For example, we asked a bunch of questions that are part of the Food Insecurity Experience Scale (FIES - read about it here: https://www.fao.org/in-action/voices-of-the-hungry/fies/en/#:~:text=The%20FIES%20is%20a%20statistical,scale%2C%20not%20as%20separate%20items.) We dont want to present the answers to each question separately, instead we want to make a scale that adds up all the answers (the answers are coded yes = 1 and no = 0, so that the someone who answered "yes" to a lot of the questions would have a higher score when we add up the answers and make the scale, a higher number means more food insecurity, i.e. worse-off). #calculate the FIES score as the sum of the answers to fies1 through fies7 , and save it as a new variable in the dataframe, called fies_score
data$fies_score <- rowSums(data[, c("fies1", "fies2", "fies3", "fies4", "fies5", "fies6", "fies7")])
#notice after you run this code the number of variables in the dataframe (in the environment box) increased from 78 to 79
#note that you need the dplyr package for this, but you should have already used it earlier in this r script so you dont need to load the library again. In case you are using this part of the tutorial for something else, you need to load library(dplyr).
summary_vars <- select(data, village, gender, education, civilstatus, age, occupation, hhsize, fies_score, spend_spare_cash, spend_spare_cash_other)
#now you should see a new dataframe appear in the environment box, called summary_vars, with only 10 variables. Notice there are still the same number of observations because you kept the data on all the households in the sample, just not all the variables. Click on the dataframe to look at it
#Think about what you'd want to tell people about this data. A lot of the variables are categorical, meaning they tell you the category a person fits into, like occupation tells you which job they have. This type of variable doesn't really make sense in a numeric form like giving the average/mean. Better to just say how many people in the same fall in each category. Let's do this for occupation, gender, civil status, education, and village.
by_village <- as.data.frame(table(summary_vars$village))
#click on by_village in the environment, you get the number of people in each village. But theres a problem because the enumerators entered the villages in capital letters sometimes, so it looks like we have more villages than we actually do. We can fix this by using the command "tolower()" to convert everything to lowercase before making the table:
by_village <- as.data.frame(table(tolower(data$village))) #now it looks good!
by_gender <- as.data.frame(table(summary_vars$gender))
by_occupation <- as.data.frame(table(summary_vars$occupation))
by_civilstatus <- as.data.frame(table(summary_vars$civilstatus))
by_education <- as.data.frame(table(summary_vars$education))
#Now in the environment you have all the tables of the count of respondents by each categorical variable.
by_gender
## Var1 Freq
## 1 male 2
## 2 woman 24
by_occupation
## Var1 Freq
## 1 farming 16
## 2 housewife 1
## 3 other 1
## 4 salaried employment 1
## 5 self-employed off-farm 7
by_civilstatus
## Var1 Freq
## 1 domestic partnership 3
## 2 married 14
## 3 single 6
## 4 widowed 3
by_education
## Var1 Freq
## 1 primary 14
## 2 secondary 9
## 3 university 3
#notice that spend_spare_cash looks like its numeric, but actually the numbers refer to categories on the survey. We need to recode the variable to the names of the categories, otherwise its not useful information.
#The categories from the survey were:
# 1 buying food
# 2 buying possessions
# 3 improving the farm
# 4 spend on people
# 5 save the money
#lets recode spend_spare_cash so it gives the category name (note this requires dplyr again - dont need to do anything as long as you've already run library(dplyr) sometime this session):
summary_vars$spend_spare_cash <- recode(summary_vars$spend_spare_cash,
"1" = "buying food",
"2" = "buying possessions (clothes, household items)",
"3" = "improving the farm (livestock, fertilizers, crops, machines)",
"4" = "spend on people (education, healthcare, travel)",
"5" = "save the money")
#check the summary_vars dataframe, now you should see the category names. We can make a count table for this now:
by_spend <- as.data.frame(table(summary_vars$spend_spare_cash))
by_spend
## Var1 Freq
## 1 buying possessions (clothes, household items) 2
## 2 improving the farm (livestock, fertilizers, crops, machines) 20
## 3 other 1
## 4 save the money 2
## 5 spend on people (education, healthcare, travel) 1
#notice that gender is a binary variable, meaning it only has two options. For this type of variable you can also treat it as numeric, with woman = 1 and man = 0. Then we can see the percentage of people in the sample who are women or men. Make a binary numeric variable from gender like this:
# Create a new numeric variable based on the categorical variable
summary_vars$gender_num <- ifelse(summary_vars$gender == "woman", 1, 0)
#we have some other numeric variables in the data as well, like age, fies score, and hhsize. Most papers have a descriptive statistics table where they normally include the mean, minumum, maximum, and standard deviation etc for each variable. We wont do this for the categorical ones (the count tables are enough for now), but lets make a small summary table for the numeric variables
#There are lots of ways to make a summary table in R, but i like the one that uses the package "psych". Install it using install.packages("psych")
library(psych)
numeric <- select(summary_vars, gender_num, age, hhsize, fies_score) #subset the dataframe to only the numeric ones, same command we used to make the summary_vars dataframe earlier
describe(numeric, fast = TRUE) #if you dont specify fast = TRUE you will get a lot of extra statistics you probably dont need.
## vars n mean sd min max range se
## gender_num 1 26 0.92 0.27 0 1 1 0.05
## age 2 26 35.00 15.16 20 69 49 2.97
## hhsize 3 26 3.92 1.65 2 8 6 0.32
## fies_score 4 26 3.54 2.12 0 7 7 0.42
#concept check: what is the percentage of women in the sample? How do you know?
##7. Looking at the outcome variable Technically this is still descriptive statistics, because we don’t have enough data to start running regressions (let’s save that for another tutorial), but we can quickly take a look at which variety was the most popular among different kinds of people in the same, like men vs women (there’s only 2 men so this is not going to be that interesting).
STOPPED HERE BECAUSE I DON’T HAVE THE OUTCOME DATA READY