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

Error produced by using factor variables #201

Closed jongohkim91 closed 3 years ago

jongohkim91 commented 3 years ago

Hello Joshua,

It is Jongoh again. Hopefully I am not disturbing you this time with a minor issue.

I just figured out that adding any factor variable in ivlike part of the ivmte function leads to spitting out the error message:"The following components are not included in the IV-like specification:..."

I checked the reason why such error message is printed. It turns out that discrepancy will always exists between the column names from the design matrix (colnames(mf$x)), which is simply xVars in ivEstimate fucntion, and the terms in ivlike specification.

https://github.com/jkcshea/ivmte/blob/3dc2b7c84f0ac8cd47c6ab9744da4aed52c7cd27/R/ivlike.R#L168-L286

For example, if I have a factor variable called var1 <- c('a','b','c') then the xVars will have columns with names 'var1a', 'var1b', 'var1c'. However, if I simply put 'var1' in ivlike, e.g. ivlike = y ~ x + var1, there are no correpsonding components in ivlike specification for' var1a', 'var1b', 'var1c' columns to match. Hence, it returns the error message.

I hope this was helpful.

Best, Jongoh

jkcshea commented 3 years ago

Hi Jungoh,

No worries at all. I have good news, and that is we do support factor variables. Attached are two simple examples of how to declare them properly (you just need to enclose their name in factor(...)). Let me know if that resolves your issue.

factor-example.zip

Thanks for the post, as it may have revealed that some other factor variable-related features of the package need to be revisited (see #202).

jongohkim91 commented 3 years ago

Thank you very much for the quick reply. It indeed solved my problem. I wish you a wonderful weekend!

Best, Jongoh