I am trying to define a function for ‘stickiness’ – a Business Analytics metric that measures user Engagement – and my function is returning a dataframe that is populated with unexpected data.
stickiness <- function(tdata) {
require(plyr)
mau_unique <- dlply(.data = tdata,
.variables = "dt",
.fun = function(x){unique(x$username)})
dates_char <- names(mau_unique)
dates_vector <- as.Date(dates_char[28:(length(dates_char))],
format = "%Y-%m-%d")
output_df <- data.frame(dates_vector,
matrix(data = 0,
nrow = length(dates_char) - 27,
ncol = 3))
colnames(output_df) <- c("Date", "DAU", "MAU", "Stickiness")
for (i in 1:length(dates_vector)) {
dt <- dates_vector[i]
output_df[i, "DAU"] <- length(unlist(mau_unique[[as.character(dt)]][2]))
set28 <- unique(unlist(lapply(X = mau_unique[i:(i + 27)], FUN = "[[", 2)))
output_df[i, "MAU"] <- length(set28)
output_df[i, "Stickiness"] <- output_df[i, "DAU"] / output_df[i, "MAU"]
}
return(output_df)
}
The following is returned:
Date DAU MAU Stickiness
1 2012-04-28 1 28 0.03571429
2 2012-04-29 1 28 0.03571429
3 2012-04-30 1 28 0.03571429
4 2012-05-01 1 28 0.03571429
5 2012-05-02 1 28 0.03571429
6 2012-05-03 1 28 0.03571429
7 2012-05-04 1 28 0.03571429
8 2012-05-05 1 28 0.03571429
9 2012-05-06 1 28 0.03571429
10 2012-05-07 1 28 0.03571429
I expected something like the following:
Date DAU MAU Stickiness
1 2012-04-28 25000 250000 0.10000000
... ... ... ... ...
10 2012-05-07 27371 284114 0.09633809
I suspect that the problem is related to which environments I’m evaluating in.
UPDATED sample data:
> tdata
dt username
4236 2012-04-06 241343664
3091 2012-04-06 306001012
2936 2012-04-06 388682041
5790 2012-04-05 235612064
6763 2012-04-05 69650072
3392 2012-04-06 617142
7684 2012-04-05 189752749
3904 2012-04-06 255852653
7915 2012-04-05 182713266
6107 2012-04-05 187675644
UPDATE working function (using Brian Diggs’s answer):
stickiness <- function(tdata) {
require(plyr)
mau_unique <- dlply(.data = tdata,
.variables = "dt",
.fun = function(x){unique(x$username)})
dates_char <- names(mau_unique)
dates_vector <- as.Date(dates_char[28:(length(dates_char))],
format = "%Y-%m-%d")
output_df <- data.frame(dates_vector,
matrix(data = 0,
nrow = length(dates_char) - 27,
ncol = 3))
colnames(output_df) <- c("Date", "DAU", "MAU", "Stickiness")
for (i in 1:length(dates_vector)) {
dt <- dates_vector[i]
output_df[i, "DAU"] <- length((mau_unique[[as.character(dt)]])
set28 <- unique(do.call(c, mau_unique[i:(i + 27)]))
output_df[i, "MAU"] <- length(set28)
output_df[i, "Stickiness"] <- output_df[i, "DAU"] / output_df[i, "MAU"]
}
return(output_df)
}
Thanks for adding some sample data, but it is still not really reproducible since the function assumes the data spans at least 28 days (or rather, at least 28 unique dates).
The problem, as near as I can figure, is inside your for loop. With your example data,
so in computing
DAU, you pull a corresponding element frommau_unique. Working outward through your calculation ofDAUwith a dummy value fordt:I don’t know how
DAUshould be calculated, but you always take the second username from the corresponding vector inmau_uniqueand take the length of that, which is why you always get 1. You are doing something similar forset28; I don’t know why you keep trying to pull the second element out.EDIT:
Synthetically generated data is fine. That is a good way to create a lot of data in a small space, and with setting a random seed will allow everyone to work with the same data.
Given you descriptions of
DAUandMAU, I think your for loop should read: (the rest of the function is unchanged)given this, your stickiness is: