<jupyter_start><jupyter_text>Exercise 5 - Logistic Regression===Simple logistic regression predicts binary (yes/no) events. For example, we may want to predict if someone will arrive at work on time, or if a person shopping will buy a product. This exercise will demonstrate simple logistic regression: predicting an outcome from only one feature. Run the code below to prepare the necessary libraries for this exercise.<jupyter_code># Run this!

suppressMessages(install.packages("tidyverse"))
suppressMessages(library("tidyverse"))
suppressMessages(library("glmnet"))<jupyter_output><empty_output><jupyter_text>Step 1---We want to place a bet on the outcome of the next football (soccer) match. It is the final of a competition, so there will not be a draw. We have historical data about our favourite team playing in matches such as this. Complete the exercise below to see the structure of this data. In the cell below replace: 1. `` with `str` 2. `` with `head` then __run the code__.<jupyter_code>team_stats <- read.delim("Data/football data.txt")

###
# REPLACE <dataPreviewStr> WITH str AND <dataPreviewHead> WITH head
###
<dataPreviewStr>(team_stats)
<dataPreviewHead>(team_stats)
###

summary(team_stats$average_goals_per_match)<jupyter_output><empty_output><jupyter_text>The `team_stats` data shows the average goals per match of our team for the season in the first column, and whether the team won the competition in the second column. The `won_competition` variable is a binary outcome, where 1 represents a win, and 0 represents a loss.Step 2---Let's graph the data so we have a better idea of what's going on. Complete the exercise below to make a scatter plot of `team_stats`. Replace the x variable with the names of the feature we want to plot on the x-axis. In the cell below replace `` with `average_goals_per_match`<jupyter_code>team_stats  %>% 
###
# REPLACE <xData> WITH average_goals_per_match
###
ggplot(aes(x = <xData>, y = as.factor(won_competition), colour = as.factor(won_competition))) +
###

geom_jitter() +
ggtitle("Game statistics for favourite football team") +
xlab("Average number of goals scored per match") +
ylab("Competition win") +
# Align title to centre
theme(plot.title = element_text(hjust = 0.5), legend.position = "none")<jupyter_output><empty_output><jupyter_text>In the plot above, we have used ggplot2's `geom_jitter` function, which adds a small amount of random variation to the location of each point. Since we have binary outcomes in this dataset, using this function allows us to handle overplotting.> If you want to test this for yourself, change the `geom_jitter` call in the code block above to `geom_point`; it is harder to decipher which points overlap using the latter function.We can see that in general, when our team has a good score average (x-axis), they tend to win the competition.Step 3---How can we predict whether the team will win this season? Let's apply AI to this problem, by making a logisitic regression model using this data and then graphing it.We will use the function `glm`, which stands for generalized linear models. We will set the type of model ("family" argument) as binomial logistic regression - to specify that we want a logistic regression model. We'll use the standard R format for the formula, which is `labels ~ features` (if you see a `.` this means it will select all features in the dataset). In the cell below replace: 1. `` with `won_competition ~ average_goals_per_match` 2. `` with `team_stats` then __run the code__.<jupyter_code>###
# REPLACE <formula> WITH won_competition ~ average_goals_per_match AND <dataset> WITH team_stats
###
glm_team <- glm(formula = <formula>, family = binomial(link = "logit"), 
                data = <dataset>)
###
summary(glm_team)

# And we'll quickly print out some predictions to make sure it's working
head(predict(glm_team, data = team_stats, type = "response"))<jupyter_output><empty_output><jupyter_text>Alright, that's the model done. Now run the code below to graph it.<jupyter_code># Run this!

# Plot using ggplot2
team_stats %>%
ggplot(aes(x = average_goals_per_match, y = won_competition)) +
geom_point(aes(colour = as.factor(won_competition)), alpha = 0.5, size = 3) +
geom_smooth(method = "glm", se = FALSE, method.args = list(family = "binomial"), 
            colour = "black") +
ggtitle("Binomial logistic regression model for football team competition win") +
xlab("Average number of goals scored per match") +
ylab("Competition win") +
theme(plot.title = element_text(hjust = 0.5), legend.position = "none") + 
scale_y_continuous(labels = c("0", "", "", "", "1"))<jupyter_output><empty_output><jupyter_text>We now have a binomial logistic regression model to fit our data. The black line represents our model.Step 4------We can read the model above like so:* Take the average number of goals per match for the current year. Let's say it is 2.5.* Find 2.5 on the x-axis. * What value (on the y axis) does the line have at x = 2.5?* If this value is above 0.5, then the model predcits that our team will win this year. If it is less than 0.5, it predicts that our team will lose.Because this line is just a mathematical function (equation) we don't have to do this visually.In the exercise below, choose the number of goals you want to evaluate.The code will calculate the probability that our team will win with your chosen number of goals in the match. Replace `` with a number between 0 and 3, then run the code.<jupyter_code>###
# REPLACE <numberOfGoals> WITH A NUMBER BETWEEN 0 AND 3
###
goals <- <numberOfGoals>
###

# Create data frame for input to predict function
mean_goals <- data.frame(average_goals_per_match = c(goals))

# Run predict function based on inout to goals
mean_goals$prediction <- predict(object = glm_team, newdata = mean_goals, type = "response")

# View result
mean_goals

# Print out the result to screen
paste0("The probability of our team winning this year is ", round(mean_goals$prediction * 100, digits = 4), "%")<jupyter_output><empty_output><jupyter_text>Now let's plot our chosen number of goals in the context of our model using ggplot2:<jupyter_code># Run this!

team_stats %>% 
ggplot(aes(x = average_goals_per_match, y = won_competition)) +
geom_point(aes(colour = as.factor(won_competition)), alpha = 0.5, size = 3) +
geom_point(data = mean_goals, aes(x = average_goals_per_match, y = prediction), size = 5, colour = "black",
           shape = "cross") +
geom_smooth(method = "glm", se = FALSE, method.args = list(family = "binomial"), 
            colour = "black") +
ggtitle("Binomial logistic regression model for football team competition win") +
xlab("Average number of goals scored per match") +
ylab("Competition win") +
theme(plot.title = element_text(hjust = 0.5), legend.position = "none") +
geom_hline(yintercept = mean_goals$prediction, linetype = "dotted") +
geom_vline(xintercept = mean_goals$average_goals_per_match, linetype = "dotted")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09473985953848359, 'fraction_numerical': 0.006879747742582772, 'mean_word_length': 5.094323144104804, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '547853', 'license': 'MIT', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2022, 'n_words': 859, 'path': '05. Logistic Regression - R.ipynb', 'repo_name': 'TomReidNZ/ms-learn-ml-crash-course-R', 'size': '6977'}