Base R
ames <- AmesHousing::make_ames()
# Gr Liv Area = Above grade (ground) living area square feet
lm_ames <- lm(Sale_Price ~ Gr_Liv_Area, data = ames)
lm_ames
##
## Call:
## lm(formula = Sale_Price ~ Gr_Liv_Area, data = ames)
##
## Coefficients:
## (Intercept) Gr_Liv_Area
## 13289.6 111.7
broom::tidy(lm_ames)
## # A tibble: 2 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 13290. 3270. 4.06 0.0000494
## 2 Gr_Liv_Area 112. 2.07 54.1 0
Tidymodels
Steps in a parsnip model:
- Pick a model
- Set the engine (what library is running the model)
- Set the mode (if needed, e.g., regression or classification)
List of models: https://tidymodels.github.io/parsnip/articles/articles/Models.html
Allows you to quickly swap out and try different models
lm_spec <- parsnip::linear_reg() %>%
parsnip::set_engine(engine = "lm")
Train/fit the model:
lm_fit <- parsnip::fit(lm_spec, Sale_Price ~ Gr_Liv_Area, data = ames)
lm_fit
## parsnip model object
##
## Fit time: 3ms
##
## Call:
## stats::lm(formula = formula, data = data)
##
## Coefficients:
## (Intercept) Gr_Liv_Area
## 13289.6 111.7
broom::tidy(lm_fit)
## # A tibble: 2 x 5
## term estimate std.error statistic p.value
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 (Intercept) 13290. 3270. 4.06 0.0000494
## 2 Gr_Liv_Area 112. 2.07 54.1 0