Skip to contents

This creates a pipe-friendly version of a fitting function of the standard format –– that is a function with a formula parameter followed by a data parameter.

Compared to just using zfunction(), this function includes some special handling to make the call information, which is usually reported by the summary() function more intuitive. Among other things, it shortens very long data names (longer than 32 characters by default), which otherwise are a nuisance when the data comes from the pipe, because the pipeline gets converted to a very long function call.

This function also stores the base name of the original fitting function, allowing one to use its full name, which is useful to just pull a single fitting function from a package without loading it.

Usage

zfitter(fun)

Arguments

fun

The fitting function to adapt. The name should not be quoted, rather, the actual function should be passed (prefixed with package if needed)

Examples


if (requireNamespace("estimatr", quietly = TRUE)) {
  zlm_robust <- zfitter(estimatr::lm_robust)
  zlm_robust(cars, speed~dist)

  # The resulting function works well the native pipe ...
  if ( getRversion() >= "4.1.0" ) {
    cars |> zlm_robust( speed ~ dist )
  }
}
#>              Estimate Std. Error  t value     Pr(>|t|)  CI Lower   CI Upper DF
#> (Intercept) 8.2839056 0.90884644 9.114747 4.810799e-12 6.4565474 10.1112639 48
#> dist        0.1655676 0.02004456 8.259977 8.937493e-11 0.1252653  0.2058699 48

# ... or with dplyr
if ( require("dplyr", warn.conflicts=FALSE) ) {

  # Pipe cars dataset into zlm_robust for fitting
  cars %>% zlm_robust( speed ~ dist )

  # Process iris with filter() before piping. Print a summary()
  # of the fitted model using zprint() before assigning the
  # model itself (not the summary) to m
  m <- iris %>%
    dplyr::filter(Species=="setosa") %>%
    zlm_robust(Sepal.Length ~ Sepal.Width + Petal.Width) %>%
    zprint(summary)
}
#> Loading required package: dplyr
#> 
#> Call:
#> lm_robust(formula = Sepal.Length ~ Sepal.Width + Petal.Width, 
#>     data = .)
#> 
#> Standard error type:  HC2 
#> 
#> Coefficients:
#>             Estimate Std. Error t value  Pr(>|t|) CI Lower CI Upper DF
#> (Intercept)   2.6300    0.28845   9.118 5.785e-12   2.0497   3.2103 47
#> Sepal.Width   0.6664    0.08766   7.602 1.006e-09   0.4901   0.8428 47
#> Petal.Width   0.3723    0.28977   1.285 2.052e-01  -0.2107   0.9552 47
#> 
#> Multiple R-squared:  0.5631 ,	Adjusted R-squared:  0.5445 
#> F-statistic:    37 on 2 and 47 DF,  p-value: 2.23e-10