Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Cohort retention Analysis in R language data Analysis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces you how to understand the R language data analysis in the Cohort retention analysis, the content is very detailed, interested partners can refer to, I hope to help you.

I believe that students who often do data analysis have heard of Cohort analysis, especially in Internet operations, which is used to analyze customer retention scenarios. In the past, this analysis was mostly done with SQL+Excel.

Recently, when trying to learn Cohort user retention analysis, I found a foreign data analysis enthusiast Cohort retention analysis Python version complete code, and very conscience to provide practice data, as a rookie analyst who is more proficient in R than Python, naturally I first thought of how to translate this code into R version.

http://www.gregreda.com/2015/08/23/cohort-analysis-with-python/

Finally kung fu regardless of the intention, busy day with R language code to restore the R language version of this Cohort analysis, here to share with you, the code is not good, please forgive me, just a demo, not yet done packaging.

library('xlsx')

library('ggplot2')

library('dplyr')

library('magrittr')

library('tidyr')

library('reshape2')

1. Data import:

setwd("D:/R/File/")df % group_by(UserId) %>% summarize( CohortGroup = min(OrderDate))

#Calculate user's first purchase date

CohortGroup$CohortGroup % format('%Y-%m') df % left_join(CohortGroup,by = 'UserId')

#Merge and align the first purchase date with the original order table

2.3 Group (according to the first purchase date, purchase date) calculate the total number of users, total number of orders, total payment amount (user ID to be repeated)

chorts % group_by(CohortGroup,OrderPeriod) %>% summarize( UserId = n_distinct(UserId), OrderId = n_distinct(OrderId), TotalCharges = sum(TotalCharges) ) %>% rename(TotalUsers= UserId , TotalOrders = OrderId)

2.4 Group by user ID and add sequential labels according to month of purchase date

chorts % arrange(CohortGroup,OrderPeriod) %>% group_by(CohortGroup) %>% mutate( CohortPeriod =row_number())

3. Calculate the number of new users purchased in the month

cohort_group_size % filter(CohortPeriod == 1) %>% select(CohortGroup,OrderPeriod,TotalUsers)user_retention % select(CohortGroup,CohortPeriod,TotalUsers) %>% spread(CohortGroup,TotalUsers) #Convert long table to wide table #Convert specific users to ratio of base month

user_retention[,-1] % t() %>% `/`(cohort_group_size$TotalUsers) %>% t() %>% as.data.frame() wide table to long table user_retention1% select(1:5) %>% melt( id.vars = 'CohortPeriod', variable.name = 'CohortGroup', value.name = 'TotalUsers' )

4. Retention curve

ggplot(user_retention1,aes(CohortPeriod,TotalUsers)) + geom_line(aes(group = CohortGroup,colour = CohortGroup)) + scale_x_continuous(breaks = 1:15) + scale_colour_brewer(type = 'div')

Final Remaining Thermodynamic Map Data Source:

user_retentionT % . [2:nrow(.),] %>% as.data.frameuser_retentionT$CohortPeriod

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report