用户定义的R函数无法使用dplyr

时间:2019-04-24 21:43:56

标签: r statistics data-science

我正尝试像示例here一样执行分析,但是使用beta分布的不同参数化。在分析开始时,我不确定我想要什么参数设置,因此我定义了一个自定义函数,以便可以在一处进行参数设置更改,然后在其余代码中使用。见下文。

f_beta <- function(x, elig, par) {
  return (
    dbeta(x, 
          exp(par[1] + par[2] * log(elig)), 
          exp(par[3] + par[4] * log(elig)), 
          log = TRUE))
}

当我尝试将函数应用于数据到数据框时,它不会返回正确的结果。 “ likelihood”和“ likelihood2”字段应返回相同的结果,但不会返回相同的结果。

assignments <- df %>%
  select(-cluster) %>%
  crossing(fits) %>%
  mutate(likelihood = prior * dbeta(enrpct, exp(a0 + b0 * log(elig)), exp(a1 + b1 * log(elig)), log = TRUE), 
         likelihood2 = prior * f_beta(enrpct, elig, c(a0, b0, a1, b1))) %>%
  group_by(id) %>%
  top_n(1, likelihood) %>%
  ungroup()

完整代码如下。

library(tidyr)
library(dplyr)

# custom beta distribution parameterization
f_beta <- function(x, elig, par) {
  return (dbeta(x, exp(par[1] + par[2] * log(elig)), exp(par[3] + par[4] * log(elig)), log = TRUE))
}

# log-likelihood
ll <- function(x, elig, par) {
  -sum(f_beta(x, elig, par))
}

# optimizer 
fit_beta <- function(x, elig, init = c(-0.5, 0.2, -1.1, 0.3)) {
  m <- optim(par = init, fn = ll, elig = elig, x = x)
  coef <- m$par
  data_frame(a0 = coef[1], b0 = coef[2], a1 = coef[3], b1 = coef[4], number = length(x))
}


####### generate data
n <- 10000
n2 <- 5000

# mixture 1 parameters
a0 <- -1
b0 <- 0.3
a1 <- -2
b1 <- 1

# mixture 2 parameters
a01 <- -1
b01 <- 0.5
a11 <- -1.5
b11 <- 0.8

# generate data 
df <- data.frame(id = 1:n, elig = sample(1:100, size = n, replace = TRUE) * 100)
df$enrpct <- rbeta(n, exp(a0 + b0 * log(df$elig)), exp(a1 + b1 * log(df$elig)))
df2 <- data.frame(id = (n+1):(n+n2), elig = sample(1:100, size = n2, replace = TRUE) * 100)
df2$enrpct <- rbeta(n2, exp(a01 + b01 * log(df2$elig)), exp(a11 + b11 * log(df2$elig)))
df <- rbind(df, df2)

# assign to clusters randomly like in example
df$cluster <- factor(sample(c("A", "B"), nrow(df), replace = TRUE))

# maximization step of E-M algorithm
fits <- df %>%
  group_by(cluster) %>%
  do(fit_beta(.$enrpct, .$elig)) %>%
  ungroup() %>% 
  mutate(prior = number / sum(number))

# expectation step of E-M algorithm
assignments <- df %>%
  select(-cluster) %>%
  crossing(fits) %>%
  mutate(likelihood = prior * dbeta(enrpct, exp(a0 + b0 * log(elig)), exp(a1 + b1 * log(elig)), log = TRUE), 
         likelihood2 = prior * f_beta(enrpct, elig, c(a0, b0, a1, b1))) %>%
  group_by(id) %>%
  top_n(1, likelihood) %>%
  ungroup()

head(assignments)

1 个答案:

答案 0 :(得分:2)

这是因为您在计算c(a0, b0, a1, b1)时使用了likelihood2。但是这些变量中的每一个都是数据帧中的整个列,因此使用c()只会将它们连接起来,最终会使用错误的值。

有了它,它将起作用:

f_beta <- function(x, elig, a0, b0, a1, b1) {
  return (dbeta(x, exp(a0 + b0 * log(elig)), exp(a1 + b1 * log(elig)), log = TRUE))
}
likelihood2 = prior * f_beta(enrpct, elig, a0, b0, a1, b1)