{ "nbformat": 4, "nbformat_minor": 2, "metadata": { "colab": { "name": "lesson_1-R.ipynb", "provenance": [], "collapsed_sections": [], "toc_visible": true }, "kernelspec": { "name": "ir", "display_name": "R" }, "language_info": { "name": "R" }, "coopTranslator": { "original_hash": "c18d3bd0bd8ae3878597e89dcd1fa5c1", "translation_date": "2025-11-18T19:16:30+00:00", "source_file": "2-Regression/1-Tools/solution/R/lesson_1-R.ipynb", "language_code": "pcm" } }, "cells": [ { "cell_type": "markdown", "source": [ "# Build regression model: Start wit R and Tidymodels for regression model\n" ], "metadata": { "id": "YJUHCXqK57yz" } }, { "cell_type": "markdown", "source": [ "## Introduction to Regression - Lesson 1\n", "\n", "#### Make we put am for better understanding\n", "\n", "✅ E get plenty kain regression methods, and di one wey you go choose depend on di kind ansa wey you dey find. If you wan predict di possible height for person wey get one kind age, you go use `linear regression`, because na **number value** you dey look for. But if na to know whether one kind food na vegan or e no be vegan, na **category assignment** you dey find, so you go use `logistic regression`. You go sabi more about logistic regression later. Try think about some kind questions wey you fit ask data, and which of these methods go fit pass.\n", "\n", "For dis section, you go work with [small dataset about diabetes](https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html). Imagine say you wan test one treatment for people wey get diabetes. Machine Learning models fit help you know which patients go respond well to di treatment, based on di combination of variables. Even di most simple regression model, if you see am for graph, fit show you information about variables wey go help you plan your clinical trials well.\n", "\n", "So, make we start dis task!\n", "\n", "
\n",
"
\n",
"
\n",
"\n",
"> glimpse() and slice() na functions wey dey inside [`dplyr`](https://dplyr.tidyverse.org/). Dplyr, wey be part of Tidyverse, na grammar for data manipulation wey dey give consistent set of verbs wey go help you solve di common wahala for data manipulation.\n",
"\n",
"
\n",
"\n",
"Now wey we don get di data, make we focus on one feature (`bmi`) wey we go use for dis exercise. To do dis one, we go need select di column wey we want. So, how we go fit do am?\n",
"\n",
"[`dplyr::select()`](https://dplyr.tidyverse.org/reference/select.html) dey allow us *select* (and if we want, rename) columns for inside data frame.\n"
],
"metadata": {
"id": "UwjVT1Hz-c3Z"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Select predictor feature `bmi` and outcome `y`\r\n",
"diabetes_select <- diabetes %>% \r\n",
" select(c(bmi, y))\r\n",
"\r\n",
"# Print the first 5 rows\r\n",
"diabetes_select %>% \r\n",
" slice(1:10)"
],
"outputs": [],
"metadata": {
"id": "RDY1oAKI-m80"
}
},
{
"cell_type": "markdown",
"source": [
"## 3. Training and Testing data\n",
"\n",
"For supervised learning, e dey normal to *divide* di data into two parts; one big part wey dem go use train di model, and one small \"hold-back\" part wey dem go use check how di model take perform.\n",
"\n",
"Now wey we don ready di data, we fit see if machine fit help us decide beta way to divide di numbers for dis dataset. We fit use di [rsample](https://tidymodels.github.io/rsample/) package, wey be part of di Tidymodels framework, to create one object wey go hold di information on *how* to divide di data, and then use two more rsample functions to bring out di training and testing sets wey we don create:\n"
],
"metadata": {
"id": "SDk668xK-tc3"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"set.seed(2056)\r\n",
"# Split 67% of the data for training and the rest for tesing\r\n",
"diabetes_split <- diabetes_select %>% \r\n",
" initial_split(prop = 0.67)\r\n",
"\r\n",
"# Extract the resulting train and test sets\r\n",
"diabetes_train <- training(diabetes_split)\r\n",
"diabetes_test <- testing(diabetes_split)\r\n",
"\r\n",
"# Print the first 3 rows of the training set\r\n",
"diabetes_train %>% \r\n",
" slice(1:10)"
],
"outputs": [],
"metadata": {
"id": "EqtHx129-1h-"
}
},
{
"cell_type": "markdown",
"source": [
"## 4. Train linear regression model wit Tidymodels\n",
"\n",
"Now we don ready to train our model!\n",
"\n",
"For Tidymodels, you go use `parsnip()` to set up model by specify three things:\n",
"\n",
"- Model **type** na wetin go make models different, like linear regression, logistic regression, decision tree models, and so on.\n",
"\n",
"- Model **mode** na common options like regression and classification; some model types fit for do any of these, while some na only one mode dem get.\n",
"\n",
"- Model **engine** na the tool wey go do the calculation to fit the model. Most times, na R packages dem be, like **`\"lm\"`** or **`\"ranger\"`**\n",
"\n",
"Dis modeling info dey inside model specification, so make we build one!\n"
],
"metadata": {
"id": "sBOS-XhB-6v7"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Build a linear model specification\r\n",
"lm_spec <- \r\n",
" # Type\r\n",
" linear_reg() %>% \r\n",
" # Engine\r\n",
" set_engine(\"lm\") %>% \r\n",
" # Mode\r\n",
" set_mode(\"regression\")\r\n",
"\r\n",
"\r\n",
"# Print the model specification\r\n",
"lm_spec"
],
"outputs": [],
"metadata": {
"id": "20OwEw20--t3"
}
},