Skip to content

Commit

Permalink
Merge pull request #186 from DeclareDesign/ls/coefs
Browse files Browse the repository at this point in the history
estimate -> coefficients in model object
  • Loading branch information
lukesonnet committed Mar 20, 2018
2 parents c7540b6 + 5519232 commit 1b7e588
Show file tree
Hide file tree
Showing 31 changed files with 153 additions and 164 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Suggests:
RcppEigen,
fabricatr (>= 0.3),
randomizr (>= 0.9),
margins,
prediction,
AER
Enhances:
broom,
Expand Down
5 changes: 0 additions & 5 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
# 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: 0 additions & 18 deletions R/S3_coef.R

This file was deleted.

2 changes: 1 addition & 1 deletion R/S3_print.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ print_summary_lm_like <- function(x,
cat("\nCoefficients:\n")
}

print(x$coefficients, digits = digits)
print(coef(x), digits = digits)

if (!is.null(x$fstatistic)) {
cat(
Expand Down
6 changes: 3 additions & 3 deletions R/S3_summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
summary.lm_robust <- function(object,
...) {

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

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

mat_objs <- c(
"estimate",
"coefficients",
"std.error",
"df",
"ci.lower",
Expand Down
3 changes: 2 additions & 1 deletion R/S3_tidy.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ tidy_data_frame <- function(object, digits = NULL) {

vec_cols <-
c(
"estimate",
"coefficients",
"std.error",
"p.value",
"ci.lower",
Expand All @@ -95,6 +95,7 @@ tidy_data_frame <- function(object, digits = NULL) {
)

tidy_mat <- do.call("cbind", lapply(vec_cols, function(x) {as.vector(object[[x]])}))
vec_cols[which(vec_cols == "coefficients")] <- "estimate"
colnames(tidy_mat) <- vec_cols
return_frame <- data.frame(
term = object[["term"]],
Expand Down
18 changes: 9 additions & 9 deletions R/estimatr_difference_in_means.R
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
#'
#' An object of class \code{"difference_in_means"} is a list containing at
#' least the following components:
#' \item{estimate}{the estimated difference in means}
#' \item{coefficients}{the estimated difference in means}
#' \item{std.error}{the estimated standard error}
#' \item{df}{the estimated degrees of freedom}
#' \item{p.value}{the p-value from a two-sided t-test using \code{estimate}, \code{std.error}, and \code{df}}
#' \item{p.value}{the p-value from a two-sided t-test using \code{coefficients}, \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}
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(estimate * N / N_overall))
diff <- with(block_estimates, sum(coefficients * N / N_overall))

df <- NA
n_blocks <- nrow(block_estimates)
Expand All @@ -332,7 +332,7 @@ difference_in_means <-
std.error <-
with(
block_estimates,
sqrt((1 / (n_blocks * (n_blocks - 1))) * sum((estimate - diff) ^ 2))
sqrt((1 / (n_blocks * (n_blocks - 1))) * sum((coefficients - diff) ^ 2))
)
} else {
design <- "Matched-pair clustered"
Expand All @@ -342,7 +342,7 @@ difference_in_means <-
block_estimates,
sqrt(
(n_blocks / ((n_blocks - 1) * N_overall ^ 2)) *
sum((N * estimate - (N_overall * diff) / n_blocks) ^ 2)
sum((N * coefficients - (N_overall * diff) / n_blocks) ^ 2)
)
)
}
Expand All @@ -367,7 +367,7 @@ difference_in_means <-
}

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

diff <- cr2_out$estimate[2]
diff <- coef(cr2_out)[2]
std.error <- cr2_out$std.error[2]
df <- cr2_out$df[2]
} else {
Expand Down Expand Up @@ -513,15 +513,15 @@ difference_in_means_internal <-
has_int = TRUE
)

diff <- w_hc2_out$estimate[2]
diff <- coef(w_hc2_out)[2]
std.error <- w_hc2_out$std.error[2]
df <- w_hc2_out$df[2]
}
}

