GithubHelp home page GithubHelp logo

matthieustigler / misconometrics Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 4.0 3.77 MB

A bunch of small R scripts useful in econometrics

License: GNU General Public License v3.0

Stata 3.76% R 5.56% HTML 90.68%

misconometrics's Introduction

Misconometrics

A bunch of small R scripts useful in econometrics

misconometrics's People

Contributors

jiaxiangbu avatar matthieustigler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

misconometrics's Issues

Running dec_covar by specifying formula with a function

@MatthieuStigler is it possible to run dec_covar with a generic formula (specified with a string) from inside of a function? I have been unable to figure out why the following examples (dec_test_1 and dec_test_2) don't work. I can provide the output of my sessionInfo() if that would be helpful, but I've tried to update all packages and tried on two separate systems and am running into the same errors.

The following is a reprex that runs successfully when specifying a particular formula, but not when trying to supply the formula as a function input:

n = 1000
test_data <- data.table(y = rnorm(n), x0 = rnorm(n), x1 = rnorm(n), x2 = rnorm(n), x3 = rnorm(n), x4 = rnorm(n), x5 = rnorm(n), z = as.factor(floor(runif(n, 0, 10))))

# works when running directly
full_model_1 <- felm(y ~ x0 + x1 + x2 + x3 + x4 + x5 | z, data = test_data)
dec <- dec_covar(object = full_model_1, var_main = "x0", format = "long", add_coefs = TRUE, conf.int = TRUE)

# works when supplying string input to formula
full_model_2 <- felm(as.formula("y ~ x0 + x1 + x2 + x3 + x4 + x5 | z"), data = test_data)
dec2 <- dec_covar(object = full_model_2, var_main = "x0", format = "long", add_coefs = TRUE, conf.int = TRUE)

# works when specifying a particular string formula inside a function
dec_test_0 <- function(){
  print("Running felm")
  full_model <- felm(as.formula("y ~ x0 + x1 + x2 + x3 + x4 + x5 | z"), data = test_data)
  print("Running dec_covar")
  dec <- dec_covar(object = full_model, var_main = "x0", format = "long", add_coefs = TRUE, conf.int = TRUE)
  dec
}
dec_test_0()

# doesn't work when supplying string formula inside a function
dec_test_1 <- function(formula_str){
  print("Running felm")
  full_model <- felm(as.formula(formula_str), data = test_data)
  print("Running dec_covar")
  dec <- dec_covar(object = full_model, var_main = "x0", format = "long", add_coefs = TRUE, conf.int = TRUE)
  dec
}
dec_test_1("y ~ x0 + x1 + x2 + x3 + x4 + x5 | z")

# also doesn't work when specifying the environment to the formula inside a function
dec_test_2 <- function(formula_str){
  .env <- environment() ## identify the environment 
  my_formula <- as.formula(formula_str, env = .env)
  print("Running felm")
  full_model <- felm(my_formula, data = test_data)
  print("Running dec_covar")
  dec <- dec_covar(object = full_model, var_main = "x0", format = "long", add_coefs = TRUE, conf.int = TRUE)
  dec
}
dec_test_2("y ~ x0 + x1 + x2 + x3 + x4 + x5 | z")

error message

Hello Matthieu,

Thank you for sharing the code. I ran the following code
dec_covar(object = model_full_1, var_main = "Education")
and got an error message:
Error in data.frame(term = names(co), estimate = unname(co), stringsAsFactors = FALSE) :
arguments imply differing number of rows: 0, 2

Any idea? Thanks!

Best,
Liying

Difficulty importing Gelbach_decompo/dec.covar.R script

Hi Matthieu

I just came across your script for Gelbach decomposition and importance of predictor variables, and would love to use the dec_covar and plot_dec functions on my lm () regression, but I cannot import it into R studio.

I'm using devtools, usethis, githubinstall libraries.
I tried the following command:
install.packages("https://raw.githubusercontent.com/MatthieuStigler/Misconometrics/master/Gelbach_decompo/dec_covar.R"). This returned the warning that this package is not available for this version of R. I am using version 4.0.4 "Lost Library Book".

Several variations of the command:
install_github("MatthieuStigler/Misconometrics/master/Gelbach_decompo/dec_covar.R") aren't giving me any joy either.

Is the problem that this is not a package, but a script? If so, how do I load the script into R studio, please?

Many thanks and kind regards, Diana

Bug when running lm()

Hi Matthieu,

as per email, here is the error I get when I try the decomposition with lm():

library(devtools)
#> Loading required package: usethis
source_url("https://raw.githubusercontent.com/MatthieuStigler/Misconometrics/master/Gelbach_decompo/dec_covar.R")
#> SHA-1 hash of file is ed716e821f7d61a45b36cedc16d1ed1be3abb046
library(tidyverse)
library(reprex)

model_full_1 <- lm(Fertility ~ ., data=swiss)
dec <- dec_covar(object = model_full_1, var_main = "Education")
#> Error in data.frame(term = names(co), estimate = unname(co), stringsAsFactors = FALSE): arguments imply differing number of rows: 0, 2

I get the same error when I do dec_covar on my linear model. While when I convert the data into panel data and use plm(), it works fine!

Thanks so much for looking into this.
Created on 2020-10-11 by the reprex package (v0.3.0)

Error message with felm

Hey Matthieu,

First, thank you so much for sharing the code! I attempted to use the dec_covar function following these steps:

library(lfe)
library(tidyverse)
library(broom)
library(Formula)

devtools::source_url("https://raw.githubusercontent.com/MatthieuStigler/Misconometrics/master/Gelbach_decompo/dec_covar.R")
model <- felm(y~ var_main + control1 + control2 + control3 + control4 |fixedeffects|0|id, data = df)
dec <- dec_covar(object = model, var_main = "var_main")

However, I get an error message stating, "Error in formula.default(object, env = baseenv()): invalid formula." I suspect that the issue lies with the "| fixedeffects | 0 | id" part. If I remove it, the code runs without errors. Nevertheless, I would like to account for fixed effects and cluster at id level. Do you have an idea on how to address this and obtain the decomposition?

Thank you very much in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.