1

我正在尝试使用 R 中的 DirichReg 包将公式对象传递给 Dirichlet 回归。如下所示,该包似乎无法接受这种格式的公式,但文档中没有任何内容说明此限制。这个工作流程的原因是我正在尝试设置一个交叉验证函数,该函数可以应用于不同公式的列表(具有不同协变量的 IE)并返回样本外预测能力以帮助模型选择。

library (DirichletReg)

df <- ArcticLake  # plug-in your data here
df$Y <- DR_data(df[,1:3])  # prepare the Y's
Warning in DR_data(df[, 1:3]) :
  not all rows sum up to 1 => normalization forced

formula <- reformulate(termlabels = "depth", response="Y")

mod <- DirichReg(formula, df)

Error: object of type 'symbol' is not subsettable
Error during wrapup: 

mod <- DirichReg(Y~depth, df)

str(Y~depth)

Class 'formula'  language Y ~ depth
  ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 

str(formula)

Class 'formula'  language Y ~ depth
  ..- attr(*, ".Environment")=<environment: R_GlobalEnv> 

formula <- as.formula("Y~depth")
mod <- DirichReg(formula, df)

Error: object of type 'symbol' is not subsettable
Error during wrapup: 

我的“公式”对象与工作 DirichReg 调用中指定的公式之间似乎没有任何区别。

我的猜测是,它与使用 DR_data 命令格式化响应变量的方式有关,但我想不出一种方法来解决这个问题,以便在函数中动态指定公式。

> str(df$Y)
 DirichletRegData [1:39, 1:3] 0.775 0.719 0.507 0.524 0.7 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:39] "1" "2" "3" "4" ...
  ..$ : chr [1:3] "sand" "silt" "clay"
 - attr(*, "Y.original")='data.frame':  39 obs. of  3 variables:
  ..$ sand: num [1:39] 0.775 0.719 0.507 0.522 0.7 0.665 0.431 0.534 0.155 0.317 ...
  ..$ silt: num [1:39] 0.195 0.249 0.361 0.409 0.265 0.322 0.553 0.368 0.544 0.415 ...
  ..$ clay: num [1:39] 0.03 0.032 0.132 0.066 0.035 0.013 0.016 0.098 0.301 0.268 ...
 - attr(*, "dims")= int 3
 - attr(*, "dim.names")= chr [1:3] "sand" "silt" "clay"
 - attr(*, "obs")= int 39
 - attr(*, "valid_obs")= int 39
 - attr(*, "normalized")= logi TRUE
 - attr(*, "transformed")= logi FALSE
 - attr(*, "base")= num 1
4

3 回答 3

1

您可以尝试改为传递字符,然后使用 as.formula 转换为公式

as.formula("z ~ x + y")
于 2017-11-29T07:00:20.670 回答
1

@Smiley Bcc 可能一直在暗示这一点,但看来您必须as.formula()DirichletReg()函数内部调用。从上面的示例数据:

> f <- as.formula('Y~depth')
> mod <- DirichReg(f, df)
Error: object of type 'symbol' is not subsettable

> f <- 'Y~depth'
> mod <- DirichReg(as.formula(f), df)

有趣的是,当您将对象命名为“公式”时,它不起作用(可能出于不同的原因):

> formula <- 'Y~depth'
> mod <- DirichReg(as.formula(formula), df)
Error: object of type 'closure' is not subsettable

formula我假设对函数内部调用的对象有某种直接引用DirichletReg(),因此请避免专门调用它。

于 2017-11-29T20:56:12.543 回答
0

此外,如果尝试在函数中使用@dmp 的解决方法,则需要将公式对象分配给全局环境。

见问题:

library (DirichletReg)

df <- ArcticLake  # plug-in your data here
df$Y <- DR_data(df[,1:3])  # prepare the Y's

f <- reformulate(termlabels = "depth", response="Y")

mod <- DirichReg(f %>% as.formula, df)

runReg <- function(this.formula, data) {

  message(this.formula)

  mod <- DirichReg(as.formula(this.formula), data)

  return(mod)

}

res <- runReg("Y~depth", df)

Y~depth as.formula(this.formula) 中的错误:找不到对象“this.formula”

和解决方案:

runReg <- function(this.formula, data) {

  message(this.formula)

  f <<- this.formula


  mod <- DirichReg(as.formula(f), data)

  return(mod)

}

res <- runReg("Y~depth", df)

这似乎是一种非常老套的方法,可能会引入危险的命名空间冲突,所以我很想看看其他人是否有其他解决方案的想法。

于 2017-11-29T22:42:09.547 回答