Skip to content

Commit

Permalink
version 2.4.10
Browse files Browse the repository at this point in the history
  • Loading branch information
Rgui authored and cran-robot committed Jul 3, 2024
1 parent 8eea057 commit 390f40a
Show file tree
Hide file tree
Showing 15 changed files with 93 additions and 64 deletions.
10 changes: 5 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Package: REndo
Title: Fitting Linear Models with Endogenous Regressors using Latent
Instrumental Variables
Type: Package
Version: 2.4.9
Date: 2023-09-04
Version: 2.4.10
Date: 2024-07-02
Authors@R: c(
person(given="Raluca", family="Gui", email = "raluca.gui@gmail.com", role = c("cre","aut")),
person(given="Markus", family="Meierer", email = "markus.meierer@business.uzh.ch", role = "aut"),
Expand All @@ -29,14 +29,14 @@ Imports: methods (>= 3.4), stats (>= 3.4), utils (>= 3.4), Formula (>=
Suggests: testthat, covr, R.rsp
LinkingTo: Rcpp, RcppEigen
Encoding: UTF-8
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
VignetteBuilder: R.rsp
LazyData: true
Packaged: 2023-09-04 21:57:16 UTC; patrik
Packaged: 2024-07-02 19:11:46 UTC; rstudio
Author: Raluca Gui [cre, aut],
Markus Meierer [aut],
Rene Algesheimer [aut],
Patrik Schilter [aut]
Maintainer: Raluca Gui <raluca.gui@gmail.com>
Repository: CRAN
Date/Publication: 2023-09-05 07:30:02 UTC
Date/Publication: 2024-07-02 21:50:02 UTC
28 changes: 14 additions & 14 deletions MD5
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
a6744b8a2ea5f8f011cdcc2bc80b1646 *DESCRIPTION
f0c77c0a172f33273676feddb4b08440 *DESCRIPTION
74ae9c68aa6e06dfa262cb8cbd338320 *NAMESPACE
9178a23f05299347d87bba646949fda2 *NEWS.md
ea2260a8b2dae4fb791e170bfa155b34 *R/REndo-package.R
8160a68e33d4b1744d6f12c6b3e5d51d *R/RcppExports.R
df63364307cf5c20f05163250318437c *NEWS.md
ffaeeba8cb4bf6ab795ee2c53127b60a *R/REndo-package.R
3556a84df6c71e3ff4ef67123f0089de *R/RcppExports.R
b615ed3e1e97e2bb460a9b9600fdb0e9 *R/data.R
677305aa5999b1be08c0983e5cbdb3b4 *R/f_checkinput_copulacorrection.R
2aa087b260cdcdc5f65f0b86348738c4 *R/f_checkinput_helperfunctions.R
75274467e76f188b93b8a231f5a0a3ed *R/f_checkinput_heterrorsIV.R
068325617b721af8252bdf2f4c8c3678 *R/f_checkinput_highermomentsiv.R
2fd8701234aeff43bd525520ca2095f2 *R/f_checkinput_latentIV.R
fb444151510738bb7df39980813acc73 *R/f_checkinput_multilevel.R
1ee0482a89510fd50cd2fb00d8b8256d *R/f_copulacorrection_interface.R
7c9bb1da2379d3ca56b67f2a7b06c4e5 *R/f_copulacorrection_interface.R
4c3f4e17e353db6531643319b11aa542 *R/f_copulacorrection_linearmodel.R
3594a3e9a84b727b092a0d0e5fa6a82c *R/f_copulacorrection_optimizeLL.R
aa1e105ab0a4615333882d96e41472e0 *R/f_copulacorrectionpstar_continuous.R
5f525f1e73922aa2aafc754f04e70597 *R/f_copulacorrectionpstar_discrete.R
6c655d2371bee756cc9c6ddd2c27592e *R/f_copulacorrection_optimizeLL.R
c53d13b9981e428c5d4545a99c59bc65 *R/f_copulacorrectionpstar_continuous.R
729e1f1cd362ca9fd460109e7dac8a05 *R/f_copulacorrectionpstar_discrete.R
3077763b323ea55046470e7c4ac7b20c *R/f_formula_helpers.R
1febb73ea52be1d77bde2eb19a187b14 *R/f_heterrorsIV.R
bf0aa05d8c037bd00c071e3240ceea35 *R/f_heterrorsIV_IIV.R
Expand All @@ -41,8 +41,8 @@ ad430d30e99a14be8c050a004979288d *R/f_s3_rendobase.R
7f712154a1318f2fa58bdeff373a7719 *R/f_s3_rendoivreg.R
7c938dc06f0aafcdd568c9b797f3ab7b *R/f_s3_rendolatentIV.R
1615dcc353f5119a7811da354dae5b9a *README.md
1961639f6f4f7f9efffa91b624b3d060 *build/partial.rdb
52944adc4ac42453fc520c523d0f3590 *build/vignette.rds
20c8d3f97d93e2e318a22508275b5a02 *build/partial.rdb
5b8a83e533330aa05ccd888f704cb39b *build/vignette.rds
6548bc140d62f6440201bce5da43f00b *data/dataCopCont.rda
fdbaf8c09d9245d45f1830659cde5aef *data/dataCopCont2.rda
1c187a62032b2fd342291b3e77a1e3e6 *data/dataCopDis.rda
Expand All @@ -55,9 +55,9 @@ cf129eb0a5c9a3801237f2107081d783 *data/dataLatentIV.rda
b8efe8a36da92f9d92c289f6fe0e9a5b *inst/CITATION
b498b1f6f3da7483ced7fa52fac5de6b *inst/doc/REndo-introduction.pdf
632c78200b0e5d7d75d9190570468571 *inst/doc/REndo-introduction.pdf.asis
ac1ddf331c259894610080677c181614 *man/REndo.Rd
0606515ce1decec0786abd46b94c63ad *man/REndo.Rd
d4847570028e0b9716ea979cd1987321 *man/confint.rendo.boots.Rd
565cc56427cac6f3ba72fb79872873e6 *man/copulaCorrection.Rd
328e4432a42ddc40d290e95eafa2a9ff *man/copulaCorrection.Rd
71765481a5f8baa657c430e54e699ad1 *man/dataCopCont.Rd
ac5abdd4d632f831e4a9837fac1788d2 *man/dataCopCont2.Rd
88ebe7ed0534eb615cc97b14728d1daa *man/dataCopDis.Rd
Expand All @@ -81,8 +81,8 @@ d91d231ac924d90fdfa064a61ade775a *man/summary.rendo.latent.IV.Rd
f2ca0a168fee046326ef6ede1cef3409 *man/vcov.rendo.boots.Rd
236ff7518dcacf1f5fa87b98a18d1d4e *src/Makevars
236ff7518dcacf1f5fa87b98a18d1d4e *src/Makevars.win
40b415eb1b5f40d27534eab22b407ba8 *src/RcppExports.cpp
6c8225540ae3f2e04dc042a3e3beca7f *src/f_copulacorrection_LL_rcpp.cpp
31c1db1c8210fecb47a4a730a24d6217 *src/RcppExports.cpp
80a9fcf19a8d86a0ef4413b553df1c50 *src/f_copulacorrection_LL_rcpp.cpp
28fec4d88c9eb3e536d606c3b4fb43fa *tests/testthat.R
b141b9c75bdb7fabd4cd6311d9ea2f71 *tests/testthat/dallas2485.csv
3b7821b39df5db31ec1f4901d1c2f912 *tests/testthat/helper_test_confint.R
Expand Down
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
# REndo 2.4.10

### NEW FEATURES
* The Pstar data generated in `copulaCorrection` is improved with the corrections proposed by Qian, Koschmann, and Xie (2024).

### MAINTENANCE
* Fix CRAN notes regarding documentation



# REndo 2.4.9

### MAINTENANCE
Expand Down
5 changes: 2 additions & 3 deletions R/REndo-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' These are statistical techniques to address the endogeneity problem where no external instrumental variables are needed.
#'
#' The main functions to estimate models are:
#' \itemize{
#' \describe{
#' \item{\code{latentIV()}}{the latent instrumental variables method of Ebbes et al. (2005)}
#' \item{\code{copulaCorrection()}}{copula correction method proposed by Paek and Gupta (2012)}
#' \item{\code{hetErrorsIV()}}{heteroskedastic errors approach proposed by Lewbel(2012)}
Expand All @@ -32,13 +32,12 @@
#'
#' @references Gui R, Meierer M, Schilter P, Algesheimer R (2023). “REndo: Internal Instrumental Variables to Address Endogeneity.” Journal of Statistical Software, 107 (3), 1-43. \doi{10.18637/jss.v107.i03}
#'
#' @docType package
#' @aliases REndo-package
#' @name REndo
#'
#' @useDynLib REndo, .registration=TRUE
#' @importFrom Rcpp sourceCpp
#'
NULL
"_PACKAGE"

#@import RcppEigen
4 changes: 2 additions & 2 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

copulaCorrection_LL_rcpp <- function(params, vec_y, m_data_exo_endo, vec_data_endo_pstar, param_pos_data, param_pos_sigma, param_pos_rho) {
.Call(`_REndo_copulaCorrection_LL_rcpp`, params, vec_y, m_data_exo_endo, vec_data_endo_pstar, param_pos_data, param_pos_sigma, param_pos_rho)
copulaCorrection_LL_rcpp <- function(params, vec_y, m_data_exo_endo, vec_data_endo_pstar) {
.Call(`_REndo_copulaCorrection_LL_rcpp`, params, vec_y, m_data_exo_endo, vec_data_endo_pstar)
}

5 changes: 4 additions & 1 deletion R/f_copulacorrection_interface.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' Fits linear models with continuous or discrete endogenous regressors (or a mixture of both) using Gaussian copulas, as presented in Park and Gupta (2012).
#' This is a statistical technique to address the endogeneity problem where no external instrumental variables are needed.
#' The important assumption of the model is that the endogenous variables should NOT be normally distributed, if continuous, preferably with a skewed distribution.
#' The corrections proposed by Qian, Koschmann, and Xie (2024, p.19-22) are implemented. These mitigate the bias of the original paper for small and moderate sample sizes.
#'
#' @template template_param_formuladataverbose
#' @param num.boots Number of bootstrapping iterations. Defaults to 1000.
Expand Down Expand Up @@ -134,8 +135,10 @@
#' @references
#' Park, S. and Gupta, S., (2012), "Handling Endogenous Regressors by Joint Estimation Using Copulas", Marketing Science, 31(4), 567-86.
#'
#' Qian, Y., Koschmann, A., and Xie, H. (2024). "A Practical Guide to Endogeneity Correction Using Copulas". National Bureau of Economic Research, w32231.
#'
#' Epanechnikov V (1969). "Nonparametric Estimation of a Multidimensional Probability Density." Teoriya veroyatnostei i ee primeneniya, 14(1), 156–161.
#
#'
#' Silverman B (1986). "Density Estimation for Statistics and Data Analysis". CRC Monographs on Statistics and Applied Probability. London: Chapman & Hall.
#'
#' Petrin A, Train K (2010). "A Control Function Approach to Endogeneity in Consumer Choice Models." Journal of Marketing Research, 47(1), 3–13.
Expand Down
26 changes: 13 additions & 13 deletions R/f_copulacorrection_optimizeLL.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,19 @@ copulaCorrection_optimizeLL <- function(F.formula, data, name.var.continuous, ve


# Add rho and sigma with defaults
# Same order as model.matrix/formula. This is done in LL again,
# but do here to have consistent output (inputorder to optimx counts for this)
# rho=0 -> rho=0.5 in LL
# sigma=0 -> sigma=1 in LL
# rho=0 -> rho=0.5 in LL
# sigma=0 -> sigma=1 in LL
start.params <- c(start.params, rho=0, sigma=0)
start.params <- start.params[c(names.model.mat, "rho", "sigma")]

# Bring start parameters to correct order
# Parameters are read in the LL by position and therefore have to match the
# coloumns of the data they are multiplied with (`m_data_exo_endo`).
# Also, to have consistent output (inputorder to optimx counts for this).
#
# <model params>: First positions, in same order as data!
# rho: second last position
# sigma: last position
start.params <- start.params[c(colnames(m.model.data.exo.endo), "rho", "sigma")]


# Definition: Optimization function -----------------------------------------------------------------
Expand All @@ -90,10 +97,6 @@ copulaCorrection_optimizeLL <- function(F.formula, data, name.var.continuous, ve
# This implies that the same transformations need to be applied to the found solution
# to report the values that are really used in the LL

param.pos.rho <- which(names(optimx.start.params) == "rho")
param.pos.sigma <- which(names(optimx.start.params) == "sigma")
param.pos.data <- which(!(names(optimx.start.params) %in% c("rho", "sigma")))

optimx.default.args <- list(par = optimx.start.params,
fn = copulaCorrection_LL_rcpp,
method = "Nelder-Mead",
Expand All @@ -104,10 +107,7 @@ copulaCorrection_optimizeLL <- function(F.formula, data, name.var.continuous, ve
dowarn = FALSE),
vec_y = vec.data.y,
m_data_exo_endo = m.model.data.exo.endo,
vec_data_endo_pstar = vec.data.endo.pstar,
param_pos_data = param.pos.data,
param_pos_sigma = param.pos.sigma,
param_pos_rho = param.pos.rho)
vec_data_endo_pstar = vec.data.endo.pstar)

# Update default args with user given args for optimx
optimx.call.args <- modifyList(optimx.default.args, val = optimx.args, keep.null = FALSE)
Expand Down
7 changes: 4 additions & 3 deletions R/f_copulacorrectionpstar_continuous.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ copulaCorrectionContinuous_pstar <- function(vec.data.endo){
# fct.single.col.pstar.continuous <- function(single.col.P){
H.p <- stats::ecdf(vec.data.endo)
U.p <- H.p(vec.data.endo)
n <- length(vec.data.endo)

U.p[U.p == 0] <- 0.0000001
U.p[U.p == 1] <- 0.9999999
U.p[U.p == 0] <- 1 - n/(n+1) #0.0000001
U.p[U.p == 1] <- n/(n+1)#0.9999999

p.star <- stats::qnorm(U.p)
p.star[p.star == 0] <- 0.0000001
p.star[p.star == 0] <- 1 - n/(n+1) #0.0000001
return(p.star)
}
9 changes: 5 additions & 4 deletions R/f_copulacorrectionpstar_discrete.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ copulaCorrectionDiscrete_pstar <- function(vec.data.endo){

# Fit empirical CDF to data
H.p <- ecdf(vec.data.endo)
n <- length(vec.data.endo)

# Caluclate H.pX
H.p1 <- H.p(vec.data.endo)
H.p0 <- H.p(vec.data.endo-1)
H.p0[H.p0 == 0] <- 0.0000001
H.p0[H.p0 == 1] <- 0.9999999
H.p1[H.p1 == 0] <- 0.0000001
H.p1[H.p1 == 1] <- 0.9999999
H.p0[H.p0 == 0] <- 1- n/(n+1) #0.0000001
H.p0[H.p0 == 1] <- n/(n+1) #0.9999999
H.p1[H.p1 == 0] <- 1- n/(n+1) #0.0000001
H.p1[H.p1 == 1] <- n/(n+1) #0.9999999

# Calculate U.p
U.p <- runif(n = length(vec.data.endo), min=H.p0, max=H.p1)
Expand Down
Binary file modified build/partial.rdb
Binary file not shown.
Binary file modified build/vignette.rds
Binary file not shown.
21 changes: 20 additions & 1 deletion man/REndo.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/copulaCorrection.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 4 additions & 7 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,22 @@ Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// copulaCorrection_LL_rcpp
double copulaCorrection_LL_rcpp(const NumericVector& params, const NumericVector& vec_y, const NumericMatrix& m_data_exo_endo, const NumericVector& vec_data_endo_pstar, const IntegerVector& param_pos_data, const int& param_pos_sigma, const int& param_pos_rho);
RcppExport SEXP _REndo_copulaCorrection_LL_rcpp(SEXP paramsSEXP, SEXP vec_ySEXP, SEXP m_data_exo_endoSEXP, SEXP vec_data_endo_pstarSEXP, SEXP param_pos_dataSEXP, SEXP param_pos_sigmaSEXP, SEXP param_pos_rhoSEXP) {
double copulaCorrection_LL_rcpp(const NumericVector& params, const NumericVector& vec_y, const NumericMatrix& m_data_exo_endo, const NumericVector& vec_data_endo_pstar);
RcppExport SEXP _REndo_copulaCorrection_LL_rcpp(SEXP paramsSEXP, SEXP vec_ySEXP, SEXP m_data_exo_endoSEXP, SEXP vec_data_endo_pstarSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< const NumericVector& >::type params(paramsSEXP);
Rcpp::traits::input_parameter< const NumericVector& >::type vec_y(vec_ySEXP);
Rcpp::traits::input_parameter< const NumericMatrix& >::type m_data_exo_endo(m_data_exo_endoSEXP);
Rcpp::traits::input_parameter< const NumericVector& >::type vec_data_endo_pstar(vec_data_endo_pstarSEXP);
Rcpp::traits::input_parameter< const IntegerVector& >::type param_pos_data(param_pos_dataSEXP);
Rcpp::traits::input_parameter< const int& >::type param_pos_sigma(param_pos_sigmaSEXP);
Rcpp::traits::input_parameter< const int& >::type param_pos_rho(param_pos_rhoSEXP);
rcpp_result_gen = Rcpp::wrap(copulaCorrection_LL_rcpp(params, vec_y, m_data_exo_endo, vec_data_endo_pstar, param_pos_data, param_pos_sigma, param_pos_rho));
rcpp_result_gen = Rcpp::wrap(copulaCorrection_LL_rcpp(params, vec_y, m_data_exo_endo, vec_data_endo_pstar));
return rcpp_result_gen;
END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_REndo_copulaCorrection_LL_rcpp", (DL_FUNC) &_REndo_copulaCorrection_LL_rcpp, 7},
{"_REndo_copulaCorrection_LL_rcpp", (DL_FUNC) &_REndo_copulaCorrection_LL_rcpp, 4},
{NULL, NULL, 0}
};

Expand Down
17 changes: 6 additions & 11 deletions src/f_copulacorrection_LL_rcpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ using namespace Rcpp;
double copulaCorrection_LL_rcpp(const NumericVector& params,
const NumericVector& vec_y,
const NumericMatrix& m_data_exo_endo,
const NumericVector& vec_data_endo_pstar,
const IntegerVector& param_pos_data,
const int& param_pos_sigma,
const int& param_pos_rho){
const NumericVector& vec_data_endo_pstar){

// # Extract params from optimx inputs --------------------------------------------------------
NumericVector params_endo_exo = params[param_pos_data - 1];
double sigma = params[param_pos_sigma - 1];
double rho = params[param_pos_rho - 1];
// Positions based on `start.params[c(colnames(m.model.data.exo.endo), "rho", "sigma")]`
const auto n_params = params.size();
const NumericVector params_endo_exo = params[Rcpp::Range(0, n_params-3)]; // everything from start until (excl) 2nd last element
double rho = params[n_params - 2]; // second last element
double sigma = params[n_params - 1]; // last element

// # Constrain rho to [-1,1] and sigma to [0, +Inf]
// # (incl bound because can be very large which flips to 0 and 1)
Expand All @@ -30,10 +29,6 @@ double copulaCorrection_LL_rcpp(const NumericVector& params,
sigma = exp(sigma);

// # epsilon, incl. endo regressor ------------------------------------------------------------
// # Reorder params to fit matrix col order
CharacterVector m_colnames = colnames(m_data_exo_endo);
params_endo_exo = params_endo_exo[m_colnames];


// Short excursion to RcppEigen: Matrix multiplication
const Eigen::Map<Eigen::MatrixXd> A = Rcpp::as<Eigen::Map<Eigen::MatrixXd> >(m_data_exo_endo);
Expand Down

0 comments on commit 390f40a

Please sign in to comment.