Skip to contents

This function wraps around the lm function in order to make it more friendly to pipe syntax (with the data first).

Usage

zlm(
  data,
  formula,
  subset,
  weights,
  na.action,
  method = "qr",
  model = TRUE,
  x = FALSE,
  y = FALSE,
  qr = TRUE,
  singular.ok = TRUE,
  contrasts = NULL,
  offset,
  ...
)

Arguments

data

A data.frame containing the model data.

formula

The formula to be fitted.

subset

See the lm function.

weights

See the lm function.

na.action

See the lm function.

method

See the lm function.

model

See the lm function.

x

See the lm function.

y

See the lm function.

qr

See the lm function.

singular.ok

See the lm function.

contrasts

See the lm function.

offset

See the lm function.

...

Other arguments to be passed to the lm function.

Value

A fitted model.

See also

  • zglm is a wrapper for glm, to fit generalized linear models.

Examples

# Usage is possible without pipes
zlm( cars, dist ~ speed )
#> 
#> Call:
#> lm(formula = dist ~ speed, data = cars)
#> 
#> Coefficients:
#> (Intercept)        speed  
#>     -17.579        3.932  
#> 

# zfit works well with dplyr and magrittr pipes
if ( require("dplyr", warn.conflicts=FALSE) ) {

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

  # Process iris with filter before piping to zlm
  iris %>%
    filter(Species == "setosa") %>%
    zlm(Sepal.Length ~ Sepal.Width + Petal.Width)
}
#> 
#> Call:
#> lm(formula = Sepal.Length ~ Sepal.Width + Petal.Width, data = .)
#> 
#> Coefficients:
#> (Intercept)  Sepal.Width  Petal.Width  
#>      2.6300       0.6664       0.3723  
#> 

# zfit also works well with the native pipe
if ( require("dplyr") && getRversion() >= "4.1.0" ) {

  # Pipe cars dataset into zlm for fitting
  cars |> zlm(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 |>
    filter(Species == "setosa") |>
    zlm(Sepal.Length ~ Sepal.Width + Petal.Width) |>
    zprint(summary)
}
#> 
#> Call:
#> lm(formula = Sepal.Length ~ Sepal.Width + Petal.Width, data = filter(iris, 
#>     Species == "setosa"))
#> 
#> Residuals:
#>      Min       1Q   Median       3Q      Max 
#> -0.50350 -0.17022  0.02213  0.15569  0.46314 
#> 
#> Coefficients:
#>             Estimate Std. Error t value Pr(>|t|)    
#> (Intercept)  2.63000    0.30928   8.504 4.56e-11 ***
#> Sepal.Width  0.66640    0.09219   7.229 3.68e-09 ***
#> Petal.Width  0.37227    0.33159   1.123    0.267    
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> Residual standard error: 0.2379 on 47 degrees of freedom
#> Multiple R-squared:  0.5631,	Adjusted R-squared:  0.5445 
#> F-statistic: 30.29 on 2 and 47 DF,  p-value: 3.541e-09
#>