Before you start: Replace Your Name in the YAML header above. Knit to HTML after each section to catch errors early. Submit the knitted HTML file and your .Rmd file to Moodle.
Grading reminder: All answers must be in full sentences. Do not use raw R variable names (
water,female) in your text — use plain English (“water facilities”, “female-led councils”). Plots must be cropped to the graph only. Code appendix must be present. A classmate will grade this using the rubric at the end of the worksheet document.
Run this chunk first every time. If a package is not installed,
uncomment the install.packages line, run it once, then
re-comment it.
# install.packages(c("tidyverse")) # run once if needed, then comment out
library(tidyverse)
We are using the India Village Dataset, from:
Chattopadhyay, R. & Duflo, E. (2004). Women as Policy Makers: Evidence from a Randomized Policy Experiment in India. Econometrica, 72(5), 1409–1443.
In this study, villages were randomly assigned to
have a female council head (female = 1) or not
(female = 0). We observe infrastructure outcomes — new or
repaired water facilities and irrigation facilities — for each
village.
# Set the path to where india.csv lives on your computer
# You can also place india.csv in the same folder as this .Rmd file
india <- read.csv("/Users/aya/Downloads/india.csv")
💡 Tip: If you get an error loading the file, check that
india.csvis in the same folder as this.Rmdfile, or update the path insideread.csv().
Here is how to look at the dataset’s size and structure. Run each chunk and look at the output.
dim(india) # rows x columns
## [1] 322 4
head(india, 10) # first 10 rows
## village female water irrigation
## 1 GP1_village2 1 10 0
## 2 GP1_village1 1 0 5
## 3 GP2_village2 1 2 2
## 4 GP2_village1 1 31 4
## 5 GP3_village2 0 0 0
## 6 GP3_village1 0 0 0
## 7 GP4_village2 0 7 4
## 8 GP4_village1 0 12 0
## 9 GP5_village2 0 28 0
## 10 GP5_village1 0 0 0
glimpse(india) # variable types and preview
## Rows: 322
## Columns: 4
## $ village <chr> "GP1_village2", "GP1_village1", "GP2_village2", "GP2_villag…
## $ female <int> 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,…
## $ water <int> 10, 0, 2, 31, 0, 0, 7, 12, 28, 0, 23, 12, 0, 0, 41, 23, 0, …
## $ irrigation <int> 0, 5, 2, 4, 0, 0, 4, 0, 0, 0, 0, 4, 0, 0, 4, 5, 0, 1, 9, 52…
summary(india) # summary statistics for all variables
## village female water irrigation
## Length :322 Min. :0.0000 Min. : 0.00 Min. : 0.000
## N.unique :322 1st Qu.:0.0000 1st Qu.: 3.00 1st Qu.: 0.000
## N.blank : 0 Median :0.0000 Median : 9.00 Median : 0.000
## Min.nchar: 12 Mean :0.3354 Mean : 17.84 Mean : 3.264
## Max.nchar: 14 3rd Qu.:1.0000 3rd Qu.: 20.00 3rd Qu.: 2.000
## Max. :1.0000 Max. :340.00 Max. :90.000
YOUR TURN — write your answer here (replace this text):
How many observations are there? How many variables? Write 2–3 sentences describing what this dataset is: what each row represents, what kinds of information the columns record, and what research question it could help us answer.
Here is how to check variable types in R:
sapply(india, class)
## village female water irrigation
## "character" "integer" "integer" "integer"
The key distinctions are:
YOUR TURN — write your answer here:
For each variable, state its R data type and level of measurement. Explain your reasoning for any classification that required a judgment call. Write this as a paragraph or clearly organized description — not a bullet list.
Here is a boxplot comparing water facilities between the two council
types. The professor’s example uses factor() to label the
groups clearly.
ggplot(india, aes(x = factor(female), y = water)) +
geom_boxplot(fill = "darkblue", alpha = 0.6) +
geom_jitter(width = 0.1, alpha = 0.3) +
scale_x_discrete(labels = c("0" = "Male-led", "1" = "Female-led")) +
labs(
title = "Water Facilities by Council Type",
x = "Council type",
y = "Number of new or repaired water facilities"
) +
theme_linedraw()+
theme(
plot.title=element_text(size = 18,face = "bold.italic",color = "darkblue",hjust = 0.5),
axis.title.x = element_text(size = 10,color="black",face = "bold"),
axis.title.y = element_text(size =10,color="black",face = "bold"),
axis.text = element_text(size = 10,color="black")
)
YOUR TURN — write your interpretation here:
Write 3–4 sentences describing what you see. Compare the medians and spreads of the two groups. Comment on any outliers. What might this pattern suggest about female political leadership and water infrastructure investment?
Here is how to make a histogram for the water variable.
Study this code, then make your own histogram for a different
variable.
ggplot(india, aes(x = water)) +
geom_histogram(binwidth = 50, fill = "plum", color = "white") +
labs(
title = "Distribution of Water Facility Investment",
x = "New or repaired water facilities",
y = "Number of villages"
) +
theme_linedraw()+
theme(
plot.title=element_text(size = 18,face = "bold",color = "plum"),
axis.title.x=element_text(size = 10,color = "black"),
axis.title.y=element_text(size = 10,color = "black"),
axis.text=element_text(size=10,color = "black")
)
YOUR TURN: Make a histogram for the
irrigation variable (or any other numeric variable in the
dataset). Copy and adapt the code above:
ggplot(india, aes(x = irrigation)) +
geom_histogram(binwidth = 10, fill ="salmon",color = "white") +
labs(
title = "Distribution of Irrigation Facility Investments",
x = "New or repaired irrigation facilities",
y = "Number of villages"
) +
theme_linedraw()+
theme(
plot.background = element_rect(fill = "lightblue"),
plot.title=element_text(size = 18,face = "bold",color = "white"),
axis.title.x=element_text(size = 10,color = "white"),
axis.title.y=element_text(size = 10,color = "white"),
axis.text=element_text(size=10,color = "white")
)
Write your interpretation here:
Describe the shape of the distribution in 2–3 sentences. Is it symmetric or skewed? Unimodal or bimodal? Are there gaps or extreme values? What does this shape tell you about how that variable is distributed across villages?
Here is how to filter rows and create new variables:
# Keep only female-led villages
female_villages <- india %>%
filter(female == 1)
nrow(female_villages) # how many remain?
## [1] 108
# Create a new variable: total facilities (water + irrigation)
india <- india %>%
mutate(total_facilities = water + irrigation)
# Compare means across groups
india %>%
group_by(female) %>%
summarise(
n = n(),
mean_water = mean(water, na.rm = TRUE),
mean_irrig = mean(irrigation, na.rm = TRUE),
mean_total = mean(total_facilities, na.rm = TRUE)
)
## # A tibble: 2 × 5
## female n mean_water mean_irrig mean_total
## <int> <int> <dbl> <dbl> <dbl>
## 1 0 214 14.7 3.39 18.1
## 2 1 108 24.0 3.02 27.0
YOUR TURN — write your answer here:
How many female-led villages are there? How many male-led? Report the mean total facilities for each group. Write 2–3 sentences interpreting the difference — what does it suggest, and what would you need to be more confident it is not just chance?
R comes with many built-in datasets. Here is one example using
mtcars:
glimpse(mtcars)
## Rows: 32
## Columns: 11
## $ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19.2, 17.8,…
## $ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, 4, 4, 8,…
## $ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 140.8, 16…
## $ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, 180, 180…
## $ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.92, 3.92,…
## $ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3.150, 3.…
## $ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 22.90, 18…
## $ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,…
## $ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0,…
## $ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, 4, 3, 3,…
## $ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2,…
ggplot(mtcars, aes(x = wt, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Car weight vs fuel efficiency",
x = "Weight (1000 lbs)", y = "Miles per gallon") +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
YOUR TURN: Choose a different dataset (e.g.,
iris, gapminder from the
gapminder package, or any dataset you find with
data()). Create one plot showing a distribution and one
showing a relationship between two variables.
glimpse(iris)
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
ggplot(iris, aes(x = Sepal.Length, y = Petal.Length)) +
geom_point() +
geom_smooth(method = "lm", se = FALSE) +
labs(title = "Sepal length vs petal length",
x = "Sepal length", y = "Petal length") +
theme_linedraw()+
theme(
plot.title = element_text(size = 18,face = "bold"),
axis.title.x = element_text(size = 10),
axis.title.y = element_text(size = 10)
)
## `geom_smooth()` using formula = 'y ~ x'
ggplot(iris, aes(x = Sepal.Length)) +
geom_histogram(binwidth = 0.5, fill ="royalblue",color = "white") +
labs(
title = "Distribution of Sepal Length",
x = "Sepal length",
y = "Number of flowers"
) +
theme_linedraw()+
theme(
plot.title = element_text(size = 18, face = "bold"),
axis.title.x = element_text(size = 10),
axis.title.y = element_text(size = 10)
)
Write your answer here:
Name the dataset you chose and describe it briefly. Interpret each plot in 2–3 sentences. Why did you find this dataset interesting?
No code needed for this question. Think carefully before writing.
YOUR TURN — write your answer here:
Based on the India dataset, write a research hypothesis. Your hypothesis must: (1) name a specific dependent variable Y and independent variable X, (2) state the expected direction of the relationship (X increases/decreases Y), and (3) be falsifiable — you could imagine data that would prove it wrong.
Then explain in 3–4 sentences: Is this a causal claim or a correlational one? What would need to be true to make it causal? What is one major confounder you would need to control for?
.html file and this .Rmd file
to Moodle.AI use note: You may use AI tools to help debug R errors or explain what a function does. If you do, add a sentence at the end of your submission noting what you used and how. All written interpretations must be your own.