2025-06-04

Overview

  • Topic: Exploratory Analysis (EDA) and Simple Linear Regression (SLR) based off Messi’s career goals.
  • Data set: Messi_stats.csv
  • Goal: Analyze Messi’s goals against different factors to better visualize his career.
  • What’s inside:
    • latex equations to explain data margins for certain graphs
    • A ggplot of total goals per team and it’s code
    • A ggplot and SLR of total goals per season
    • A horizontal ggplot of total goals per opponent
    • An interactive 3D Plotly plot (e.g., season × team × goals)

Marginal Goals per Team

Let \(g_{ij}\) be the number of goals Messi scored in season \(i\) for team \(j\). Then the total goals per team \(j\) is

\[ y_{j} \;=\; \sum_{i=1}^{n} g_{ij} \]

These \(y_{j}\) values are exactly what the “Goals per Team” bar chart displays.

Marginal Goals by Season

Using the same notation, the total goals by season \(i\) across all seasons is

\[ y_{i} \;=\; \sum_{j=1}^{m} g_{ij} \]

This \(y_{i}\) corresponds to the bar heights in the “Goals by Season” plot.

Messi Total Goals per Team

Messi Total Goals per Team Code

library(dplyr)
library(ggplot2)

messi_raw <- read.csv("Messi_stats.csv", stringsAsFactors = FALSE)
goals_team <- messi_raw %>%
  group_by(Team) %>%
  summarize(Goals = n()) %>%
  ungroup() %>%
  mutate(Team = factor(Team, levels = Team[order(-Goals)]))

ggplot(goals_team, aes(x = Team, y = Goals)) +
  geom_col(fill = "#1F618D") +
  labs(x = "Team", y = "Total Goals") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

SLR of Messi’s Goals by Season

Messi Total Goals by Opponent

Interactive 3D: Goals by Season and Team