Fits a linear regression model to the given data

fit_linear_regression(data, x_vars, y_var, intercept = TRUE)

Arguments

data

dataframe containing the feature and label variables

x_vars

character vector of variable names to be used as predictors

y_var

character string of the variable name to be used as the response variable

intercept

logical value indicating whether to include an intercept term in the model

Value

a list containing the model object and fitted values

Examples

data(mtcars)
lm_fit <- fit_linear_regression(data = mtcars, x_vars = c("wt"), y_var = "mpg")
lm_fit$model # display the model object
#> $coefficients
#>          mpg
#>    37.285126
#> wt -5.344472
#> 
#> $residuals
#>                            mpg
#> Mazda RX4           -2.2826106
#> Mazda RX4 Wag       -0.9197704
#> Datsun 710          -2.0859521
#> Hornet 4 Drive       1.2973499
#> Hornet Sportabout   -0.2001440
#> Valiant             -0.6932545
#> Duster 360          -3.9053627
#> Merc 240D            4.1637381
#> Merc 230             2.3499593
#> Merc 280             0.2998560
#> Merc 280C           -1.1001440
#> Merc 450SE           0.8668731
#> Merc 450SL          -0.0502472
#> Merc 450SLC         -1.8830236
#> Cadillac Fleetwood   1.1733496
#> Lincoln Continental  2.1032876
#> Chrysler Imperial    5.9810744
#> Fiat 128             6.8727113
#> Honda Civic          1.7461954
#> Toyota Corolla       6.4219792
#> Toyota Corona       -2.6110037
#> Dodge Challenger    -2.9725862
#> AMC Javelin         -3.7268663
#> Camaro Z28          -3.4623553
#> Pontiac Firebird     2.4643670
#> Fiat X1-9            0.3564263
#> Porsche 914-2        0.1520430
#> Lotus Europa         1.2010593
#> Ford Pantera L      -4.5431513
#> Ferrari Dino        -2.7809399
#> Maserati Bora       -3.2053627
#> Volvo 142E          -1.0274952
#> 
lm_fit$fitted_values # display the fitted values
#>                           mpg
#> Mazda RX4           23.282611
#> Mazda RX4 Wag       21.919770
#> Datsun 710          24.885952
#> Hornet 4 Drive      20.102650
#> Hornet Sportabout   18.900144
#> Valiant             18.793255
#> Duster 360          18.205363
#> Merc 240D           20.236262
#> Merc 230            20.450041
#> Merc 280            18.900144
#> Merc 280C           18.900144
#> Merc 450SE          15.533127
#> Merc 450SL          17.350247
#> Merc 450SLC         17.083024
#> Cadillac Fleetwood   9.226650
#> Lincoln Continental  8.296712
#> Chrysler Imperial    8.718926
#> Fiat 128            25.527289
#> Honda Civic         28.653805
#> Toyota Corolla      27.478021
#> Toyota Corona       24.111004
#> Dodge Challenger    18.472586
#> AMC Javelin         18.926866
#> Camaro Z28          16.762355
#> Pontiac Firebird    16.735633
#> Fiat X1-9           26.943574
#> Porsche 914-2       25.847957
#> Lotus Europa        29.198941
#> Ford Pantera L      20.343151
#> Ferrari Dino        22.480940
#> Maserati Bora       18.205363
#> Volvo 142E          22.427495