Reputation: 4570
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)
}
Upvotes: 0
Views: 81
Reputation: 58855
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,
> mau_unique
$`2012-04-05`
[1] 235612064 69650072 189752749 182713266 187675644
$`2012-04-06`
[1] 241343664 306001012 388682041 617142 255852653
attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
dt
1 2012-04-05
2 2012-04-06
so in computing DAU
, you pull a corresponding element from mau_unique
. Working outward through your calculation of DAU
with a dummy value for dt
:
> dt <- as.Date("2012-04-05")
> dt
[1] "2012-04-05"
> as.character(dt)
[1] "2012-04-05"
> mau_unique[[as.character(dt)]]
[1] 235612064 69650072 189752749 182713266 187675644
> mau_unique[[as.character(dt)]][2]
[1] 69650072
> unlist(mau_unique[[as.character(dt)]][2])
[1] 69650072
> length(unlist(mau_unique[[as.character(dt)]][2]))
[1] 1
I don't know how DAU
should be calculated, but you always take the second username from the corresponding vector in mau_unique
and take the length of that, which is why you always get 1. You are doing something similar for set28
; 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.
set.seed(1234)
tdata <- data.frame(dt = sample(seq(as.Date("2012-04-01"),
as.Date("2012-04-30"),
by = "day"),
size = 10000,
replace = TRUE),
username = sample(10000:10200,
10000,
replace = TRUE))
Given you descriptions of DAU
and MAU
, I think your for loop should read: (the rest of the function is unchanged)
for (i in 1:length(dates_vector)) {
dt <- dates_vector[i]
output_df[i, "DAU"] <- length(mau_unique[[as.character(dt)]])
output_df[i, "MAU"] <- length(unique(unlist(mau_unique[i:(i+27)])))
output_df[i, "Stickiness"] <- output_df[i, "DAU"] / output_df[i, "MAU"]
}
given this, your stickiness is:
> stickiness(tdata)
Date DAU MAU Stickiness
1 2012-04-28 156 201 0.7761194
2 2012-04-29 168 201 0.8358209
3 2012-04-30 152 201 0.7562189
Upvotes: 4