我正在尝试使用 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