In this exercise, we consider the effect of a change in the price of agricultural goods whose production and cultivation are dominated by either men or women.

This exercise is based on: Qian, Nancy. 2008. “Missing Women and the Price of Tea in China: The Effect of gender-Specific Earnings on gender Imbalance.” Quarterly Journal of Economics 123(3): 1251–85.

Our data come from China, where centrally planned production targets during the Maoist era led to changes in the prices of major staple crops. We focus here on tea, the production and cultivation of which required a large female labor force, as well as orchard fruits, for which the labor force was overwhelmingly male. We use price increases brought on by government policy change in 1979 as a proxy for increases in gender-specific income, and ask the following question: Do changes in gender-specific income alter the incentives for Chinese families to have children of one gender over another? The CSV data file, chinawomen.csv, contains the variables shown in the table below, with each observation representing a particular Chinese county in a given year. Note that post is an indicator variable that takes takes 1 in a year following the policy change and 0 in a year before the policy change.


 
  
 
--------------------------------------------------------------------------------
 Name                 Description
 -------------------- ----------------------------------------------------------
 `birpop`               Birth population in a given year
 
 `biryr`              Year of cohort (birth year)
 
 `cashcrop`             Amount of cash crops planted in county
 
 `orch`                 Amount of orchard-type crops planted in county
 
 `teasown`            Amount of tea sown in county
 
 `gender`                Proportion of males in birth cohorts
 
 `post`               Indicator variable for instruction of price reforms
 --------------------------------------------------------------------------------
 
 

## Question 1

We begin by examining gender ratios in the post-reform period (that is, the period after 1979) according to whether or not tea crops were sown in the region. Estimate the mean gender ratio in 1985, which we define as the proportion of male births, separately for tea-producing and non-tea-producing regions. Compute the 95% confidence interval for each estimate by assuming independence across counties within a year (We will maintain this assumption throughout this exercise). Furthermore, compute the difference-in-means between the two regions and its 95% confidence interval. Are gender ratios different across these regions? What assumption is required in order for us to interpret this difference as causal?
  
**Note**: Do not run the "install_github"  function every time you compile, it is un-necessary. You only need to install the package once. 
  
## Answer 1

```{r, echo=FALSE}
#library(devtools)
#install_github("kosukeimai/qss-package", build_vignettes = TRUE)
library(qss)
data("chinawomen",package="qss")
women <- chinawomen
rm(chinawomen)
```


```{r}
## load data

women$teadum <- ifelse(women$teasown > 0 & women$orch == 0, 1, 0)
women$orchdum <- ifelse(women$orch > 0 & women$teasown == 0, 1, 0)
women.after <- women[women$biryr == 1985, ]
## function to compute standard errors, confidence intervals
est.conf <- function(x, conf.level){
  mean.x <- mean(x, na.rm = TRUE)
  se.x <- sqrt(mean.x * (1 - mean.x) / length(x))
  ci <- c(mean.x - qnorm(1 - conf.level / 2) * se.x, 
          mean.x + qnorm(1 - conf.level / 2) * se.x)
  final <- c(mean.x, se.x, ci, length(x))
  names(final) <- c("mean", "se", "lower.ci", "upper.ci", "n.obs")
  return(final)
}
## point estimates and CIs
tea <- est.conf(women.after$sex[women.after$teadum == 1], 
                conf.level = 0.05)
orch <- est.conf(women.after$sex[women.after$orchdum == 1], 
                 conf.level = 0.05)
## difference-in-means
conf.level <- 0.05
est.diff <- tea[1] - orch[1]
se.diff <- sqrt(tea[2]^2 / tea[5] + orch[2]^2 / orch[5])
cis.diff <- c(est.diff - qnorm(1 - conf.level / 2) * se.diff, 
              est.diff + qnorm(1 - conf.level / 2) * se.diff)
results <- c(est.diff, se.diff, cis.diff)
names(results) <- c("diff", "se", "lower.ci", "upper.ci")
results

```

We find that the difference in sex ratios across the regions is 0.007, with a 95 percent confidence interval equal to [-0.025, 0.039 ]. Since the interval contains zero, it appears that sex ratios do not significantly differ across the regions after the introduction of the reforms.


## Question 2

Repeat the analysis in the previous question for subsequent years, i.e., 1980, 1981, 1982, …, 1990. Create a graph which plots the difference-in-means estimates and their 95% confidence intervals against years. Give a substantive interpretation of the plot.
   
## Answer 2