return_frame <-
data.frame(
estimate = diff,
coefficients = diff,
std.error = std.error,
df = df,
stringsAsFactors = FALSE
Expand Down
10 changes: 5 additions & 5 deletions R/estimatr_horvitz_thompson.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@
#' An object of class \code{"horvitz_thompson"} is a list containing at
#' least the following components:
#'
#' \item{estimate}{the estimated difference in totals}
#' \item{coefficients}{the estimated difference in totals}
#' \item{std.error}{the estimated standard error}
#' \item{df}{the estimated degrees of freedom}
#' \item{p.value}{the p-value from a two-sided z-test using \code{estimate} and \code{std.error}}
#' \item{p.value}{the p-value from a two-sided z-test using \code{coefficients} 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}
Expand Down Expand Up @@ -545,12 +545,12 @@ horvitz_thompson <-

n_blocks <- nrow(block_estimates)

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

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

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

return_frame <-
data.frame(
estimate = diff,
coefficients = diff,
std.error = std.error,
N = N,
stringsAsFactors = FALSE
Expand Down
4 changes: 2 additions & 2 deletions R/estimatr_iv_robust.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
#'
#' An object of class \code{"iv_robust"} is a list containing at least the
#' following components:
#' \item{estimate}{the estimated coefficients}
#' \item{coefficients}{the estimated coefficients}
#' \item{std.error}{the estimated standard errors}
#' \item{df}{the estimated degrees of freedom}
#' \item{p.value}{the p-values from a two-sided t-test using \code{estimate}, \code{std.error}, and \code{df}}
#' \item{p.value}{the p-values from a two-sided t-test using \code{coefficients}, \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}
Expand Down
8 changes: 5 additions & 3 deletions R/estimatr_lm_lin.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,19 @@
#' return results in a \code{data.frame}. To get useful data out of the return,
#' you can use these data frames, you can use the resulting list directly, or
#' you can use the generic accessor functions \code{coef}, \code{vcov},
#' \code{confint}, and \code{predict}.
#' \code{confint}, and \code{predict}. Marginal effects and uncertainty about
#' them can be gotten by passing this object to
#' \code{\link[margins]{margins}} from the \pkg{margins}.
#'
#' Users who want to print the results in TeX of HTML can use the
#' \code{\link[texreg]{extract}} function and the \pkg{texreg} package.
#'
#' An object of class \code{"lm_robust"} is a list containing at least the
#' following components:
#' \item{estimate}{the estimated coefficients}
#' \item{coefficients}{the estimated coefficients}
#' \item{std.error}{the estimated standard errors}
#' \item{df}{the estimated degrees of freedom}
#' \item{p.value}{the p-values from a two-sided t-test using \code{estimate}, \code{std.error}, and \code{df}}
#' \item{p.value}{the p-values from a two-sided t-test using \code{coefficients}, \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}
Expand Down
10 changes: 6 additions & 4 deletions R/estimatr_lm_robust.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
#' return results in a \code{data.frame}. To get useful data out of the return,
#' you can use these data frames, you can use the resulting list directly, or
#' you can use the generic accessor functions \code{coef}, \code{vcov},
#' \code{confint}, and \code{predict}.
#' \code{confint}, and \code{predict}. Marginal effects and uncertainty about
#' them can be gotten by passing this object to
#' \code{\link[margins]{margins}} from the \pkg{margins}.
#'
#' Users who want to print the results in TeX of HTML can use the
#' \code{\link[texreg]{extract}} function and the \pkg{texreg} package.
Expand All @@ -71,10 +73,10 @@
#'
#' An object of class \code{"lm_robust"} is a list containing at least the
#' following components:
#' \item{estimate}{the estimated coefficients}
#' \item{coefficients}{the estimated coefficients}
#' \item{std.error}{the estimated standard errors}
#' \item{df}{the estimated degrees of freedom}
#' \item{p.value}{the p-values from a two-sided t-test using \code{estimate}, \code{std.error}, and \code{df}}
#' \item{p.value}{the p-values from a two-sided t-test using \code{coefficients}, \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}
Expand Down Expand Up @@ -122,7 +124,7 @@
#' # Can use summary() to get more statistics
#' summary(lmro)
#' # Can also get coefficients three ways
#' lmro$estimate
#' lmro$coefficients
#' coef(lmro)
#' tidy(lmro)$estimate
#' # Can also get confidence intervals from object or with new 1 - `alpha`
Expand Down
8 changes: 4 additions & 4 deletions R/helper_cis_pvals.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ add_cis_pvals <- function(return_frame, alpha, ci, ttest = TRUE) {

return_frame$p.value <- with(
return_frame,
2 * pt(abs(estimate / std.error), df = df, lower.tail = FALSE)
2 * pt(abs(coefficients / std.error), df = df, lower.tail = FALSE)
)

crit_se <- with(return_frame, qt(1 - alpha / 2, df = df) * std.error)
} else {
return_frame$p.value <- with(
return_frame,
2 * pnorm(abs(estimate / std.error), lower.tail = FALSE)
2 * pnorm(abs(coefficients / std.error), lower.tail = FALSE)
)

crit_se <- with(return_frame, qnorm(1 - alpha / 2) * std.error)

return_frame$df <- NA
}

return_frame$ci.lower <- with(return_frame, estimate - crit_se)
return_frame$ci.upper <- with(return_frame, estimate + crit_se)
return_frame$ci.lower <- with(return_frame, coefficients - crit_se)
return_frame$ci.upper <- with(return_frame, coefficients + crit_se)

return(as.list(return_frame))
} else {
Expand Down
10 changes: 5 additions & 5 deletions R/helper_extract.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ extract.lm_robust <- function(model, include.ci = TRUE, include.rsquared = TRUE,
s <- summary(model, ...)

names <- rownames(s$coefficients)
co <- s$coefficients[, 1]
se <- s$coefficients[, 2]
pval <- s$coefficients[, 3]
co <- coef(s)[, 1]
se <- coef(s)[, 2]
pval <- coef(s)[, 3]
cilow <- numeric()
ciupper <- numeric()
if (include.ci) {
cilow <- s$coefficients[, 4]
ciupper <- s$coefficients[, 5]
cilow <- coef(s)[, 4]
ciupper <- coef(s)[, 5]
}

rs <- s$r.squared #extract R-squared
Expand Down
4 changes: 2 additions & 2 deletions R/helper_lm_robust_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,13 @@ lm_robust_fit <- function(y,

if (multivariate) {
return_list <- list(
estimate = fit$beta_hat,
coefficients = fit$beta_hat,
std.error = matrix(NA, k, ny),
df = matrix(NA, k, ny)
)
} else {
return_list <- list(
estimate = setNames(as.vector(fit$beta_hat), variable_names),
coefficients = setNames(as.vector(fit$beta_hat), variable_names),
std.error = NA,
df = NA
)
Expand Down
2 changes: 1 addition & 1 deletion R/helper_return.R
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ dim_like_return <- function(return_list, alpha, formula, conditions) {

return_list[["outcome"]] <- deparse(formula[[2]], nlines = 5)

names(return_list[["estimate"]]) <- return_list[["term"]]
names(return_list[["coefficients"]]) <- return_list[["term"]]

return(return_list)
}
4 changes: 2 additions & 2 deletions man/difference_in_means.Rd

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

4 changes: 2 additions & 2 deletions man/horvitz_thompson.Rd

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

4 changes: 2 additions & 2 deletions man/iv_robust.Rd

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

Loading

0 comments on commit 1b7e588

Please sign in to comment.