更新拟合的 R mgcv::bam 模型报告“无效类型(闭包)...”错误

问题描述 投票:0回答:1

我正在尝试“bam.update”我安装的 mgcv::bam 模型,该模型使用“权重”参数并收到以下错误:

Error in model.frame.default(gp$fake.formula, data, weights = weights,  : 
  invalid type (closure) for variable '(weights)'

我自己无法解决这个问题,所以我把这个问题放在这里。这是一个示例代码,生成与我在更大的模型中遇到的错误完全相同的错误:

library(data.table)
library(mgcv)

mtcars <- data.table(mtcars)

# adding arbitrary "model_weigts"
set.seed(55)
mtcars[, model_weigts := abs(rnorm(nrow(mtcars)))]

# split the dataset
mtcars_1 <- mtcars[1:20,]
mtcars_2 <- mtcars[21:32,]

# an arbitrary model formula 
formula_c <- formula(mpg ~ s(wt) + s(hp))

# fit the initial model to mtcars_1 and attempt to update it with mtcars_2
model_initial <- mgcv::bam(formula_c, data = mtcars_1, weights = model_weigts)
model_updated <- mgcv::bam.update(model_initial, data = mtcars_2)
sessionInfo()
R version 4.3.3 (2024-02-29)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Ventura 13.5

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRblas.0.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Australia/Adelaide
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] mgcv_1.9-1        nlme_3.1-164      data.table_1.15.2

loaded via a namespace (and not attached):
[1] compiler_4.3.3 Matrix_1.6-5   splines_4.3.3  grid_4.3.3     lattice_0.22-5
r mgcv
1个回答
0
投票

如果将权重提供给初始

bam()
,则
bam.update()
期望“新数据”的权重;来自 docs:“如果原始拟合已加权,则必须包含权重列;如果原始拟合中 AR.start 不为 NULL,则必须包含 AR.start 列。”

我不确定这是否“有意义”,但也许您可以在创建 model_initial 之后删除权重?例如

library(data.table)
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.9-1. For overview type 'help("mgcv-package")'.

mtcars <- data.table(mtcars)

# adding arbitrary "model_weigts"
set.seed(55)
mtcars$model_weigts <- abs(rnorm(nrow(mtcars)))

# split the dataset
mtcars_1 <- mtcars[1:20,]
mtcars_2 <- mtcars[21:32,]

# an arbitrary model formula 
formula_c <- formula(mpg ~ s(wt) + s(hp))

# fit the initial model to mtcars_1 and attempt to update it with mtcars_2
model_initial <- mgcv::bam(formula_c, data = mtcars_1, weights = model_weigts)
model_updated <- mgcv::bam.update(model_initial, data = mtcars_2)
#> Error in model.frame.default(gp$fake.formula, data, weights = weights, : invalid type (closure) for variable '(weights)'
model_initial$model$`(weights)` <- NULL
model_updated <- mgcv::bam.update(model_initial, data = mtcars_2)
model_initial
#> 
#> Family: gaussian 
#> Link function: identity 
#> 
#> Formula:
#> mpg ~ s(wt) + s(hp)
#> 
#> Estimated degrees of freedom:
#> 1.71 2.89  total = 5.6 
#> 
#> fREML score: 38.56395
model_updated
#> 
#> Family: gaussian 
#> Link function: identity 
#> 
#> Formula:
#> mpg ~ s(wt) + s(hp)
#> 
#> Estimated degrees of freedom:
#> 2.66 2.43  total = 6.09 
#> 
#> fREML score: 67.67355

创建于 2024-04-16,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.