lm_()
is an experimental wrapper around the base stats::lm()
function.
It behaves similarly to lm()
, but enriches the returned object with additional metadata.
The order of the arguments differs from lm()
, and the function uses evaluation
through svBase::prepare_data_dot and svBase::recall_with_data_dot to support the data-dot mechanism.
lm_(data = (.), formula, ..., .data = data)
A data.frame
containing the variables in the model.
An object of class formula
: a symbolic description of the model to be fitted.
Additional arguments passed to stats::lm()
.
an alias for the data
argument
An object of class lm_
, which inherits from lm
, and includes additional
components such as labels
. If no additional attributes are added, a standard lm
object is returned.
data(iris)
# Add labels to variables
attr(iris$Sepal.Length, "label") <- "Sepal Length (cm)"
attr(iris$Petal.Length, "label") <- "Petal Length (cm)"
# Fit the model using lm_()
res <- lm_(iris, formula = Petal.Length ~ Sepal.Length + Species)
. <- iris
res1 <- lm_(Petal.Length ~ Sepal.Length + Species)
#> Error in recall_with_data_dot(arg = "data"): Data-dot mechanism activated, but no `.` object found.
#> ℹ Define `.` before calling this function, or provide `data=` explicitly.
#> ℹ See `?svMisc::data_dot_mechanism()` for more infos.
res
#>
#> Call:
#> stats::lm(formula = formula, data = data)
#>
#> Coefficients:
#> (Intercept) Sepal.Length Speciesversicolor Speciesvirginica
#> -1.7023 0.6321 2.2101 3.0900
#>
class(res)
#> [1] "lm_" "lm"
summary(res)
#>
#> Call:
#> stats::lm(formula = formula, data = data)
#>
#> Residuals:
#> Min 1Q Median 3Q Max
#> -0.76390 -0.17875 0.00716 0.17461 0.79954
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) -1.70234 0.23013 -7.397 1.01e-11 ***
#> Sepal.Length 0.63211 0.04527 13.962 < 2e-16 ***
#> Speciesversicolor 2.21014 0.07047 31.362 < 2e-16 ***
#> Speciesvirginica 3.09000 0.09123 33.870 < 2e-16 ***
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 0.2826 on 146 degrees of freedom
#> Multiple R-squared: 0.9749, Adjusted R-squared: 0.9744
#> F-statistic: 1890 on 3 and 146 DF, p-value: < 2.2e-16
#>
# Access labels
res$labels
#> Petal.Length Sepal.Length Species
#> "Petal Length (cm)" "Sepal Length (cm)" "Species"