Skip to content

Commit

Permalink
Undo AsIs when sorting running variable
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesarm committed Jun 18, 2021
1 parent 711d70f commit 1a51291
Show file tree
Hide file tree
Showing 12 changed files with 58 additions and 30 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: R
sudo: false
language: r
os: linux
cache: packages
addons:
apt:
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: RDHonest
Title: Honest inference in regression discontinuity designs
Title: Honest Inference in Regression Discontinuity Designs
Version: 0.3.2
Authors@R: person("Michal", "Kolesar",
email = "[email protected]", role = c("aut", "cre"))
Expand All @@ -21,7 +21,7 @@ Suggests:
rmarkdown,
Formula,
formatR
RoxygenNote: 7.0.2
RoxygenNote: 7.1.1
URL: https://github.com/kolesarm/RDHonest
VignetteBuilder: knitr
Language: en-US
Expand Down
6 changes: 1 addition & 5 deletions R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ plot_RDscatter <- function(d, avg=10, xlab=NULL, ylab=NULL,
p <- ggplot2::qplot(x=x, y=y, data=bd)
}

p <- p + ggplot2::theme_classic() +
ggplot2::theme(legend.position = "none", axis.line.x=ggplot2::
element_line(color="black", size=0.2, linetype="solid"),
axis.line.y=ggplot2::
element_line(color="black", size=0.2, linetype="solid"))
p <- p + ggplot2::theme(legend.position = "none")
if(!is.null(xlab)) p <- p + ggplot2::xlab(xlab)
if(!is.null(ylab)) p <- p + ggplot2::ylab(ylab)
if(vert) p <- p + ggplot2::geom_vline(xintercept=d$orig.cutoff,
Expand Down
18 changes: 14 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
## Get rid of AsIs class attribute for making sort work
unAsIs <- function(X) {
if("AsIs" %in% class(X)) {
class(X) <- class(X)[-match("AsIs", class(X))]
}
X
}

#' Class Constructor for \code{"RDData"}
#'
#' Convert data to a standardized format for use with low-level functions. If
Expand Down Expand Up @@ -47,8 +55,9 @@
#' @export
RDData <- function(d, cutoff) {

## Unclass in case generated by I()
if(is.unsorted(d[[2]]))
d <- d[sort(d[[2]], index.return=TRUE)$ix, ]
d <- d[sort(unAsIs(d[[2]]), index.return=TRUE)$ix, ]

X <- d[[2]] - cutoff
df <- list(Ym=d[[1]][X<0], Yp=d[[1]][X>=0], Xm=X[X<0], Xp=X[X>=0],
Expand Down Expand Up @@ -117,8 +126,9 @@ RDData <- function(d, cutoff) {
#' d <- FRDData(cbind(logcn=log(rcp[, 6]), rcp[, c(3, 2)]), cutoff=0)
#' @export
FRDData <- function(d, cutoff) {
## Unclass in case generated by I()
if(is.unsorted(d[[3]]))
d <- d[sort(d[[3]], index.return=TRUE)$ix, ]
d <- d[sort(unAsIs(d[[3]]), index.return=TRUE)$ix, ]
X <- d[[3]] - cutoff
df <- list(Ym=cbind(d[[1]][X<0], d[[2]][X<0]),
Yp=cbind(d[[1]][X>=0], d[[2]][X>=0]),
Expand Down Expand Up @@ -175,9 +185,9 @@ FRDData <- function(d, cutoff) {
#' d2 <- LPPData(lee08, point=50)
#' @export
LPPData <- function(d, point) {

## Unclass in case generated by I()
if(is.unsorted(d[[2]]))
d <- d[sort(d[[2]], index.return=TRUE)$ix, ]
d <- d[sort(unAsIs(d[[2]]), index.return=TRUE)$ix, ]

df <- list(Y=d[[1]], X=d[[2]] - point,
orig.point=point, var.names=names(d)[1:2])
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![Travis build status](https://travis-ci.org/kolesarm/RDHonest.svg?branch=master)](https://travis-ci.org/kolesarm/RDHonest) [![Coverage status](https://codecov.io/gh/kolesarm/RDHonest/branch/master/graph/badge.svg)](https://codecov.io/github/kolesarm/RDHonest?branch=master)
[![Travis build status](https://travis-ci.com/kolesarm/RDHonest.svg?branch=master)](https://travis-ci.com/kolesarm/RDHonest) [![Coverage status](https://codecov.io/gh/kolesarm/RDHonest/branch/master/graph/badge.svg)](https://codecov.io/github/kolesarm/RDHonest?branch=master)

# RDHonest

Expand All @@ -21,10 +21,11 @@ Foundation under grant numbers SES-1628939 (Armstrong) and SES-1628878

## Installation

You can install the current development version of the package by downloading
the source code here, or using the `install_github()` function:
you can get the current development version from GitHub:

```
install.packages("remotes") # if not installed
``` r
if (!requireNamespace("remotes")) {
install.packages("remotes")
}
remotes::install_github("kolesarm/RDHonest")
```
6 changes: 4 additions & 2 deletions man/cghs.Rd

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

6 changes: 4 additions & 2 deletions man/headst.Rd

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

6 changes: 4 additions & 2 deletions man/kernC.Rd

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

6 changes: 4 additions & 2 deletions man/lee08.Rd

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

6 changes: 4 additions & 2 deletions man/rcp.Rd

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

6 changes: 4 additions & 2 deletions man/rebp.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test_rd.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ test_that("Test class constructor sorting", {
d1 <- LPPData(rcp[sort(rcp$elig_year,
index.return=TRUE)$ix, c(6, 2)], point=-3)
expect_identical(d1, d0)

## Now test that I() works
expect_equal(RDHonest(voteshare ~ margin, data=lee08, M=0, h=2)$estimate,
-RDHonest(voteshare ~ I(-margin),
data=lee08, M=0, h=2)$estimate)
expect_equal(FRDHonest(cn~retired | elig_year, data=rcp, cutoff=0,
M=c(1, 0.1), h=3)$estimate,
FRDHonest(cn~retired | I(2*elig_year), data=rcp, cutoff=0,
M=c(1, 0.1), h=6)$estimate)
})

test_that("IK bandwidth calculations", {
Expand Down

0 comments on commit 1a51291

Please sign in to comment.