Skip to content

Commit

Permalink
Add error message when i() is used in the FE part => related to #77
Browse files Browse the repository at this point in the history
  • Loading branch information
lrberge committed Jan 5, 2021
1 parent 5a369fe commit bbc8371
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
# Other

- More coherence regarding the use of `summary` applied to models for which the SEs were computed at estimation time. Now there is a memory of how the SEs were computed, so that, for example, if only the argument `dof` is passed to `summary`, then the SEs will be clustered in the same way as estimation time but and only `dof` will change.

- Now an error is raised when `i()` is used in the fixed-effects part of the formula. The appropriate way is indicated (related to [#77](https://github.com/lrberge/fixest/issues/77)).

# fixest 0.8.0 (2020-12-14)

Expand Down
20 changes: 19 additions & 1 deletion R/MiscFuns.R
Original file line number Diff line number Diff line change
Expand Up @@ -2503,7 +2503,7 @@ did_means = function(fml, base, treat_var, post_var, tex = FALSE, treat_dict, di

#' Create, or interact variables with, factors
#'
#' Treat a variable as a factor, or interacts a variable with another treated as a factor. Values to be dropped/kept from the factor can be easily set.
#' Treat a variable as a factor, or interacts a variable with another treated as a factor. Values to be dropped/kept from the factor can be easily set. Note that to interact fixed-effects, this function should not be used: instead use directly the syntax \code{fe1^fe2}.
#'
#' @param var A vector to be interacted with \code{f}. If the other argument \code{f} is missing, then this vector will be treated as the argument \code{f}.
#' @param f A vector (of any type) that will be treated as a factor. Must be of the same length as \code{var} if \code{var} is not missing.
Expand All @@ -2514,6 +2514,9 @@ did_means = function(fml, base, treat_var, post_var, tex = FALSE, treat_dict, di
#' @param drop2 A vector of regular expressions or integers (if \code{f2} is integer). If provided, all values from \code{f2} that match \code{drop2} will be removed.
#' @param keep2 A vector of regular expressions or integers (if \code{f2} is integer). If provided, only the values from \code{f2} that match \code{keep2} will be kept.
#'
#' @details
#' To interact fixed-effects, this function should not be used: instead use directly the syntax \code{fe1^fe2} in the fixed-effects part of the formula. Please see the details and examples in the help page of \code{\link[fixest]{feols}}.
#'
#' @return
#' It returns a matrix with number of rows the length of \code{var}. The number of columns is equal to the number of cases contained in \code{f} minus the reference(s).
#'
Expand Down Expand Up @@ -5139,7 +5142,22 @@ fixef_terms = function(fml, stepwise = FALSE, origin_type = "feols"){
return(msg)
}
}
}

# And yet again some error checking => i() should NOT be used
if(any(grepl("^i(nteract)?\\(", my_vars))){
# We create an error instead of simply correcting the syntax => this is because the function i is
# very different and should not be confused

var_pblm = my_vars[grepl("^i(nteract)?\\(", my_vars)][1]

get_new_var = function(var, f, f2, ...) match.call()

what = eval(str2lang(gsub("^i(nteract)?", "get_new_var", var_pblm)))
n_var = sum(c("var", "f", "f2") %in% names(what))
msg = if(n_var == 1) "Using i() to create fixed-effects is not possible, use directly the variable." else paste0("To interact fixed-effects, use the syntax fe1^fe2 (in your case ", deparse(what[[2]]), "^", deparse(what[[3]]), ").")

stop("The function i() should not be used in the fixed-effects part of the formula. ", msg)
}

# Internal function
Expand Down

0 comments on commit bbc8371

Please sign in to comment.