Skip to content

Fits a linear regression on a 'tidyFit' R6 class. The function can be used with regress.

Usage

# S3 method for class 'lm'
.fit(self, data = NULL)

Arguments

self

a 'tidyFit' R6 class.

data

a data frame, data frame extension (e.g. a tibble), or a lazy data frame (e.g. from dbplyr or dtplyr).

Value

A fitted 'tidyFit' class model.

Details

Hyperparameters:

None. Cross validation not applicable.

Important method arguments (passed to m)

The function provides a wrapper for stats::lm. See ?lm for more details.

Implementation

An argument vcov. can be passed in control or to ... in m to estimate the model with robust standard errors. vcov. can be one of "BS", "HAC", "HC" and "OPG" and is passed to the sandwich package.

See also

.fit.robust, .fit.glm and m methods

Author

Johann Pfitzinger

Examples

# Load data
data <- tidyfit::Factor_Industry_Returns

# Stand-alone function
fit <- m("lm", Return ~ `Mkt-RF` + HML + SMB, data)
fit
#> # A tibble: 1 × 5
#>   estimator_fct `size (MB)` grid_id  model_object settings        
#>   <chr>               <dbl> <chr>    <list>       <list>          
#> 1 stats::lm            2.77 #0010000 <tidyFit>    <tibble [1 × 0]>

# Within 'regress' function
fit <- regress(data, Return ~ ., m("lm"), .mask = c("Date", "Industry"))
coef(fit)
#> # A tibble: 7 × 4
#> # Groups:   model [1]
#>   model term        estimate model_info      
#>   <chr> <chr>          <dbl> <list>          
#> 1 lm    (Intercept)  -0.0312 <tibble [1 × 3]>
#> 2 lm    Mkt-RF        0.977  <tibble [1 × 3]>
#> 3 lm    SMB           0.0182 <tibble [1 × 3]>
#> 4 lm    HML           0.0605 <tibble [1 × 3]>
#> 5 lm    RMW           0.164  <tibble [1 × 3]>
#> 6 lm    CMA           0.117  <tibble [1 × 3]>
#> 7 lm    RF            1.00   <tibble [1 × 3]>

# With robust standard errors
fit <- m("lm", Return ~ `Mkt-RF` + HML + SMB, data, vcov. = "HAC")
fit
#> # A tibble: 1 × 5
#>   estimator_fct `size (MB)` grid_id  model_object settings        
#>   <chr>               <dbl> <chr>    <list>       <list>          
#> 1 stats::lm            2.77 #0010000 <tidyFit>    <tibble [1 × 1]>