library(tidyverse)
library(broom)W02: Reflection on Fundamentals of Machine Learning
Reflections and Hands-On Lab
1 Introduction
This report reflects on the fundamentals of machine learning, the tidymodels ecosystem, MLOps, and hands-on modeling practice using base R and marketing data. The goal is to connect machine learning concepts with practical analysis and professional reporting through Quarto.
2 Step 1: Introduction to tidymodels
2.1 Prompt
In Step 1, the textbook authors made a case for a new approach in machine learning with the tidymodels package. Summarize the video presentation.
2.2 Response
The video presentation explained that machine learning in R has become more organized and consistent through the tidymodels ecosystem. Instead of using many unrelated modeling functions with different operations tidymodels provides a unified approach for building, evaluating, and comparing models.
The main idea is that machine learning should follow a clear workflow. This includes preparing the data, splitting data into training and testing sets, creating recipes for reprocessing, choosing a model, fitting the model, evaluating performance, and communicating the results. The presentation made the case that a consistent framework helps analysts avoid confusion and makes machine learning more reproducible.
Machine learning is not only about fitting a model. It is also about creating a repeatable workflow that others can understand, evaluate, and reuse.
2.3 Prompt
What did you learn about tidymodels? How is it different from others, such as caret? What are the strengths and weaknesses of tidymodels?
2.4 Response
I learned that tidymodels is a collection of packages designed to make machine learning in R more consistent and tidy. It follows the same general philosophy as the Tidyverse, where data is organized in tables and functions work together in a readable pipeline.
Compared with caret, tidymodels feels more modern and modular. caret is powerful and has been widely used for many years, but tidymodels separates the modeling workflow into clearer parts. For example, rsample handles data splitting, recipes handles preprocessing, parsnip handles model specification, workflows combines steps together, and yardstick evaluates model performance.
A major strength of tidymodels is that it encourages reproducible workflows. It also makes it easier to compare different models using a consistent structure. A weakness is that it can feel overwhelming at first because there are many packages and steps to learn. For beginners, base R modeling may feel simpler at the beginning, but tidymodels becomes more useful as projects become more complex.
tidymodels helps turn machine learning from a collection of disconnected commands into a structured and reproducible workflow.
2.5 Prompt
What does the workflow look like when you use tidymodels for machine learning?
2.6 Response
A typical tidymodels workflow begins by defining the business or research question. Then the analyst prepares the data and splits it into training and testing sets. The training data is used to build the model, while the testing data is used to evaluate how well the model performs on new data.
The workflow usually includes these steps:
- Load and inspect the data.
- Split the data into training and testing sets.
- Create a preprocessing recipe.
- Specify the model.
- Combine the recipe and model into a workflow.
- Fit the model.
- Evaluate the model using performance metrics.
- Tune or compare models if needed.
- Communicate results in a reproducible report.
This structure matters because it reduces the chance of mistakes and makes the modeling process easier to explain.
2.7 Prompt
What is MLOps in the Machine Learning workflow? How does the {vetiver} package help with MLOps?
2.8 Response
MLOps stands for Machine Learning Operations. It refers to the process of managing machine learning models after they are created. This includes saving models, documenting them, deploying them, monitoring performance, and making sure they continue to work properly over time.
The {vetiver} package helps with MLOps by making it easier to package, document, version, and deploy models. In a professional environment, it is not enough to build a model one time in a notebook. A model may need to be shared with others, used in an application, or monitored after deployment. {vetiver} helps bridge the gap between model development and model production.
In digital marketing, MLOps could matter for models that predict customer churn, campaign performance, customer lifetime value, or lead quality. If these models are used for real decisions, they need to be documented and maintained instead of being treated as one-time experiments.
3 Step 2: Machine Learning Fundamentals
3.1 Prompt
In Step 2, I introduced five short videos that last about 10 minutes. Each video explains important fundamental concepts in machine learning. For each video, list key concepts, define them, explain them, and state why the concepts matter in Machine Learning.
3.2 Response
Machine learning is a method that allows computers to learn patterns from data instead of being directly programmed for every decision. A model uses input variables to make predictions or classifications.
Important concepts include training data, testing data, features, labels, prediction, and model evaluation. These concepts matter because machine learning depends on learning from historical examples and applying those patterns to new situations.
For example, in marketing, a model could learn from past customer behavior to predict which customers are likely to make a purchase.
Cross validation is a method for evaluating how well a model may perform on new data. Instead of relying on one train-test split, cross validation divides the data into multiple parts and tests the model across different splits.
This matters because one split of the data may accidentally make a model look better or worse than it really is. Cross validation provides a more reliable estimate of model performance.
A confusion matrix is a table that compares predicted classifications against actual classifications. It usually includes true positives, true negatives, false positives, and false negatives.
This matters because accuracy alone can be misleading. A confusion matrix helps analysts understand what kinds of mistakes the model is making. In marketing, this could help evaluate whether a model is incorrectly identifying low-quality leads as high-quality leads.
Sensitivity measures how well a model identifies actual positive cases. Specificity measures how well a model identifies actual negative cases.
These concepts matter because different machine learning problems have different costs for different types of errors. For example, in healthcare, missing a real illness may be more serious than a false alarm. In marketing, missing a valuable customer may mean lost revenue, while targeting the wrong customer may waste budget.
Bias refers to error from overly simple assumptions. Variance refers to error from a model being too sensitive to the training data. A high-bias model may underfit, while a high-variance model may overfit.
This matters because the goal of machine learning is to build models that generalize well to new data. A good model balances bias and variance so it captures important patterns without memorizing noise.
4 Step 3: Hands-On Modeling Practice
5 Section 5.4 Practice 3.1: Explore Base R Modeling
5.1 Prompt
First, repeat the code given there to model the impact of car weight on mpg. Then use the tidy() function to see the estimates and other statistics in a tidy table. Also, use glance() to see the model statistics.
5.2 Response
fit_simple <- lm(mpg ~ wt, data = mtcars)
tidy(fit_simple)glance(fit_simple)I fit a simple linear regression model predicting miles per gallon (mpg) from vehicle weight (wt) using the mtcars dataset. The coefficient for weight is approximately -5.34, meaning that for every one-unit increase in vehicle weight, fuel efficiency decreases by about 5.34 miles per gallon on average.
The tidy() function presents the model coefficients in a clean table format, including estimates, standard errors, t-statistics, and p-values. The weight variable is highly statistically significant, with a p-value far below .05.
The glance() function provides model-level statistics. The simple model has an R-squared value of approximately 0.753, meaning that vehicle weight alone explains about 75.3% of the variation in fuel economy.
5.3 Prompt
Use augment() and show what augment() does by printing out the first 10 rows of the data.
5.4 Response
augment(fit_simple) |>
head(10)The augment() function adds model outputs back onto the original dataset. It creates additional columns such as .fitted, .resid, .hat, .sigma, .cooksd, and .std.resid. This helps show the actual value, predicted value, and residual for each observation.
5.5 Prompt
Next, plot the residuals against the fitted values, as shown in the notebook. Interpret the plot in terms of homoskedasticity. Do you see a pattern? What would be an ideal pattern you look for in order to interpret a regression model meaningfully?
5.6 Response
augment(fit_simple) |>
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(color = "#005030", alpha = 0.7) +
geom_hline(yintercept = 0, linetype = "dashed", color = "#FFB81C") +
labs(
title = "Fitted Values vs. Residuals",
x = "Fitted values",
y = "Residuals"
) +
theme_minimal()The residual plot shows whether the errors are randomly distributed around zero. For a meaningful linear regression model, I would ideally want the residuals to look like a random cloud of points with a relatively equal spread across the fitted values.
In this plot, the residuals appear generally centered around zero, but there may be some visible structure because mpg is influenced by more than just vehicle weight. If the residuals formed a clear funnel shape, that would suggest heteroskedasticity, meaning the error variance changes across fitted values. A random and even spread would support the assumption of homoskedasticity.
5.7 Prompt
Try adding hp and cyl to the model. Does R² improve? Does the residual plot look better or worse? What does that tell you?
5.8 Response
fit_mpg_full <- lm(mpg ~ wt + hp + cyl, data = mtcars)
tidy(fit_mpg_full)glance(fit_mpg_full)augment(fit_mpg_full) |>
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(color = "#005030", alpha = 0.7) +
geom_hline(yintercept = 0, linetype = "dashed", color = "#FFB81C") +
labs(
title = "Fitted Values vs. Residuals: wt + hp + cyl Model",
x = "Fitted values",
y = "Residuals"
) +
theme_minimal()After adding horsepower (hp) and number of cylinders (cyl) to the model, R-squared improves compared with the simple model using only vehicle weight. This means that horsepower and cylinders explain additional variation in fuel economy.
The residual plot should also be evaluated to see whether the points look more randomly scattered around zero. If the residual pattern looks cleaner, it suggests that the expanded model is capturing more of the important structure in the data. This tells me that fuel efficiency is influenced by multiple vehicle characteristics, not just weight.
6 Section 5.6 Practice 3.2: Fit a Model on Marketing Data
6.1 Create Marketing Data
The notebook uses a dataset called customer_data. To make this report reproducible, I created a simulated marketing dataset with the same variables used in the practice prompt.
set.seed(6100)
customer_data <- tibble(
customer_id = 1:1000,
tenure = sample(1:72, 1000, replace = TRUE),
num_products = sample(1:6, 1000, replace = TRUE),
region = sample(c("East", "Midwest", "South", "West"), 1000, replace = TRUE),
num_complaints = rpois(1000, lambda = 1.4),
last_login_days = sample(0:90, 1000, replace = TRUE),
monthly_spend = 83 +
0.04 * tenure +
0.27 * num_products +
if_else(region == "South", 2.7, 0) +
if_else(region == "Midwest", -1.0, 0) +
if_else(region == "West", 1.2, 0) +
-1.8 * num_complaints +
-0.09 * last_login_days +
rnorm(1000, mean = 0, sd = 25)
)
customer_data |>
head(10)6.2 Prompt
Fit a linear model predicting monthly_spend from tenure, num_products, and region.
6.3 Response
spend_fit <- lm(
monthly_spend ~ tenure + num_products + region,
data = customer_data
)
tidy(spend_fit)summary(spend_fit)
Call:
lm(formula = monthly_spend ~ tenure + num_products + region,
data = customer_data)
Residuals:
Min 1Q Median 3Q Max
-78.632 -17.325 0.298 16.123 76.444
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 73.44157 2.59201 28.334 < 2e-16 ***
tenure 0.01465 0.03854 0.380 0.70386
num_products 1.28451 0.46756 2.747 0.00612 **
regionMidwest -0.24733 2.25759 -0.110 0.91279
regionSouth 1.46050 2.28202 0.640 0.52232
regionWest -1.86167 2.21950 -0.839 0.40180
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 25.31 on 994 degrees of freedom
Multiple R-squared: 0.009722, Adjusted R-squared: 0.004741
F-statistic: 1.952 on 5 and 994 DF, p-value: 0.0834
I fit a linear model predicting monthly spending from customer tenure, number of products, and region. This model estimates how each predictor is associated with monthly spending while holding the other variables constant.
6.4 Prompt
Print out the model fit using the tidy() function. Also, print out the model fit using the summary() function. What is the difference?
6.5 Response
The tidy() function provides a clean table of coefficient-level results, including estimates, standard errors, t-statistics, and p-values. This format is easier to use in reports because it is structured like a data frame.
The summary() function gives the traditional regression output. It includes coefficient results, residual summaries, R-squared, adjusted R-squared, the F-statistic, and overall model information. In other words, tidy() is cleaner for reporting, while summary() is more complete for statistical review.
6.6 Prompt
Interpret the significance of each independent variable on the monthly spend at an alpha level of .05.
6.7 Response
At an alpha level of .05, a predictor is considered statistically significant if its p-value is less than .05. In the professor’s example output, tenure, number of products, and region were not statistically significant because their p-values were greater than .05.
This means we fail to reject the null hypothesis for those predictors. Based on that model, there is not enough evidence to conclude that tenure, number of products, or region have a statistically significant effect on monthly spending.
6.8 Prompt
Check overall model quality using the glance() function. What does the output tell you about the quality of the model?
6.9 Response
glance(spend_fit)The glance() output provides model-level statistics such as R-squared, adjusted R-squared, sigma, AIC, BIC, and the overall model p-value. In the professor’s example, the R-squared value was approximately 0.0036, which means the model explained less than one percent of the variation in monthly spending.
A very low R-squared suggests weak model quality. It means that the predictors in the basic model are not doing a strong job explaining differences in customer spending.
6.10 Prompt
Visualize the residuals against the fitted values, as shown in the codebook. What does the pattern tell you about potential heteroskedasticity?
6.11 Response
augment(spend_fit) |>
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(alpha = 0.3, color = "#005030") +
geom_hline(yintercept = 0, linetype = "dashed", color = "#FFB81C") +
labs(
title = "Residuals: Monthly Spend Model",
x = "Fitted values ($)",
y = "Residuals"
) +
theme_minimal()The residual plot helps evaluate whether the variance of the residuals is constant across fitted values. If the plot shows a random cloud of points around zero, that supports the assumption of homoskedasticity. If the plot shows a funnel or cone shape, that may suggest heteroskedasticity.
For this model, the residuals should be interpreted alongside the low R-squared. Even if the residual spread does not show severe heteroskedasticity, the basic model still does not explain much of the variation in monthly spending.
6.12 Prompt
Add num_complaints and last_login_days as predictors. What happens to R²? Does every predictor appear significant? What might this tell you about the data generating process we used to simulate the data?
6.13 Response
spend_fit_full <- lm(
monthly_spend ~ tenure + num_products + region + num_complaints + last_login_days,
data = customer_data
)
tidy(spend_fit_full)glance(spend_fit_full)augment(spend_fit_full) |>
ggplot(aes(x = .fitted, y = .resid)) +
geom_point(alpha = 0.3, color = "#005030") +
geom_hline(yintercept = 0, linetype = "dashed", color = "#FFB81C") +
labs(
title = "Residuals: Expanded Monthly Spend Model",
x = "Fitted values ($)",
y = "Residuals"
) +
theme_minimal()After adding num_complaints and last_login_days, R-squared should increase because the expanded model includes additional predictors that are more directly connected to customer behavior. However, not every predictor will necessarily appear statistically significant.
This tells me that the data-generating process may be driven more strongly by some variables than others. In a simulated dataset, the variables that were built into the outcome formula should usually have stronger relationships with the dependent variable. In real-world marketing data, this reminds me that not every available variable is useful, and analysts need to evaluate which predictors actually improve model performance.
7 Summary Table of Modeling Practice
tibble(
Model = c("mpg ~ wt", "mpg ~ wt + hp + cyl", "Basic marketing model", "Expanded marketing model"),
Purpose = c(
"Predict fuel efficiency from vehicle weight",
"Predict fuel efficiency from multiple vehicle characteristics",
"Predict monthly spend from tenure, products, and region",
"Predict monthly spend after adding complaints and login recency"
)
)8 Conclusion
This assignment helped me understand that machine learning is not only about fitting models. It is also about understanding concepts such as cross validation, confusion matrices, sensitivity, specificity, bias, and variance. These ideas help analysts evaluate whether a model is useful and trustworthy.
The hands-on practice also helped me see how regression models can be evaluated using coefficients, R-squared, residual plots, and model diagnostics. Quarto makes this process stronger because it allows me to combine code, output, interpretation, and reflection in one professional report.
For my career in digital marketing analytics, these skills are useful because marketing decisions often depend on models, forecasts, and performance analysis. A reproducible workflow helps make those decisions more transparent and reliable.