
{
"cell_type": "markdown",
"source": [
"Afta dem don *specify* di model, di model fit dey `estimate` or `train` wit di [`fit()`](https://parsnip.tidymodels.org/reference/fit.html) function, wey dem dey usually use formula and some data.\n",
"\n",
"`y ~ .` mean say we go fit `y` as di tin wey we wan predict/di target, wey all di predictors/features go explain am, i.e., `.` (for dis case, we get only one predictor: `bmi`).\n"
],
"metadata": {
"id": "_oDHs89k_CJj"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Build a linear model specification\r\n",
"lm_spec <- linear_reg() %>% \r\n",
" set_engine(\"lm\") %>%\r\n",
" set_mode(\"regression\")\r\n",
"\r\n",
"\r\n",
"# Train a linear regression model\r\n",
"lm_mod <- lm_spec %>% \r\n",
" fit(y ~ ., data = diabetes_train)\r\n",
"\r\n",
"# Print the model\r\n",
"lm_mod"
],
"outputs": [],
"metadata": {
"id": "YlsHqd-q_GJQ"
}
},
{
"cell_type": "markdown",
"source": [
"From wetin de model show us, we fit see di coefficients wey e learn during training. Dem represent di coefficients of di line wey fit pass well well wey go give us di lowest overall error between di real and di predicted variable. \n",
"
\n",
"\n",
"## 5. Predict for di test set\n",
"\n",
"Now we don train di model, we fit use am predict how di disease go waka (y) for di test dataset using [parsnip::predict()](https://parsnip.tidymodels.org/reference/predict.model_fit.html). Dis one go help us draw di line wey go separate di data groups.\n"
],
"metadata": {
"id": "kGZ22RQj_Olu"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Make predictions for the test set\r\n",
"predictions <- lm_mod %>% \r\n",
" predict(new_data = diabetes_test)\r\n",
"\r\n",
"# Print out some of the predictions\r\n",
"predictions %>% \r\n",
" slice(1:5)"
],
"outputs": [],
"metadata": {
"id": "nXHbY7M2_aao"
}
},
{
"cell_type": "markdown",
"source": [
"Woohoo! 💃🕺 We don train one model and use am take make predictions!\n",
"\n",
"Wen we dey make predictions, di tidymodels way na to always produce one tibble/data frame of results wey get standard column names. Dis one go make am easy to join di original data and di predictions for one format wey fit work well for di next steps like plotting.\n",
"\n",
"`dplyr::bind_cols()` dey join multiple data frames column by column sharp sharp.\n"
],
"metadata": {
"id": "R_JstwUY_bIs"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Combine the predictions and the original test set\r\n",
"results <- diabetes_test %>% \r\n",
" bind_cols(predictions)\r\n",
"\r\n",
"\r\n",
"results %>% \r\n",
" slice(1:5)"
],
"outputs": [],
"metadata": {
"id": "RybsMJR7_iI8"
}
},
{
"cell_type": "markdown",
"source": [
"## 6. Plot modelling results\n",
"\n",
"Now na time to see dis one for eye 📈. We go create scatter plot of all di `y` and `bmi` values wey dey test set, den use di predictions take draw one line for di best place, between di model data groupings.\n",
"\n",
"R get plenty systems to take make graphs, but `ggplot2` na one of di most fine and flexible ones. E go allow you fit **combine independent components** to take build your graphs.\n"
],
"metadata": {
"id": "XJbYbMZW_n_s"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Set a theme for the plot\r\n",
"theme_set(theme_light())\r\n",
"# Create a scatter plot\r\n",
"results %>% \r\n",
" ggplot(aes(x = bmi)) +\r\n",
" # Add a scatter plot\r\n",
" geom_point(aes(y = y), size = 1.6) +\r\n",
" # Add a line plot\r\n",
" geom_line(aes(y = .pred), color = \"blue\", size = 1.5)"
],
"outputs": [],
"metadata": {
"id": "R9tYp3VW_sTn"
}
},
{
"cell_type": "markdown",
"source": [
"✅ Make you think small about wetin dey happen here. One straight line dey pass through plenty small dots of data, but wetin e dey really do? You fit see how you go fit use dis line take predict where new data wey you never see before go fit enter for the plot y axis? Try talk the practical use of dis model for words.\n",
"\n",
"Congrats, you don build your first linear regression model, use am predict something, and show am for one plot!\n"
],
"metadata": {
"id": "zrPtHIxx_tNI"
}
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n\n\n**Disclaimer**: \nDis dokyument don use AI transleto service [Co-op Translator](https://github.com/Azure/co-op-translator) do di translation. Even as we dey try make am accurate, abeg make you sabi say machine translation fit get mistake or no dey correct well. Di original dokyument for im native language na di main source wey you go fit trust. For important mata, e good make professional human translator check am. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translation.\n\n"
]
}
]
}