我正在try "bam.更新"我匹配的mgcv::bam模型,该模型使用"weights"参数,并得到以下错误:

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

推荐答案

这可能应该是一条 comments ,但我无法重现您的问题

事实上我可以(我以为您在您的示例中加载了mtcars,但当我在判断实际上没有重置为干净的mtcars后重置时,您没有这样做.

但这也指出了解决方案.从西蒙在?bam.update中所写的内容来看:

如果对原始适合进行加权,则必须包括weights

如果我将模型设置为使用weights而不是model_weigts,我就可以实现这个目标[sic]:

r$> library(data.table) 
    library(mgcv) 
     
    mtcars <- data.table(mtcars) 
     
    # adding arbitrary "model_weigts" 
    set.seed(55) 
    mtcars[, weights := 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 =  weights) 
    model_updated <- mgcv::bam.update(model_initial, data = mtcars_2)           
r$>

注意没有错误.我看到

r$> model_updated$weights                                                       
 [1] 0.120139084 1.812376850 0.151582984 1.119221005 0.001908206 1.188518494
 [7] 0.505343855 0.099234393 0.305353199 0.198409703 0.048910950 0.843233767
[13] 2.075270771 0.360763148 0.637689661 0.366278030 2.355363898 1.093377235
[19] 0.285841001 0.993657777 1.519271950 1.497117849 0.819615308 1.066050411
[25] 0.733755906 0.960343958 0.692180983 1.405998395 1.633538987 0.261830993
[31] 1.564754415 0.314589303
r$> mtcars_1$weights                                                            
 [1] 0.120139084 1.812376850 0.151582984 1.119221005 0.001908206 1.188518494
 [7] 0.505343855 0.099234393 0.305353199 0.198409703 0.048910950 0.843233767
[13] 2.075270771 0.360763148 0.637689661 0.366278030 2.355363898 1.093377235
[19] 0.285841001 0.993657777
r$> mtcars_2$weights                                                            
 [1] 1.5192720 1.4971178 0.8196153 1.0660504 0.7337559 0.9603440 0.6921810
 [8] 1.4059984 1.6335390 0.2618310 1.5647544 0.3145893
r$> all.equal(model_updated$weights, c(mtcars_1$weights, mtcars_2$weights))     
[1] TRUE

因此,除了原始数据和权重之外,bam()更新了适合,以包括新的观察结果,并使用其权重.

这是与:

─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.3 (2024-02-29)
 os       Ubuntu 20.04.6 LTS
 system   x86_64, linux-gnu
 ui       X11
 language en_GB:en
 collate  en_GB.UTF-8
 ctype    en_GB.UTF-8
 tz       Europe/Copenhagen
 date     2024-04-18
 pandoc   2.5 @ /usr/bin/pandoc

─ Packages ───────────────────────────────────────────────────────────────────
 package     * version date (UTC) lib source
 cli           3.6.2   2023-12-11 [1] RSPM (R 4.3.2)
 data.table  * 1.15.4  2024-03-30 [1] RSPM (R 4.3.3)
 lattice       0.22-6  2024-03-20 [1] RSPM (R 4.3.3)
 Matrix        1.6-5   2024-01-11 [1] RSPM (R 4.3.2)
 mgcv        * 1.9-1   2023-12-21 [1] CRAN (R 4.3.3)
 nlme        * 3.1-164 2023-11-27 [1] RSPM (R 4.3.2)
 sessioninfo   1.2.2   2021-12-06 [1] RSPM (R 4.3.0)

 [1] /home/au690221/R/x86_64-pc-linux-gnu-library/4.3
 [2] /usr/local/lib/R/site-library
 [3] /usr/lib/R/site-library
 [4] /usr/lib/R/library

──────────────────────────────────────────────────────────────────────────────

R相关问答推荐

如果索引重复,聚合xts核心数据

带有gplot 2的十字舱口

如何自定义Shapviz图?

当两个图层映射到相同的美学时,隐藏一个图层的图例值

如何在geom_col中反转条

使用rest从header(h2,h3,table)提取分层信息

如何删除最后一个可操作对象

按多列统计频次

在保留列表元素属性的同时替换列表元素

条形图顶部与其错误条形图不对齐

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

按列中显示的配对组估算NA值

提高圣彼得堡模拟的速度

将数据集旋转到长格式,用于遵循特定名称模式的所有变量对

如何在AER::ivreg中指定仪器?

按镜像列值自定义行顺序

使用dqur在不同变量上创建具有多个条件的变量

如何在R中的两列以上使用联合(&U)?

了解nchar在列表上的意外行为

如何在R中添加标识连续日期的新列