Skip to content

Commit

Permalink
fix formulae with spaces bug (closes #80)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeper committed Jan 10, 2018
1 parent 5eaf510 commit 9cffafb
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Title: Marginal Effects for Model Objects
Description: An R port of Stata's 'margins' command, which can be used to
calculate marginal (or partial) effects from model objects.
License: MIT + file LICENSE
Version: 0.3.8
Version: 0.3.9
Date: 2018-01-10
Authors@R: c(person("Thomas J.", "Leeper",
role = c("aut", "cre"),
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
YEAR: 2014-2017
YEAR: 2014-2018
COPYRIGHT HOLDER: Thomas J. Leeper
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## margins 0.3.9

* Fixed a bug wherein model formulae involving non-standard variables names with spaces in them led to errors. (#80)

## margins 0.3.8

* Added method for "svyglm" from **survey**.
Expand Down
2 changes: 1 addition & 1 deletion R/find_terms_in_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ find_terms_in_model <- function(model, variables = NULL) {
# handle character variables as factors
classes[classes == "character"] <- "factor"
## cleanup names of terms
terms2 <- sapply(names(classes), function(x) all.vars(parse(text = x)))
terms2 <- sapply(names(classes), function(x) all.vars(parse(text = paste0("`", x, "`"))))
names(classes)[names(classes) %in% names(terms2)] <- terms2[names(classes) %in% names(terms2)]

# identify factors versus numeric terms in `model`
Expand Down
8 changes: 4 additions & 4 deletions R/formulae.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# function to cleanup I(), etc. in formulas
gsub_bracket <- function(a, b) {
tmp <- regmatches(a, gregexpr(paste0("(",b,"\\().+(\\))"), a))
regmatches(a, gregexpr(paste0("(",b,"\\().+(\\))"), a)) <-
gsub(")$","", gsub(paste0("^",b,"\\("), "", tmp))
regmatches(a, gregexpr(paste0("(",b,"\\().+(\\))"), a)) <- gsub(")$","", gsub(paste0("^",b,"\\("), "", tmp))
a
}

# function to drop multipliers, powers, etc.
drop_operators <- function(a, dropdigits = TRUE) {
a <- gsub(" ","",a)
# drop leading or trailing spaces ?
a <- gsub(" +$", "", gsub("^ +","",a))
# remove mathematical operators
if(dropdigits) {
a <- gsub("^[:digit:]+(\\^|\\+|\\-|\\*|\\|/|,)", "", a)
Expand All @@ -27,7 +27,7 @@ drop_operators <- function(a, dropdigits = TRUE) {
a
}

# call sub_bracket on all common formula operations
# call gsub_bracket on all common formula operations
clean_terms <- function(terms) {
v <- gsub_bracket(terms, "factor")
v <- gsub_bracket(v, "ordered")
Expand Down
6 changes: 3 additions & 3 deletions R/print.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ function(x, digits = 4, order = NULL, ...) {
tmp <- marginal_effects(x, with_at = FALSE)
names(tmp) <- gsub("^dydx_", "", names(tmp))
if (isTRUE(is_weighted)) {
out <- data.frame(lapply(tmp, stats::weighted.mean, w = wts, na.rm = TRUE))
out <- data.frame(lapply(tmp, stats::weighted.mean, w = wts, na.rm = TRUE), check.names = FALSE)
} else {
out <- data.frame(lapply(tmp, mean, na.rm = TRUE))
out <- data.frame(lapply(tmp, mean, na.rm = TRUE), check.names = FALSE)
}
print(out, digits = digits, row.names = FALSE, ...)
} else {
Expand All @@ -39,7 +39,7 @@ function(x, digits = 4, order = NULL, ...) {
xby <- x[ , attr(x, "at"), drop = FALSE]
splits <- split(tmp, xby)
out <- do.call("rbind", lapply(splits, function(set) {
cbind(set[1L, attr(x, "at"), drop = FALSE], data.frame(lapply(set[, !names(set) %in% c("_weights", attr(x, "at")), drop = FALSE], stats::weighted.mean, w = set[["_weights"]], na.rm = TRUE)))
cbind(set[1L, attr(x, "at"), drop = FALSE], data.frame(lapply(set[, !names(set) %in% c("_weights", attr(x, "at")), drop = FALSE], stats::weighted.mean, w = set[["_weights"]], na.rm = TRUE), check.names = FALSE))
}))
} else {
tmp <- x[, grepl("^dydx_", names(x)), drop = FALSE]
Expand Down

0 comments on commit 9cffafb

Please sign in to comment.