```{r}
 par(cex = 1.5) 
years <- unique(women$biryr[women$biryr > 1980])
diff <- matrix(data = NA, nrow = length(years), ncol = 5)
rownames(diff) <- years
colnames(diff) <- c("est", "se", "lower.ci", "upper.ci", "n.obs")
for(i in 1:length(years)){
  tea <- est.conf(women$sex[women$teadum == 1 & women$biryr == years[i]], 
                  conf.level = 0.05)
  orch <- est.conf(women$sex[women$orchdum == 1 & women$biryr == years[i]], 
                   conf.level = 0.05)
  conf.level <- 0.05
  est.diff <- tea[1] - orch[1]
  se.diff <- sqrt(tea[2]^2/tea[5] + orch[2]^2 / orch[5])
  cis.diff <- c(est.diff - qnorm(1 - conf.level/2) * se.diff, 
                est.diff + qnorm(1 - conf.level/2) * se.diff)
  diff[i, ] <- c(est.diff, se.diff, cis.diff, tea[5] + orch[5])
}
plot(years, diff[, "est"], type = "l",
     xlab = "Year", ylab = "Difference in Sex Ratio",
     ylim = c(min(diff[, "lower.ci"]) - 4 * sd(diff[, "lower.ci"]), 
              max(diff[, "upper.ci"]) + 4 * sd(diff[, "upper.ci"])), 
     main = "Post-Reform Sex Ratio Difference")
lines(years, diff[, "lower.ci"], lty = 2, col = "red")
lines(years, diff[, "upper.ci"], lty = 2, col = "red")
abline(h = 0, lty = 3)
```
 
 
 The plot shows that despite some variation across the years, the confidence intervals remain quite large. As such, there does not appear to be a change over time in our difference-in-means estimates in the period after the reform was implemented. A slight dip occurs at the beginning and end of the periods, however, which would correspond to an increase in the sex ratio in fruit-producing counties relative to tea-producing counties.
 

## Question 3

Next, we compare tea-producing and orchard-producing regions before the policy enactment. Specifically, we examine the gender ratio and the proportion of Han Chinese in 1978. Estimate the mean difference, its standard error, and 95% confidence intervals for each of these measures between the two regions. What do the results imply about the interpretation of the results given in Question~1?

## Answer 3

```{r}
 women.before <- women[women$biryr == 1978, ]
han.tea <- est.conf(women.before$han[women.before$teadum == 1], 
                    conf.level = 0.05)
sex.tea <- est.conf(women.before$sex[women.before$teadum == 1], 
                    conf.level = 0.05) 
han.orch <- est.conf(women.before$han[women.before$orchdum == 1], 
                     conf.level = 0.05)
sex.orch <- est.conf(women.before$sex[women.before$orchdum == 1], 
                     conf.level = 0.05) 
## Han difference
han.diff <- han.tea[1] - han.orch[1]
han.se <- sqrt(han.tea[1]^2 / han.tea[5] + 
                   han.orch[1]^2 / han.orch[5])
han.cis <- c(han.diff - qnorm(1-conf.level / 2) * han.se, 
             han.diff + qnorm(1-conf.level / 2) * han.se)
results <- c(han.diff, han.se, han.cis)
names(results) <- c("mean", "se", "lower.ci", "upper.ci")
results
```
 

```{r}
## sex difference
sex.diff <- sex.tea[1] - sex.orch[1]
sex.se <- sqrt(sex.tea[1]^2 / sex.tea[5] + 
                   sex.orch[1]^2 / sex.orch[5])
sex.cis <- c(sex.diff - qnorm(1 - conf.level / 2) * sex.se, 
             sex.diff + qnorm(1 - conf.level / 2) * sex.se)
results <- c(sex.diff, sex.se, sex.cis)
names(results) <- c("mean", "se", "lower.ci", "upper.ci")
results
```

We find no statistically significant difference in both variables between tea-producing and orchard-producing regions before the policy enactment, suggesting that these two regions may be comparable. This increases the validity of findings in Question 1.



## Question 4

Repeat the analysis for the gender ratio in the previous question for each year before the reform, i.e., from 1962 until 1978. Create a graph which plots the difference-in-means estimates between the two regions and their 95% confidence intervals against years. Give a substantive interpretation of the plot.

## Answer 4


Similar to the other analyses, we see that while there is a difference in the sex ratio, it remains small and the confidence intervals for each year are relatively large. There does not appear to be a meaningful change in the difference between the sex ratios of the two regions in the period leading up to the reforms.

```{r}
 par(cex = 1.5)
## sex differences
years <- unique(women$biryr[women$biryr < 1978])
results.sex <- matrix(data = NA, ncol = 4, nrow = length(years))
rownames(results.sex) <- years
colnames(results.sex) <- c("mean", "se", "lower.ci", "upper.ci")
for(i in 1:length(years)){
    women.before <- women[women$biryr == years[i], ]
    sex.tea <- est.conf(women.before$sex[women.before$teadum == 1], 
                        conf.level = 0.05)
    sex.orch <- est.conf(women.before$sex[women.before$orchdum == 1], 
                         conf.level = 0.05)
    ## sex difference
    sex.diff <- sex.tea[1] - sex.orch[1]
    sex.se <- sqrt(sex.tea[1]^2 / sex.tea[5] + 
                       sex.orch[1]^2 / sex.orch[5])
    sex.cis <- c(sex.diff - qnorm(1 - conf.level / 2) * sex.se, 
                 sex.diff + qnorm(1 - conf.level / 2) * sex.se)
    results.sex[i, ] <- c(sex.diff, sex.se, sex.cis)
}
## graph the results
plot(years, results.sex[, "mean"], type = "l",
     xlab = "Year", ylab = "Difference in Sex Ratio",
     ylim = c(min(results.sex[, "lower.ci"]) - 
                  4 * sd(results.sex[, "lower.ci"]), 
              max(results.sex[, "upper.ci"]) + 
                  4 * sd(results.sex[, "upper.ci"])), 
     main = "Pre-Reform Sex Ratio Difference")
lines(years, results.sex[, "lower.ci"], lty = 2, col = "red")
lines(years, results.sex[, "upper.ci"], lty = 2, col = "red")
abline(h = 0, lty = 3)
```
 
 
## Question 5

