Skip to content

Commit

Permalink
Merge pull request #182 from DeclareDesign/ls/list_names
Browse files Browse the repository at this point in the history
Rename all list output to match tidy; remove any mention of margins
  • Loading branch information
lukesonnet committed Mar 20, 2018
2 parents 4f9202a + 1daa185 commit c7540b6
Show file tree
Hide file tree
Showing 33 changed files with 309 additions and 292 deletions.
2 changes: 1 addition & 1 deletion .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ update_repo.R
^_pkgdown\.yml$
^tests/testthat/mtcars\.csv$
^tests/testthat/run-stata-models\.do$
^tests/testthat/test-broom\.R$
^tests/testthat/test-zzzzzzbroom\.R$
^estimatr_.*\.tar\.gz$
^tests/testthat/test-texreg\.R$
^cran-comments\.md$
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ Suggests:
RcppEigen,
fabricatr (>= 0.3),
randomizr (>= 0.9),
margins,
prediction,
AER
Enhances: broom, texreg
Enhances:
broom,
texreg
Additional_Repositories: http://r.declaredesign.org
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Generated by roxygen2: do not edit by hand

S3method(coef,difference_in_means)
S3method(coef,horvitz_thompson)
S3method(coef,iv_robust)
S3method(coef,lm_lin)
S3method(coef,lm_robust)
S3method(confint,difference_in_means)
S3method(confint,horvitz_thompson)
S3method(confint,iv_robust)
Expand Down
18 changes: 18 additions & 0 deletions R/S3_coef.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
coef_estimatr <- function(object, ...) {
object$estimate
}

#' @export
coef.lm_robust <- coef_estimatr

#' @export
coef.iv_robust <- coef_estimatr

#' @export
coef.lm_lin <- coef_estimatr

#' @export
coef.horvitz_thompson <- coef_estimatr

#' @export
coef.difference_in_means <- coef_estimatr
14 changes: 7 additions & 7 deletions R/S3_confint.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,20 @@ get_ci_mat <- function(object, level, ttest = TRUE) {
}

cis <- cbind(
as.vector(object$ci_lower),
as.vector(object$ci_upper)
as.vector(object$ci.lower),
as.vector(object$ci.upper)
)

