Reproductions

  • Chapter 11: Monte Carlo Integration

    Exercise 11.2: Drawing frmm standard distributions # Sample size n1 <- 10L n2 <- 100L n3 <- 100000L # We use integers as indicated by the "L" at the end of the number. # This only makes the results look nicer # Reset random number generator for reproducibilty set.seed(123456) Uniform The standard specification of R’s random number generator (RNG) runif is min = 0 and max = 1, which is exactly what we need.
  • Chapter 20: Multivariate Time Series Methods

    Load required packages library(bvartools) library(dplyr) library(ggplot2) library(spam) library(tidyr) Data US macro data until 2007Q4 # Specify URL url <- "https://web.ics.purdue.edu/~jltobias/second_edition/Chapter20/code_for_exercise_1/US_macrodata.csv" # Load data us_macro_2007 <- read.csv(url, col.names = c("date", "Dp", "u", "r")) # Omit NA values us_macro_2007 <- na.omit(us_macro_2007) # Transform into time-series object us_macro_2007 <- ts(us_macro_2007[, -1], start = c(1959, 2), frequency = 4) Exercise 20.1 Prepare the data temp <- gen_var(us_macro_2007, p = 2, deterministic = "const") y <- temp$Y x <- temp$Z k <- NROW(y) tt <- ncol(y) z <- kronecker(t(x), diag(1, k)) m <- ncol(z) Specify the priors
  • Chapter 18: State Space and Unobserved Components Models

    Exercise 18.1: The local level model Data: US PCE inflation until 2015Q4 # Specify URL url <- "https://web.ics.purdue.edu/~jltobias/second_edition/Chapter18/code_for_exercise_1/USPCE_2015Q4.csv" # Load data pce <- read.csv(url, header = FALSE, col.names = "pci") # Log growth rates pce <- diff(ts(log(pce), start = 1959, frequency = 4)) # Rescale y <- matrix(pce * 100 * 4, 1) tt <- ncol(y) # T z <- diag(1, tt) # Data matrix Estimation Prior a0 <- 5 b0 <- 100 Initial values
  • Reproduction: Timmer, M. P., Dietzenbacher, E., Los, B., Stehrer, R., & De Vries, G. J. (2015). An illustrated user guide to the world input–output database: the case of global automotive production.

    As international trade has become increasingly fragmented over the past decades the analysis of global value chains (GVC) has gained popularity in economic research. This post reproduces Timmer et al. (2015), who introduce the world input-output database (WIOD) and present basic concepts of GVC analysis. Data Timmer et al. (2015) use the 2013 vintage of the world input-output database (WIOD). The following code downloads the data from the project’s website, unzips it and loads the resulting STATA file into R using the readstata13 package.
  • Chan et al. (2019): Bayesian Econometric Methods

    This section is based on Chan et al. (2019). Bayesian econometric methods (2nded.). The following links contain examples in the main text of the book and use R to estimate the models. Chapter 11: Monte Carlo Integration Chapter 18: State Space and Unobserved Components Models Chapter 20: Multivariate Time Series Methods
  • Chapter 8: Heteroskedasticity

    library(wooldridge) Example 8.1 After loading the data and generating the dummy variables from chapter 7 estimate the model in the already familiar way. data("wage1") marrmale <- as.numeric(wage1$female == 0 & wage1$married == 1) marrfem <- as.numeric(wage1$female == 1 & wage1$married == 1) singfem <- as.numeric(wage1$female == 1 & wage1$married == 0) lm.8.1 <- lm(lwage ~ marrmale + marrfem + singfem + educ + exper + expersq + tenure + tenursq, data = wage1) summary(lm.
  • Chapter 10: Basic Regression Analysis with Time Series Data

    library(wooldridge) Example 10.1 data("phillips") lm.10.1 <- lm(inf ~ unem, data = phillips) summary(lm.10.1) ## ## Call: ## lm(formula = inf ~ unem, data = phillips) ## ## Residuals: ## Min 1Q Median 3Q Max ## -5.2176 -1.7812 -0.6659 1.1473 8.8795 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 1.0536 1.5480 0.681 0.4990 ## unem 0.5024 0.2656 1.892 0.0639 . ## --- ## Signif. codes: 0 '***' 0.
  • Chapter 11: Further Issues in Using OLS with Time Series Data

    library(wooldridge) Example 11.4 For this regression the lagged values of return are already contained in the dataset. Thus, we do not have to calculated them ourselves and can simply run the regression. data("nyse") lm.11.4 <- lm(return ~ return_1, data = nyse) summary(lm.11.4) ## ## Call: ## lm(formula = return ~ return_1, data = nyse) ## ## Residuals: ## Min 1Q Median 3Q Max ## -15.261 -1.302 0.098 1.316 8.065 ## ## Coefficients: ## Estimate Std.
  • Chapter 16: Simultaneous Equations Models

    library(AER) library(wooldridge) Example 16.5 data("mroz") s.iv.1 <- summary(iv.1 <- ivreg(hours ~ lwage + educ + age + kidslt6 + nwifeinc | educ + age + kidslt6 + nwifeinc + exper + expersq, data = mroz)) s.iv.1 ## ## Call: ## ivreg(formula = hours ~ lwage + educ + age + kidslt6 + nwifeinc | ## educ + age + kidslt6 + nwifeinc + exper + expersq, data = mroz) ## ## Residuals: ## Min 1Q Median 3Q Max ## -4570.
  • Chapter 17: Limited Dependent Variable Models and Sample Selection Corrections

    Example 17.1 First, look at the linear model. data("mroz") lm.17.1 <- lm(inlf ~ nwifeinc + educ + exper + expersq + age + kidslt6 + kidsge6, data = mroz) summary(lm.17.1) ## ## Call: ## lm(formula = inlf ~ nwifeinc + educ + exper + expersq + age + ## kidslt6 + kidsge6, data = mroz) ## ## Residuals: ## Min 1Q Median 3Q Max ## -0.93432 -0.37526 0.08833 0.34404 0.
  • Chapter12: Serial Correlation and Heteroskedasticity in Time Series Regressions

    library(lmtest) library(sandwich) library(tseries) library(wooldridge) Example 12.1 data("phillips") Static Phillips curve lm.12.1.1 <- lm(inf ~ unem, data = phillips) res.static <- lm.12.1.1$res res.static_1 <- c(NA, lm.12.1.1$res[-(length(res.static))]) lm.12.1.1.test <- lm(res.static ~ res.static_1) summary(lm.12.1.1.test) ## ## Call: ## lm(formula = res.static ~ res.static_1) ## ## Residuals: ## Min 1Q Median 3Q Max ## -8.047 -1.104 -0.248 1.028 6.684 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -0.1118 0.3180 -0.
  • Chapter 2: Simple OLS

    In R the function for basic linear regression models is lm, which is short for linear model. Its first argument is a formula of the regression model, which has the form y ~ a. The tilde between y and a indicates that y is the dependent variable and a is the explanatory variable. It is also possible to add a further explanatory variable - for example b - to the regression by adding a plus sign followed by the the name of the additional variable to the formula.
  • Chapter 5: OLS Asymptotics

    Make a histogram of the variable “prate”. data("k401k") hist(k401k$prate) Example 5.2 data("bwght") lm.1 <- lm(lbwght ~ cigs + lfaminc, data = bwght[1:694,]) s.1 <- summary(lm.1) lm.2 <- lm(lbwght ~ cigs + lfaminc, data = bwght) s.2 <- summary(lm.2) s.2$coefficients[2, 2] / s.1$coefficients[2, 2] ## [1] 0.6443341 lm.3 <- lm(cigs ~ lfaminc, data = bwght[1:694,]) s.3 <- summary(lm.3) sigma.j <- s.3$coefficients[2, 2] sigma <- s.3$sigma r2 <- s.3$r.squared sigma / (sqrt(1388) * sigma.
  • Wooldridge (2013): Introductory Econometrics

    This section is based on Wooldridge, J.M. (2013). Introductory econometrics: A modern approach (5thed.). The following links contain examples in the main text of the book and use R to estimate the models. Alternatively, Heiss, F. (2016) Using R for Introductory Econometrics is a standalone textbook, which covers the same topics as Wooldridge (2013) and provides an introduction to R as well. The data sets are from the wooldridge package, which is a collection of all data sets used in the Wooldridge textbook.
  • Reproduction: Cincera, M. (1997). Patents, R&D, and technological spillovers at the firm level.

    Get the data The data set can be downloaded from the Journal of Applied Econometrics Data Archive. download.file("http://qed.econ.queensu.ca/jae/1997-v12.3/cincera/mc-data.zip", destfile = "1997_cincera_data.zip") unzip("1997_cincera_data.zip") data <- read.delim("data.mc", header = FALSE) names(data) <- c("fi", "s", "g", "p83", "p84", "p85", "p86", "p87", "p88", "p89", "p90", "p91", "lr83", "lr84", "lr85", "lr86", "lr87", "lr88", "lr89", "lr90", "lr91", "ls83", "ls84", "ls85", "ls86", "ls87", "ls88", "ls89", "ls90", "ls91") p <- matrix(as.matrix(data[, c("p83", "p84", "p85", "p86", "p87", "p88", "p89", "p90", "p91")])) k <- matrix(as.
  • Chapter 14: Advanced Panel Data Methods

    library(wooldridge) In order to estimate panel data models, I use the plm function from the plm package. The intuition behind its structure is similar to the ordinary linear model, except that it allows you to specify the panel’s group and time variables and the effects model. Example 14.1 Similar to the lm function, you can specify your model’s equation and the sample. Since you want to use panel data methods for you estimation, you also have to specify which variables in your sample contain the information used to distinguish different groups and which variable contains the time measurement.
  • Chapter 15: Instrumental Variables Estimation and Two Stage Least Squares

    library(AER) library(wooldridge) Example 15.1 data("mroz") OLS s.lm.1 <- summary(lm(lwage ~ educ, data = mroz)) s.lm.1 ## ## Call: ## lm(formula = lwage ~ educ, data = mroz) ## ## Residuals: ## Min 1Q Median 3Q Max ## -3.10256 -0.31473 0.06434 0.40081 2.10029 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) -0.1852 0.1852 -1.000 0.318 ## educ 0.1086 0.0144 7.545 2.76e-13 *** ## --- ## Signif. codes: 0 '***' 0.
  • Chapter13: Simple Panel Data Methods

    library(wooldridge) Example 13.1 data("fertil1") s.lm.1 <- summary(lm.1 <- lm(kids ~ educ + age + agesq + black + east + northcen + west + farm + othrural + town + smcity + y74 + y76 + y78 + y80 + y82 + y84, data = fertil1)) s.lm.1 ## ## Call: ## lm(formula = kids ~ educ + age + agesq + black + east + northcen + ## west + farm + othrural + town + smcity + y74 + y76 + y78 + ## y80 + y82 + y84, data = fertil1) ## ## Residuals: ## Min 1Q Median 3Q Max ## -3.
  • Chapter 3: Multiple Regression Analysis

    Below you find the script for all examples in chapter 3 of Wooldridge (2013). The only difference to the last chapter is how to use multiple independent variables in the lm function. This is easily achieved by separating each exogenous variable that should enter the model with a plus sign, e.g. lm(colGPA ~ hsGPA + ACT). The other commands that appear new are quite helpful and you should take a minute to think about them:
  • Chapter 6: Further Issues

    Chapter 6 in Wooldridge (2013) deals with some more issues that might have an impact on your estimates. Among these are a change in the scale of your variables, so-called beta-factors and the interpretation of variables that appear together with their squared values, i.e. interaction terms. Before you start, set your working directory, load the foreign library and download the data we are going to use in this chapter.
  • Chapter 7: Multiple Regression Analysis with Qualitative Information

    library(wooldridge) Estimating dummy variables that reflect qualitative information works quite the same way in R as it does with quantitative variables. You just add it as an independent variable into the formula of the model. # Example 7.1 data("wage1") lm.7.1.1 <- lm(wage ~ female + educ + exper + tenure, data = wage1) summary(lm.7.1.1) ## ## Call: ## lm(formula = wage ~ female + educ + exper + tenure, data = wage1) ## ## Residuals: ## Min 1Q Median 3Q Max ## -7.
  • Chapter 9: More on Specification and Data Issues

    library(wooldridge) Example 9.1 data("crime1") lm.9.1.1 <- lm(narr86 ~ pcnv + avgsen + tottime + ptime86 + qemp86 + inc86 + black + hispan, data = crime1) summary(lm.9.1.1) ## ## Call: ## lm(formula = narr86 ~ pcnv + avgsen + tottime + ptime86 + qemp86 + ## inc86 + black + hispan, data = crime1) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.0108 -0.4518 -0.2392 0.2707 11.5284 ## ## Coefficients: ## Estimate Std.
  • Statistical Inference

    Before we start, let’s clear our memory, set the working directory and load the wooldridge data package. rm(list = ls()) setwd("C:/path/to/the/working/directory") library(wooldridge) data(wage1) lm.1 <- lm(lwage ~ educ + exper + tenure, data = wage1) s.1 <- summary(lm.1) s.1 ## ## Call: ## lm(formula = lwage ~ educ + exper + tenure, data = wage1) ## ## Residuals: ## Min 1Q Median 3Q Max ## -2.05802 -0.29645 -0.03265 0.28788 1.
  • Reproduction: Sheperd, B. (2016). The gravity model of international trade: A user guide.

    The updated paper and dataset can be downloaded from UNESCAP. Load libraries and read data library(AER) library(dplyr) library(foreign) library(ggplot2) library(lmtest) library(multiwayvcov) library(sampleSelection) data <- read.dta("servicesdataset_2016.dta") Correlations Table 1 data <- data %>% mutate(ln_trade = log(trade), ln_distance = log(dist), ln_gdp_exp = log(gdp_exp), ln_gdp_imp = log(gdp_imp)) cor.data <- data %>% filter(sector == "SER") %>% select(ln_trade, ln_gdp_exp, ln_gdp_imp, ln_distance) %>% na.omit %>% filter(ln_trade > -Inf) round(cor(cor.data), 4) ## ln_trade ln_gdp_exp ln_gdp_imp ln_distance ## ln_trade 1.