A bunch of small R scripts useful in econometrics
matthieustigler / misconometrics Goto Github PK
View Code? Open in Web Editor NEWA bunch of small R scripts useful in econometrics
License: GNU General Public License v3.0
A bunch of small R scripts useful in econometrics
License: GNU General Public License v3.0
Hi, this is a three year old repository but I would like to make you aware of a fairly new option to run plm in fast mode: set options("plm.fast" = TRUE)
is possible from plm version 2.4-0 on (see the NEWS file of the package). The current development version 2.4-1.99999 brings more speed-ups (in optional fast mode and in regular mode) https://r-forge.r-project.org/R/?group_id=406.
@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")
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
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
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)
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!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.