if (is.matrix(object$ci_lower)) {
ny <- ncol(object$ci_lower)
p <- nrow(object$ci_lower)
if (is.matrix(object$ci.lower)) {
ny <- ncol(object$ci.lower)
p <- nrow(object$ci.lower)
rownames(cis) <- paste0(
rep(object$outcome, each = p),
":",
rep(object$coefficient_name, times = ny)
rep(object$term, times = ny)
)
} else {
rownames(cis) <- object$coefficient_name
rownames(cis) <- object$term
}

colnames(cis) <- paste((1 - level) / 2 * c(100, -100) + c(0, 100), "%")
Expand Down
20 changes: 10 additions & 10 deletions R/S3_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
summary.lm_robust <- function(object,
...) {

if (is.matrix(object$coefficients)) {
ny <- ncol(object$coefficients)
if (is.matrix(object$estimate)) {
ny <- ncol(object$estimate)

ret <- setNames(
vector("list", ny),
paste("Response", object$outcome)
)

mat_objs <- c(
"coefficients",
"se",
"estimate",
"std.error",
"df",
"ci_lower",
"ci_upper",
"p"
"ci.lower",
"ci.upper",
"p.value"
)

vec_objs <- c(
Expand Down Expand Up @@ -119,15 +119,15 @@ summarize_tidy <- function(object, test = "t", ...) {
tidy_mat <- as.matrix(tidy_out[, !(names(tidy_out) %in% remove_cols)])

ny <- length(object$outcome)
p <- length(object$coefficient_name)
p <- length(object$term)
if (length(object$outcome) > 1) {
rownames(tidy_mat) <- paste0(
rep(object$outcome, each = p),
":",
rep(object$coefficient_name, times = ny)
rep(object$term, times = ny)
)
} else {
rownames(tidy_mat) <- object$coefficient_name
rownames(tidy_mat) <- object$term
}

return(tidy_mat)
Expand Down
16 changes: 3 additions & 13 deletions R/S3_tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ tidy.horvitz_thompson <- function(object, ...) {
tidy_data_frame <- function(object, digits = NULL) {

vec_cols <-
c(
"coefficients",
"se",
"p",
"ci_lower",
"ci_upper",
"df"
)

tidy_names <-
c(
"estimate",
"std.error",
Expand All @@ -105,14 +95,14 @@ tidy_data_frame <- function(object, digits = NULL) {
)

tidy_mat <- do.call("cbind", lapply(vec_cols, function(x) {as.vector(object[[x]])}))
colnames(tidy_mat) <- tidy_names
colnames(tidy_mat) <- vec_cols
return_frame <- data.frame(
term = object[["coefficient_name"]],
term = object[["term"]],
tidy_mat,
stringsAsFactors = FALSE
)

return_frame$outcome <- rep(object[["outcome"]], each = length(object[["coefficient_name"]]))
return_frame$outcome <- rep(object[["outcome"]], each = length(object[["term"]]))

rownames(return_frame) <- NULL
return(return_frame)
Expand Down
46 changes: 23 additions & 23 deletions R/estimatr_difference_in_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@
#'
#' An object of class \code{"difference_in_means"} is a list containing at
#' least the following components:
#' \item{coefficients}{the estimated coefficients}
#' \item{se}{the estimated standard errors}
#' \item{estimate}{the estimated difference in means}
#' \item{std.error}{the estimated standard error}
#' \item{df}{the estimated degrees of freedom}
#' \item{p}{the p-values from a two-sided t-test using \code{coefficients}, \code{se}, and \code{df}}
#' \item{ci_lower}{the lower bound of the \code{1 - alpha} percent confidence interval}
#' \item{ci_upper}{the upper bound of the \code{1 - alpha} percent confidence interval}
#' \item{coefficient_name}{a character vector of coefficient names}
#' \item{p.value}{the p-value from a two-sided t-test using \code{estimate}, \code{std.error}, and \code{df}}
#' \item{ci.lower}{the lower bound of the \code{1 - alpha} percent confidence interval}
#' \item{ci.upper}{the upper bound of the \code{1 - alpha} percent confidence interval}
#' \item{term}{a character vector of coefficient names}
#' \item{alpha}{the significance level specified by the user}
#' \item{N}{the number of observations used}
#' \item{outcome}{the name of the outcome variable}
Expand Down Expand Up @@ -320,7 +320,7 @@ difference_in_means <-
N_overall <- with(block_estimates, sum(N))

# Blocked design, (Gerber Green 2012, p73, eq3.10)
diff <- with(block_estimates, sum(coefficients * N / N_overall))
diff <- with(block_estimates, sum(estimate * N / N_overall))

df <- NA
n_blocks <- nrow(block_estimates)
Expand All @@ -329,20 +329,20 @@ difference_in_means <-
if (is.null(data$cluster)) {
design <- "Matched-pair"
# Pair matched, unit randomized (Gerber Green 2012, p77, eq3.16)
se <-
std.error <-
with(
block_estimates,
sqrt((1 / (n_blocks * (n_blocks - 1))) * sum((coefficients - diff) ^ 2))
sqrt((1 / (n_blocks * (n_blocks - 1))) * sum((estimate - diff) ^ 2))
)
} else {
design <- "Matched-pair clustered"
# Pair matched, cluster randomized (Imai, King, Nall 2009, p36, eq6)
se <-
std.error <-
with(
block_estimates,
sqrt(
(n_blocks / ((n_blocks - 1) * N_overall ^ 2)) *
sum((N * coefficients - (N_overall * diff) / n_blocks) ^ 2)
sum((N * estimate - (N_overall * diff) / n_blocks) ^ 2)
)
)
}
Expand All @@ -351,7 +351,7 @@ difference_in_means <-
df <- n_blocks - 1
} else {
# Block randomized (Gerber and Green 2012, p. 74, footnote 17)
se <- with(block_estimates, sqrt(sum(se ^ 2 * (N / N_overall) ^ 2)))
std.error <- with(block_estimates, sqrt(sum(std.error ^ 2 * (N / N_overall) ^ 2)))


## we don't know if this is correct!
Expand All @@ -367,8 +367,8 @@ difference_in_means <-
}

return_frame <- data.frame(
coefficients = diff,
se = se,
estimate = diff,
std.error = std.error,
df = df,
N = N_overall,
stringsAsFactors = FALSE
Expand Down Expand Up @@ -465,24 +465,24 @@ difference_in_means_internal <-
has_int = TRUE
)

diff <- cr2_out$coefficients[2]
se <- cr2_out$se[2]
diff <- cr2_out$estimate[2]
std.error <- cr2_out$std.error[2]
df <- cr2_out$df[2]
} else {
if (is.null(data$weights)) {
diff <- mean(Y2) - mean(Y1)

if (pair_matched) {
# Pair matched designs
se <- NA
std.error <- NA
} else {
# Non-pair matched designs, unit level randomization
var_Y2 <- var(Y2)
var_Y1 <- var(Y1)

se <- sqrt(var_Y2 / N2 + var_Y1 / N1)
std.error <- sqrt(var_Y2 / N2 + var_Y1 / N1)

df <- se ^ 4 /
df <- std.error ^ 4 /
(
(var_Y2 / N2) ^ 2 / (N2 - 1) +
(var_Y1 / N1) ^ 2 / (N1 - 1)
Expand Down Expand Up @@ -513,16 +513,16 @@ difference_in_means_internal <-
has_int = TRUE
)

diff <- w_hc2_out$coefficients[2]
se <- w_hc2_out$se[2]
diff <- w_hc2_out$estimate[2]
std.error <- w_hc2_out$std.error[2]
df <- w_hc2_out$df[2]
}
}

return_frame <-
data.frame(
coefficients = diff,
se = se,
estimate = diff,
std.error = std.error,
df = df,
stringsAsFactors = FALSE
)
Expand Down
38 changes: 19 additions & 19 deletions R/estimatr_horvitz_thompson.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@
#' An object of class \code{"horvitz_thompson"} is a list containing at
#' least the following components:
#'
#' \item{coefficients}{the estimated coefficients}
#' \item{se}{the estimated standard errors}
#' \item{estimate}{the estimated difference in totals}
#' \item{std.error}{the estimated standard error}
#' \item{df}{the estimated degrees of freedom}
#' \item{p}{the p-values from from a two-sided z-test using \code{coefficients}, \code{se}, and \code{df}}
#' \item{ci_lower}{the lower bound of the \code{1 - alpha} percent confidence interval}
#' \item{ci_upper}{the upper bound of the \code{1 - alpha} percent confidence interval}
#' \item{coefficient_name}{a character vector of coefficient names}
#' \item{p.value}{the p-value from a two-sided z-test using \code{estimate} and \code{std.error}}
#' \item{ci.lower}{the lower bound of the \code{1 - alpha} percent confidence interval}
#' \item{ci.upper}{the upper bound of the \code{1 - alpha} percent confidence interval}
#' \item{term}{a character vector of coefficient names}
#' \item{alpha}{the significance level specified by the user}
#' \item{N}{the number of observations used}
#' \item{outcome}{the name of the outcome variable}
Expand Down Expand Up @@ -545,13 +545,13 @@ horvitz_thompson <-

n_blocks <- nrow(block_estimates)

diff <- with(block_estimates, sum(coefficients * N / N_overall))
diff <- with(block_estimates, sum(estimate * N / N_overall))

se <- with(block_estimates, sqrt(sum(se ^ 2 * (N / N_overall) ^ 2)))
std.error <- with(block_estimates, sqrt(sum(std.error ^ 2 * (N / N_overall) ^ 2)))

return_frame <- data.frame(
coefficients = diff,
se = se,
estimate = diff,
std.error = std.error,
N = N_overall
)
}
Expand Down Expand Up @@ -601,7 +601,7 @@ horvitz_thompson_internal <-

N <- length(t2) + length(t1)

se <- NA
std.error <- NA

collapsed <- !is.null(data$clusters)
if (collapsed) {
Expand Down Expand Up @@ -639,7 +639,7 @@ horvitz_thompson_internal <-

diff <- (sum(Y2) - sum(Y1)) / N

se <-
std.error <-
sqrt(
sum(Y2 ^ 2) +
sum(Y1 ^ 2) +
Expand Down Expand Up @@ -687,7 +687,7 @@ horvitz_thompson_internal <-
)


se <-
std.error <-
sqrt(
var_ht_total_no_cov(y1, data$condition_probabilities) +
var_ht_total_no_cov(y0, 1 - data$condition_probabilities) +
Expand All @@ -696,7 +696,7 @@ horvitz_thompson_internal <-
) / N
} else {
# Young's inequality
se <-
std.error <-
sqrt(sum(Y2 ^ 2) + sum(Y1 ^ 2)) / N
}
} else {
Expand Down Expand Up @@ -733,24 +733,24 @@ horvitz_thompson_internal <-
if (!is.nan(varN2)) {
if (varN2 < 0) {
warning("Variance below 0")
se <- NA
std.error <- NA
} else {
se <- sqrt(varN2) / N
std.error <- sqrt(varN2) / N
}
} else {
warning(
"Variance is NaN. This is likely the result of a complex condition probability matrix"
)
se <- NA
std.error <- NA
}
}
}
}

return_frame <-
data.frame(
coefficients = diff,
se = se,
estimate = diff,
std.error = std.error,
N = N,
stringsAsFactors = FALSE
)
Expand Down
Loading

0 comments on commit c7540b6

Please sign in to comment.