R
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Data Structures
-
Section 4: Data Manipulation
-
Section 5: Visualization and Statistics
-
Section 6: Advanced R
-
Section 7: Practical Projects
-
Section 8: Interview Practice
-
Section 9: More Practice Exercises
-
Section 10: Shiny Apps in Depth
-
Section 11: More Data Wrangling
-
Section 12: Tidyverse Deep Dive
-
Section 13: Statistical Modeling Deep Dive
-
Section 14: Machine Learning in R
-
Section 15: R Visualization Deep Dive
-
Section 16: R Package Development Deep Dive
-
Section 17: R for Reproducible Research
-
Section 18: R and Databases
-
Section 19: R Performance Optimization
-
Section 20: Bioinformatics and Specialized R
-
Section 21: More Shiny Practice
-
Section 22: More Practice Exercises
-
Section 23: R Data Structures Deep Dive
-
Section 24: More Interview and Review
-
Section 25: R for Business Analytics
-
Section 26: R Text Mining and NLP
-
Section 27: R Spatial Data Analysis
-
Section 28: R Deep Learning
-
Section 29: Advanced Statistical Techniques
-
Section 30: R Object Systems Deep Dive
-
Section 31: R Environments and Metaprogramming
-
Section 32: R for Finance
-
Section 33: R for Clinical and Health Data
-
Section 34: More Shiny Advanced Practice
-
Section 35: R Data Cleaning Deep Dive
-
Section 36: R Reporting Automation
-
Section 37: More Practical Projects Round 2
-
Section 38: R Ecosystem and Career
-
Section 39: More Visualization Practice
-
Section 40: R for Experimentation
-
Section 41: R for Genomics and Bioinformatics Deep Dive
-
Section 42: R for Marketing Analytics
-
Section 43: R Data Import/Export Deep Dive
-
Section 44: R String Processing Deep Dive
-
Section 45: R for Actuarial and Insurance Analytics
-
Section 46: R Testing and Quality Assurance Deep Dive
-
Section 47: R Version Control and Collaboration
-
Section 48: R Advanced Functional Programming
-
Section 49: R for Supply Chain and Operations
-
Section 50: More Practice Exercises Round 3
-
Section 51: R Dashboards and BI Integration
-
Section 52: R Data Governance and Ethics
-
Section 53: More Modeling Practice
-
Section 54: R Final Capstone Projects
-
Section 55: R for Sports Analytics
-
Section 56: More Interview Practice Round 2
-
Section 57: R Networking and APIs
-
Section 58: R for Environmental Science
-
Section 59: R Notebook and Documentation Practices
-
Section 60: More Data Wrangling Mastery
-
Section 61: R for A/B Testing at Scale
-
Section 62: R Package Ecosystem Deep Dive
165: Attribution Modeling in R
Imagine you're watching a basketball game. A player sinks a three-pointer right at the buzzer to win the game. The crowd goes wild for the shooter, but as a coach, you know the story is deeper. The point guard stole the ball to start the play, and the power forward made a crucial pass to get the ball to the shooter. If you only give the "credit" for the win to the person who took the shot, you're ignoring the effort that actually made the shot possible.
Attribution modeling in marketing is exactly the same. A customer might see a Facebook ad on Monday, read a blog post on Wednesday, and finally click a Google search ad on Friday to buy your product. If you use a "Last-Touch" model, Google gets 100% of the credit. But that's a lie—the Facebook ad and the blog post did the heavy lifting of introducing the brand. We're going to use R to stop lying to ourselves about where our conversions are actually coming from.
Who actually gets the credit?
In R, we usually handle this by structuring our data as a "customer journey." You need a dataframe where every row is a touchpoint (an interaction), tied to a user ID and a timestamp. Once you have that, you apply a rule to distribute the "conversion value" (usually 1 for a sale) across those touchpoints.
Let's look at how we'd set this up using dplyr. I'll create a small dataset of a user's path to purchase so we can see the math in action.
library(dplyr)
# A simple customer journey dataset
journeys <- data.frame(
user_id = c(1, 1, 1, 2, 2),
touchpoint = c("Facebook", "Blog", "Google", "Email", "Google"),
timestamp = as.POSIXct(c("2023-01-01 10:00", "2023-01-02 12:00", "2023-01-03 15:00",
"2023-01-01 09:00", "2023-01-02 11:00")),
converted = c(0, 0, 1, 0, 1)
)
# We only care about journeys that ended in a conversion
conversions <- journeys %>%
group_by(user_id) %>%
filter(any(converted == 1)) %>%
arrange(timestamp)
Translating the basketball logic to R
Now we apply the models. The "Last-Touch" model is the "shooter" analogy—it only cares about the final interaction. The "Linear" model is the "team effort"—everyone gets an equal slice of the pie.
I prefer writing these as custom mutations. It keeps the logic transparent, which is vital when you have to explain these numbers to a marketing manager who doesn't know R.
# Calculating Linear Attribution
linear_attribution <- conversions %>%
group_by(user_id) %>%
mutate(credit = 1 / n()) %>% # Split 1 conversion equally among all touchpoints
ungroup() %>%
group_by(touchpoint) %>%
summarise(total_credit = sum(credit))
# Calculating Last-Touch Attribution
last_touch_attribution <- conversions %>%
group_by(user_id) %>%
slice_tail(n = 1) %>% # Only take the very last interaction
ungroup() %>%
group_by(touchpoint) %>%
summarise(total_credit = n())
print(linear_attribution)
print(last_touch_attribution)
Why Last-Touch usually lies to you
If you run the code above, you'll notice that "Google" looks like a superhero in the Last-Touch model. But in the Linear model, "Facebook" and "Blog" suddenly appear on the scoreboard. I've seen companies slash their social media budgets because Last-Touch data suggested those channels weren't "converting," only to see their total sales plummet three months later because they stopped filling the top of the funnel.
The reality is that most businesses need a "Position-Based" (or U-Shaped) model. This gives 40% to the first touch (the discovery), 40% to the last touch (the closer), and splits the remaining 20% among the middle. It's a bit more complex to code, but it's far more honest about how people actually shop.
📋 Practical Task
Exercise: Building a U-Shaped Attribution Calculator for an E-commerce Store
You have been handed a dataset of customer interactions. Your goal is to implement a Position-Based (U-Shaped) Attribution Model.
The Requirements:
- Create a dataframe with at least 3 different users. One user should have a journey of 1 touchpoint, one should have 2, and one should have 4. All must end in a conversion (
converted = 1). - Assign 40% of the credit to the first touchpoint.
- Assign 40% of the credit to the last touchpoint.
- Distribute the remaining 20% equally among any touchpoints that occurred in the middle.
- Special Case: If a user only had one touchpoint, that touchpoint gets 100% of the credit. If they had two, each gets 50%.
- Output a final summary table showing the total credit assigned to each channel (e.g., "Instagram", "Organic Search", "Referral").
There are no comments for now.