Given a model returned by a function such as lm()
and one or more
expressions to evaluate, construct a point estimate and confidence intervals
on the expression(s) using the delta method. Estimate p-values using the normal
distribution as an asymptotic approximation.
Arguments
- model
A linear model, fit by a call to
lm
,feols
, orlm_robust
- ...
One or more expressions involving parameters in
model
- vcov.
Optional specify the covariance of the model. Can be a matrix, function or lambda. Defaults to
stats::vcov(model)
- params
Optional character vector. rename the model parameters to make them easier to refer to in your expressions in
...
Value
A list containing the results of running the delta method, along with
a specialized print
method to print a summary table with p-values
Details
There are two ways to refer to model parameters in your expressions.
The names of your model coefficients, such as
educ
andabil
. Note that "strange" expressions that do not evaluate to a symbol in R code must be surrounded by backticks, e.g.`(Intercept)`
or`educ:abil`
.Using the names supplied as the
params
argument to this function. The order of the coefficients is the order returned bycoef(model)
.
By default, the covariances of the model parameters are taken from the
stats::vcov()
function. This is a generic function that will produce
different results for different model types. That means that lm()
models
will have a homoskedastic covariance matrix, whereas feols
and lm_robust
models will have the type of robust covariance matrix that you specify when
creating the model. You can override this default behavior by using the
vcov.
argument.
Examples
library(fixest)
model <- feols(mpg ~ wt + hp, data = mtcars, vcov = "HC1")
nlcom(model, wt^2, sqrt(hp / wt))
#> # A tibble: 2 × 7
#> Expression Estimate `Std. Error` `Z-Value` `Pr(>|z|)` `CI Lower` `CI Upper`
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 wt^2 15.0 5.05 2.98 2.91e- 3 4.71 25.4
#> 2 sqrt(hp / wt) 0.0905 0.0147 6.14 8.13e-10 0.0604 0.121
# equivalently, supply your own parameter names
library(glue)
nlcom(model, b2^2, sqrt(b3 / b2), params = glue("b{1:3}"))
#> # A tibble: 2 × 7
#> Expression Estimate `Std. Error` `Z-Value` `Pr(>|z|)` `CI Lower` `CI Upper`
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 b2^2 15.0 5.05 2.98 2.91e- 3 4.71 25.4
#> 2 sqrt(b3 / b2) 0.0905 0.0147 6.14 8.13e-10 0.0604 0.121
#>
#> ℹ Where b1=(Intercept), b2=wt, b3=hp