jkcshea / ivmte

An R package for implementing the method in Mogstad, Santos, and Torgovitsky (2018, Econometrica).
GNU General Public License v3.0
18 stars 2 forks source link

S weights and factor(d) #155

Closed jkcshea closed 4 years ago

jkcshea commented 4 years ago

Constructing the matrices to estimate the S weights becomes problematic when the user uses factor(d) instead of d when declaring IV-like specifications.

The issue arises when fixing d to either 0 or 1: A contrasts error will arise, since factor(d) does not make sense when d is a constant.'

You already had code that takes the formula, and converts all factor(d) expressions into booleans. However, you did not have code to account for the fact that some columns generated prior to fixing d (e.g. factor(d)1:x)) will not be generated once fixing d (e.g. if d is fixed at 0, then factor(d)1:x will not be generated).

But you know beforehand which columns should exist after fixing d. So the solution is simply to generate any missing columns (with values 0) once you fix d. Here is the code: https://github.com/jkcshea/ivmte/blob/5fab27f8f6ca35d983d66726c66f99ad9d17ef12/R/design.R#L86-L92