We will adopt the difference-in-differences design by comparing the gender ratio in 1978 (right before the reform) with that in 1980 (right after the reform). Focus on a subset of counties that do not have missing observations in these two years. Compute the difference-in-differences estimate and its 95% confidence interval. Note that we assume independence across counties but account for possible dependence across years within each county. Then, the variance of the difference-in-differences estimate is given by:

$$
\frac{(\overline{Y}_{tea, after} - \overline{Y}_{tea, before}) - (\overline{Y}_{orchard, after} - \overline{Y}_{orchard, before})}{(\overline{Y}_{tea, after} - \overline{Y}_{tea, before}) + (\overline{Y}_{orchard, after} - \overline{Y}_{orchard, before})}
$$

where dependence across years is given by:

$$
\frac{\overline{Y}_{tea, after} - \overline{Y}_{tea, before}}{\overline{Y}_{tea, after} - 2 \text{Cov}(\overline{Y}_{tea, after}, \overline{Y}_{tea, before}) + \overline{Y}_{tea, before}}
$$

$$
\frac{1}{n} (\overline{Y}_{tea, after} - 2 \text{Cov}(\overline{Y}_{tea, after}, \overline{Y}_{tea, before}) + \overline{Y}_{tea, before})
$$

A similar formula can be given for orchard-producing regions. What substantive assumptions does the difference-in-differences design require? Give a substantive interpretation of the results.

## Answer 5

Somewhat surprisingly, the difference-in-difference estimates suggest that the tea-producing regions saw a relative increase of 0.02 in the proportion of males born when compared to the fruit-producing regions. The 95 percent confidence interval for our estimate is [-0.006, 0.047], suggesting that probably does not differ from zero. The assumption we make is the parallel time trend assumption, which requires that the over-time change in the tea-producing regions would have been the same as in the fruit-producing regions.

```{r}
 ## Tea-producing regions
counties.before <- unique(women$admin[women$biryr == 1978])
counties.after <- unique(women$admin[women$biryr == 1980])
counties.list <- intersect(counties.before, counties.after)
women.before <- women[women$teadum == 1 & women$biryr == 1978 & 
                          women$admin %in% counties.list, ]
women.after <- women[women$teadum == 1 & women$biryr == 1980 & 
                         women$admin %in% counties.list, ]
tea.before <- est.conf(women.before$sex, conf.level = 0.95)
tea.after <- est.conf(women.after$sex, conf.level = 0.95)
covar.tea <- cov(women.before$sex,  women.after$sex, 
                 use = "pairwise.complete.obs")
est.tea <- tea.after[1] - tea.before[1]
var.tea <- (tea.after[2]^2 + tea.before[2]^2 - 2*covar.tea) / 
    (tea.before[5] + tea.after[5])
## Fruit-producing regions
women.before <- women[women$orchdum == 1 & women$biryr == 1978 & 
                          women$admin %in% counties.list, ]
women.after <- women[women$orchdum == 1 & women$biryr == 1980 & 
                         women$admin %in% counties.list, ]
orch.before <- est.conf(women.before$sex, conf.level = 0.95)
orch.after <- est.conf(women.after$sex, conf.level = 0.95)
covar.orch <- cov(women.before$sex,  women.after$sex, 
                 use = "pairwise.complete.obs")
est.orch <- orch.after[1] - orch.before[1]
var.orch <- (orch.after[2]^2 + orch.before[2]^2 - 2*covar.orch) / 
    (orch.before[5] + orch.after[5])
## Difference-in-differences
est.did <- est.tea - est.orch
se.did <- sqrt(var.tea + var.orch)
cis.did <- c(est.did - qnorm(1 - conf.level/2) * se.did, 
             est.did + qnorm(1 - conf.level/2) * se.did)
results <- c(est.did, se.did, cis.did)
names(results) <- c("diff-in-diff", "se", "lower.ci", "upper.ci")
results
```

## Question 6

How about matching method for the same research question? 
Could you come up with a design in which we can use matching for this research question?
What additional variables would you suggest the researchers to collect?

## Answer 6

They can list any pre-treatment covariates that may be related to the treatment assignment and the outcome. For example, they can collect data on the population of the county, the average income of the county, the average education level of the county, and the average age of the county.  




 




