首页 > 解决方案 > R - 计算“生存”业务指标

问题描述

在 R 中,我有一个数据库,其中包含给定月份的公司数据。这些数据与他们花了多少钱以及他们进行了多少交易等有关。我只关心公司 id 和观察月份:

month firm_id
3     333333
3     222222
3     111111
3     444444
3     555555
3     666666
4     111111
4     444444
4     555555
4     666666
4     888888
4     999999
4     000000
5     333333
5     222222
5     111111
5     999999
5     123456
6     333333
6     222222
6     111111
6     444444
6     555555
6     000000
6     999999
6     123456

我要计算的指标如下:我想在每个月“标记”新公司,比如在第 3 个月,并计算其中有多少(占总数的百分比)在第 4、5、6 个月,依此类推,每个月。

我所说的“新”是指在前几个月没有出现的那些。

例如,在示例数据中,我在第 3 个月有 6 家公司。其中 4 家在第 4 个月再次出现,即 66.66%。然后只有三个出现在第 5 个月,50%。很快。

然后,在第四个月,有三个新公司。其中,只有一个出现在第五个月,占 33.33%。其中两个出现在第 6 个月,即 66.66%,依此类推。

总之,我标记了 X 月的新公司,看看其中有多少也在 X+1、X+2、...、X+n 月花钱。然后是 X+1 月,以此类推。

这样做的好方法是什么?我尝试了一些事情并在网上查看,但没有一个选项被证明是富有成效的。谢谢。

编辑:我在以下 OneDrive 链接中共享数据库第一个月(2016 年 9 月)和另一个 2018 年 9 月的数据:

https://1drv.ms/f/s!An8oTgObDW0Sk3P6WSTUR-WKDjHp

谢谢。

编辑 2:我正在添加所需的输出,当我第一次发布问题时我没有包括在内。

#  month next.month  prop.new
#1     3          4 0.6666667
#2     3          5 0.5000000
#3     3          6 0.8333333
#4     4          5 0.3333333
#5     4          6 0.6666667
#6     5          6 1.0000000

标签: r

解决方案


这是一个复杂的功能。我敢打赌,有更简单的方法可以做到这一点,但这个方法有效。
大部分工作是firm_id在每个月创建一个新的数据框,然后将这些月份与之前的月份进行比较。

funNewFirms <- function(firms){
  f <- function(x, y) {
    x <- unlist(x)
    y <- unlist(y)
    setdiff(y, x)
  }
  g <- function(x, y) {
    x <- unlist(x)
    y <- unlist(y)
    length(intersect(x, y))/length(x)
  }

  sp <- split(firms$firm_id, firms$month)

  new <- lapply(seq_along(sp)[-length(sp)], function(i){
    f(sp[[i]], sp[[i + 1]])
  })
  names(new) <- names(sp)[-1]
  newfirms <- data.frame(month = rep(names(new), lengths(new)),
                         firm_id = unlist(new), 
                         stringsAsFactors = FALSE)
  newfirms <- rbind(split(firms, firms$month)[[1]], newfirms)
  spnew <- split(newfirms$firm_id, newfirms$month)

  tmp <- lapply(seq_along(sp)[-length(sp)], function(i){
    inx <- seq_len(i)
    out <- sapply(seq_along(spnew)[-inx], function(j){
      g(spnew[[i]], sp[[j]])
    })
    names(out) <- names(sp)[-inx]
    out
  })
  names(tmp) <- names(sp)[-length(sp)]
  result <- data.frame(
    month = rep(names(tmp), lengths(tmp)),
    next.month = unlist(lapply(tmp, names)),
    prop.new = unlist(tmp)
  )
  row.names(result) <- NULL
  result
}

funNewFirms(firms)
#  month next.month  prop.new
#1     3          4 0.6666667
#2     3          5 0.5000000
#3     3          6 0.8333333
#4     4          5 0.3333333
#5     4          6 0.6666667
#6     5          6 1.0000000

dput格式的数据。

firms <-
structure(list(month = c("3", "3", "3", "3", "3", "3", "4", "4", 
"4", "4", "4", "4", "4", "5", "5", "5", "5", "5", "6", "6", "6", 
"6", "6", "6", "6", "6"), firm_id = c("333333", "222222", "111111", 
"444444", "555555", "666666", "111111", "444444", "555555", "666666", 
"888888", "999999", "000000", "333333", "222222", "111111", "999999", 
"123456", "333333", "222222", "111111", "444444", "555555", "000000", 
"999999", "123456")), class = "data.frame", row.names = c(NA, 
-26L))

推荐阅读