You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ML-For-Beginners/translations/pcm/2-Regression/1-Tools/solution/R/lesson_1-R.ipynb

450 lines
16 KiB

{
"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",
"<p >\n",
" <img src=\"../../../../../../translated_images/pcm/encouRage.e75d5fe0367fb913.webp\"\n",
" width=\"630\"/>\n",
" <figcaption>Artwork by @allison_horst</figcaption>\n"
],
"metadata": {
"id": "LWNNzfqd6feZ"
}
},
{
"cell_type": "markdown",
"source": [
"## 1. Load our tool set\n",
"\n",
"For dis task, we go need dis packages:\n",
"\n",
"- `tidyverse`: Di [tidyverse](https://www.tidyverse.org/) na [collection of R packages](https://www.tidyverse.org/packages) wey dem design to make data science fast, easy, and fun!\n",
"\n",
"- `tidymodels`: Di [tidymodels](https://www.tidymodels.org/) framework na [collection of packages](https://www.tidymodels.org/packages/) for modeling and machine learning.\n",
"\n",
"You fit install dem like dis:\n",
"\n",
"`install.packages(c(\"tidyverse\", \"tidymodels\"))`\n",
"\n",
"Di script wey dey below go check whether you get di packages wey you need to complete dis module, and e go install dem for you if any dey miss.\n"
],
"metadata": {
"id": "FIo2YhO26wI9"
}
},
{
"cell_type": "code",
"execution_count": 2,
"source": [
"suppressWarnings(if(!require(\"pacman\")) install.packages(\"pacman\"))\n",
"pacman::p_load(tidyverse, tidymodels)"
],
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"Loading required package: pacman\n",
"\n"
]
}
],
"metadata": {
"id": "cIA9fz9v7Dss",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "2df7073b-86b2-4b32-cb86-0da605a0dc11"
}
},
{
"cell_type": "markdown",
"source": [
"Make we load dis beta packages dem and make dem dey available for our current R session. (Dis na just for example, `pacman::p_load()` don already do am for you)\n"
],
"metadata": {
"id": "gpO_P_6f9WUG"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# load the core Tidyverse packages\r\n",
"library(tidyverse)\r\n",
"\r\n",
"# load the core Tidymodels packages\r\n",
"library(tidymodels)\r\n"
],
"outputs": [],
"metadata": {
"id": "NLMycgG-9ezO"
}
},
{
"cell_type": "markdown",
"source": [
"## 2. Diabetes dataset\n",
"\n",
"For dis exercise, we go show our regression skills by predicting tins for diabetes dataset. Di [diabetes dataset](https://www4.stat.ncsu.edu/~boos/var.select/diabetes.rwrite1.txt) get `442 samples` of data wey dey about diabetes, wit 10 predictor feature variables, `age`, `sex`, `body mass index`, `average blood pressure`, and `six blood serum measurements` plus one outcome variable `y`: one quantitative measure of how di disease don progress one year after baseline.\n",
"\n",
"|Number of observations|442|\n",
"|----------------------|:---|\n",
"|Number of predictors|First 10 columns na numeric predictive|\n",
"|Outcome/Target|Column 11 na quantitative measure of how di disease don progress one year after baseline|\n",
"|Predictor Information|- age in years\n",
"||- sex\n",
"||- bmi body mass index\n",
"||- bp average blood pressure\n",
"||- s1 tc, total serum cholesterol\n",
"||- s2 ldl, low-density lipoproteins\n",
"||- s3 hdl, high-density lipoproteins\n",
"||- s4 tch, total cholesterol / HDL\n",
"||- s5 ltg, maybe log of serum triglycerides level\n",
"||- s6 glu, blood sugar level|\n",
"\n",
"\n",
"\n",
"\n",
"> 🎓 Make sure say you remember, dis na supervised learning, and we need one target wey dem call 'y'.\n",
"\n",
"Before you fit do any manipulation for data wit R, you go first import di data enter R memory, or you go build connection wey R fit use access di data remotely.\n",
"\n",
"> Di [readr](https://readr.tidyverse.org/) package, wey dey part of Tidyverse, dey provide fast and easy way to read rectangular data enter R.\n",
"\n",
"Now, make we load di diabetes dataset wey dey dis source URL: <https://www4.stat.ncsu.edu/~boos/var.select/diabetes.html>\n",
"\n",
"We go also check our data small to make sure say e dey okay using `glimpse()` and show di first 5 rows using `slice()`.\n",
"\n",
"Before we go further, make we introduce one tin wey you go dey see well well for R code 🥁🥁: di pipe operator `%>%`\n",
"\n",
"Di pipe operator (`%>%`) dey help perform operations in logical sequence by passing one object go front enter one function or call expression. You fit think of di pipe operator like say e dey talk \"and then\" for your code.\n"
],
"metadata": {
"id": "KM6iXLH996Cl"
}
},
{
"cell_type": "code",
"execution_count": null,
"source": [
"# Import the data set\r\n",
"diabetes <- read_table2(file = \"https://www4.stat.ncsu.edu/~boos/var.select/diabetes.rwrite1.txt\")\r\n",
"\r\n",
"\r\n",
"# Get a glimpse and dimensions of the data\r\n",
"glimpse(diabetes)\r\n",
"\r\n",
"\r\n",
"# Select the first 5 rows of the data\r\n",
"diabetes %>% \r\n",
" slice(1:5)"
],
"outputs": [],
"metadata": {
"id": "Z1geAMhM-bSP"
}
},
{
"cell_type": "markdown",
"source": [
"`glimpse()` dey show say dis data get 442 rows and 11 columns, and all di columns na `double` type. \n",
"\n",
"<br>\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",
"<br>\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",
"<br>\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<!-- CO-OP TRANSLATOR DISCLAIMER START -->\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<!-- CO-OP TRANSLATOR DISCLAIMER END -->\n"
]
}
]
}