Fits an OLS model via lm(), optionally performing stepwise selection
using Akaike's information criterion.
Usage
reg_lm(
formula,
data,
step = FALSE,
direction = c("both", "forward", "backward"),
...
)Value
A named list:
- model
Fitted
lmobject.- coefficient
Tibble of coefficients, standard errors, t-values, p-values, 95% CIs.
- model_info
Tibble of fit statistics (
r.squared,adj.r.squared, AIC, BIC).- residuals
Tibble of observed, fitted, residuals.
- diagnostics
Tibble of key fit metrics (
Breusch-Pagan, Durbin-Watson).- formula
Original formula.
- input
The original data frame.
Details
When step = TRUE, the function first fits a null model
(response only), then uses step() to search for the best
combination of terms by AIC.
Examples
set.seed(42)
x1 = rnorm(100); x2 = rnorm(100)
y = 1 + 2*x1 - 3*x2 + rnorm(100, sd = 2)
reg_lm(y ~ x1 + x2, data = data.frame(y, x1, x2))
#> $model
#>
#> Call:
#> lm(formula = formula, data = data)
#>
#> Coefficients:
#> (Intercept) x1 x2
#> 1.004 1.713 -2.829
#>
#>
#> $coefficient
#> # A tibble: 3 × 7
#> term estimate std.error statistic p.value conf.low conf.high
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 (Intercept) 1.00 0.204 4.92 3.48e- 6 0.599 1.41
#> 2 x1 1.71 0.196 8.75 6.72e-14 1.32 2.10
#> 3 x2 -2.83 0.225 -12.5 4.91e-22 -3.28 -2.38
#>
#> $model_info
#> # A tibble: 1 × 4
#> r.squared adj.r.squared aic bic
#> <dbl> <dbl> <dbl> <dbl>
#> 1 0.701 0.695 430. 440.
#>
#> $residuals
#> # A tibble: 100 × 3
#> .observed .fitted .residual
#> <dbl> <dbl> <dbl>
#> 1 -3.86 -0.0466 -3.82
#> 2 -2.60 -2.92 0.323
#> 3 7.08 4.46 2.61
#> 4 0.839 -3.14 3.98
#> 5 1.06 3.58 -2.53
#> 6 -1.83 0.523 -2.35
#> 7 3.88 4.79 -0.909
#> 8 -0.930 1.19 -2.12
#> 9 3.18 3.93 -0.747
#> 10 0.146 0.559 -0.413
#> # ℹ 90 more rows
#>
#> $diagnostics
#> # A tibble: 2 × 2
#> metric value
#> <chr> <dbl>
#> 1 BP_test_pvalue 0.0139
#> 2 DW_test_pvalue 0.563
#>
#> $formula
#> y ~ x1 + x2
#> <environment: 0x00000224a501a698>
#>
#> $input
#> y x1 x2
#> 1 -3.86283771 1.37095845 1.200965e+00
#> 2 -2.59609521 -0.56469817 1.044751e+00
#> 3 7.07853302 0.36312841 -1.003209e+00
#> 4 0.83935799 0.63286260 1.848482e+00
#> 5 1.05513368 0.40426832 -6.667734e-01
#> 6 -1.83050160 -0.10612452 1.055138e-01
#> 7 3.87816885 1.51152200 -4.222559e-01
#> 8 -0.93037913 -0.09465904 -1.223502e-01
#> 9 3.18078088 2.01842371 1.881930e-01
#> 10 0.14633299 -0.06271410 1.191610e-01
#> 11 1.28257286 1.30486965 -2.509255e-02
#> 12 9.32301694 2.28664539 1.080727e-01
#> 13 -0.10586620 -1.38886070 -4.854352e-01
#> 14 1.78685766 -0.27878877 -5.042171e-01
#> 15 6.70789385 -0.13332134 -1.661099e+00
#> 16 3.49373235 0.63595040 -3.823337e-01
#> 17 1.70526886 -0.28425292 -5.126503e-01
#> 18 -9.46500900 -2.65645542 2.701891e+00
#> 19 -0.22864558 -2.44046693 -1.362116e+00
#> 20 0.66125363 1.32011335 1.372562e-01
#> 21 5.63893379 -0.30663859 -1.493625e+00
#> 22 1.14566461 -1.78130843 -1.470436e+00
#> 23 -0.76153406 -0.17191736 1.247024e-01
#> 24 4.28300440 1.21467470 -9.966391e-01
#> 25 5.65258657 1.89519346 -1.822614e-03
#> 26 1.07580191 -0.43046913 -4.282589e-01
#> 27 3.35781151 -0.25726938 -6.136716e-01
#> 28 3.07897681 -1.76316309 -2.024678e+00
#> 29 4.27743171 0.46009735 -1.224748e+00
#> 30 1.68193413 -0.63999488 1.795164e-01
#> 31 -0.33548897 0.45545012 5.676206e-01
#> 32 5.78421073 0.70483734 -4.928774e-01
#> 33 0.66685353 1.03510352 6.288407e-05
#> 34 -4.51875387 -0.60892638 1.122890e+00
#> 35 -2.84835977 0.50495512 1.439856e+00
#> 36 0.07539313 -1.71700868 -1.097114e+00
#> 37 2.48045469 -0.78445901 -1.173196e-01
#> 38 -4.35183979 -0.85090759 1.201498e+00
#> 39 -1.93077486 -2.41420765 -4.697296e-01
#> 40 -0.65508975 0.03612261 -5.246948e-02
#> 41 0.21188454 0.20599860 -8.610730e-02
#> 42 4.93706027 -0.36105730 -8.876790e-01
#> 43 6.36734182 0.75816324 -4.446840e-01
#> 44 2.13265236 -0.72670483 -2.944488e-02
#> 45 -3.25622964 -1.36828104 -4.138688e-01
#> 46 2.62539937 0.43281803 1.113386e+00
#> 47 2.85393783 -0.81139318 -4.809928e-01
#> 48 5.13427469 1.44410126 -4.331690e-01
#> 49 -0.54626458 -0.43144620 6.968626e-01
#> 50 3.53763055 0.65564788 -1.056368e+00
#> 51 -0.42636653 0.32192527 -4.069848e-02
#> 52 4.18505749 -0.78383894 -1.551545e+00
#> 53 -1.74704532 1.57572752 1.167170e+00
#> 54 3.48677371 0.64289931 -2.736457e-01
#> 55 5.17846907 0.08976065 -4.678453e-01
#> 56 3.20011103 0.27655075 -1.238252e+00
#> 57 0.90498223 0.67928882 -7.762034e-03
#> 58 3.67364019 0.08983289 -8.002822e-01
#> 59 -5.42089542 -2.99309008 -5.334923e-01
#> 60 -3.05982775 0.28488295 1.287675e+00
#> 61 2.53761915 -0.36723464 -1.755259e-01
#> 62 6.52489831 0.18523056 -1.071782e+00
#> 63 2.44172014 0.58182373 1.632069e-01
#> 64 1.18457758 1.39973683 -3.627384e-01
#> 65 -2.33261824 -0.72729206 5.900135e-01
#> 66 1.43736591 1.30254263 1.432422e+00
#> 67 6.27616385 0.33584812 -9.926925e-01
#> 68 1.33142836 1.03850610 4.546503e-01
#> 69 -2.81309666 0.92072857 8.489806e-02
#> 70 -0.12300714 0.72087816 8.955656e-01
#> 71 0.75059993 -1.04311894 -2.297781e-01
#> 72 -1.59862282 -0.09018639 8.366191e-01
#> 73 7.79702899 0.62351816 -1.745056e+00
#> 74 -5.11229273 -0.95352336 1.689459e+00
#> 75 -3.47309104 -0.54282881 8.647780e-01
#> 76 5.23427741 0.58099650 -1.507760e-01
#> 77 7.82416567 0.76817874 -1.449007e+00
#> 78 -2.48683146 0.46376759 6.430087e-01
#> 79 0.54201673 -0.88577630 4.831939e-01
#> 80 1.22842296 -1.09978090 -6.355626e-03
#> 81 5.21919427 1.51270701 1.514559e-01
#> 82 -0.05708902 0.25792144 -5.841090e-01
#> 83 -1.06815243 0.08844023 3.688067e-01
#> 84 1.14527154 -0.12089654 2.946543e-01
#> 85 -0.46343566 -1.19432890 -2.792594e-01
#> 86 6.92872837 0.61199690 -1.336237e+00
#> 87 3.38266095 -0.21713985 7.007488e-01
#> 88 -2.66486393 -0.18275671 5.541966e-01
#> 89 1.14921221 0.93334633 -8.363066e-01
#> 90 7.97470125 0.82177311 -1.594588e+00
#> 91 1.79416333 1.39211638 2.049586e-01
#> 92 1.97499819 -0.47617392 -3.450880e-01
#> 93 -0.08190744 0.65034856 2.526117e-01
#> 94 12.08833927 1.39111046 -1.294002e+00
#> 95 1.40852163 -1.11078888 -9.591704e-01
#> 96 -4.93358075 -0.86079259 1.085775e+00
#> 97 -2.80732506 -1.13173868 4.037749e-01
#> 98 -1.95276384 -1.45921400 5.864875e-01
#> 99 -4.09103926 0.07998255 1.815228e+00
#> 100 -1.33128908 0.65320434 1.288214e-01
#>