Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprende Building Regression Models | Section
Predictive Modeling with Tidymodels in R
Sección 1. Capítulo 4
single

single

bookBuilding Regression Models

Desliza para mostrar el menú

Regression modeling allows you to predict a continuous outcome based on one or more input features, making it an essential tool for exploring relationships between variables and making informed predictions. In business, economics, health sciences, and many other fields, regression models help you estimate values such as house prices, sales, or patient outcomes based on relevant predictors. Tidymodels provides a unified and tidy approach for building, fitting, and interpreting regression models in R, streamlining the process from model specification to extracting actionable results.

1234567891011121314151617181920212223
options(crayon.enabled = FALSE) library(tidymodels) # Load example housing dataset data("ames", package = "modeldata") # Split data into training and testing sets set.seed(123) housing_split <- initial_split(ames, prop = 0.8) housing_train <- training(housing_split) housing_test <- testing(housing_split) # Specify a linear regression model using parsnip lm_spec <- linear_reg() %>% set_engine("lm") # Fit the model to the training data lm_fit <- lm_spec %>% fit(Sale_Price ~ Gr_Liv_Area + Year_Built, data = housing_train) # Extract model summary and coefficients summary(lm_fit$fit) coef(lm_fit$fit)
copy

When building a regression model with Tidymodels, you begin by specifying the model type and engine using the parsnip package. Here, linear_reg() defines a linear regression model, and set_engine("lm") selects R's base linear modeling engine. You then fit the model to your data using the fit() function, providing a formula that describes the relationship between the outcome and predictors, along with the training dataset. Once fitted, you can extract results such as model summaries and coefficients directly from the fitted model object. These outputs allow you to interpret the influence of each predictor on the outcome and assess model performance.

Tarea

Desliza para comenzar a programar

Specify and fit a linear regression model to a dataset using Tidymodels. Use the provided sample dataset, where y is the outcome and x1, x2 are predictors.

To complete this task, ensure your function does the following:

  • Load the tidymodels meta-package.
  • Specify a linear regression model utilizing the linear_reg() function.
  • Set the engine to "lm" utilizing the set_engine() function.
  • Fit the model to the dataset utilizing the fit() function. Use y as the outcome and x1, x2 as predictors in your formula.
  • Return the fitted model object.

Solución

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 1. Capítulo 4
single

single

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

some-alt