尝试修改lmer固定效应时出错:找不到函数"fixef<-"。

4
为了进行功效分析,我希望修改我的模型的固定效应系数,以测试各种效应大小的功效。从主要的SIMR包发表内容(https://besjournals.onlinelibrary.wiley.com/doi/full/10.1111/2041-210X.12504)中,我应该能够使用以下代码模板在运行模型后修改固定效应。
fixef(model1)["x"] <- number

然而,当我试图使用自己的模型进行此操作时,我会收到以下错误信息:
could not find function "fixef<-"

如果我只运行fixef(model)["x"],函数就会正确运行。这里有一个可重现的例子:
#Load package and data
library(lme4)
data(iris)

#build the model
mod<-lmer(Sepal.Length~Petal.Length + Petal.Width + (1|Species),
          data=iris)

fixef(mod)["Petal.Width"] #this code works
fixef(mod)["Petal.Width"] <- 1 #this code produces an error

如果有人知道为什么会出现这种情况,或者有一种替代方法可以修改lmer模型对象中的固定效应系数值,我将非常感激您的帮助。
> sessionInfo()
R version 3.6.0 (2019-04-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

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

other attached packages:
 [1] sjstats_0.18.0  lmerTest_3.1-2  lme4_1.1-23     Matrix_1.2-17   forcats_0.5.0  
 [6] stringr_1.4.0   dplyr_1.0.0     purrr_0.3.4     readr_1.3.1     tidyr_1.1.0    
[11] tibble_3.0.1    ggplot2_3.3.2   tidyverse_1.3.0

loaded via a namespace (and not attached):
 [1] httr_1.4.1          jsonlite_1.7.0      splines_3.6.0       modelr_0.1.8       
 [5] assertthat_0.2.1    statmod_1.4.34      blob_1.2.1          cellranger_1.1.0   
 [9] yaml_2.2.1          bayestestR_0.7.0    numDeriv_2016.8-1.1 pillar_1.4.4       
[13] backports_1.1.7     lattice_0.20-38     glue_1.4.1          rvest_0.3.5        
[17] minqa_1.2.4         colorspace_1.4-1    sandwich_2.5-1      pkgconfig_2.0.3    
[21] broom_0.5.6         haven_2.3.1         xtable_1.8-4        mvtnorm_1.1-1      
[25] scales_1.1.1        emmeans_1.4.7       generics_0.0.2      sjlabelled_1.1.6   
[29] ellipsis_0.3.1      TH.data_1.0-10      withr_2.2.0         cli_2.0.2          
[33] survival_3.2-3      magrittr_1.5        crayon_1.3.4        effectsize_0.3.1   
[37] readxl_1.3.1        estimability_1.3    fs_1.4.1            fansi_0.4.1        
[41] nlme_3.1-148        MASS_7.3-51.4       xml2_1.3.2          rsconnect_0.8.16   
[45] tools_3.6.0         hms_0.5.3           lifecycle_0.2.0     multcomp_1.4-13    
[49] munsell_0.5.0       reprex_0.3.0        compiler_3.6.0      rlang_0.4.6        
[53] grid_3.6.0          nloptr_1.2.2.1      parameters_0.8.0    rstudioapi_0.11    
[57] boot_1.3-22         gtable_0.3.0        codetools_0.2-16    DBI_1.1.0          
[61] sjmisc_2.8.5        R6_2.4.1            zoo_1.8-8           lubridate_1.7.9    
[65] knitr_1.29          performance_0.4.7   insight_0.8.5       stringi_1.4.6      
[69] Rcpp_1.0.4.6        vctrs_0.3.1         dbplyr_1.4.4        tidyselect_1.1.0   
[73] xfun_0.15           coda_0.19-3        

谢谢。


1
教程中的第一行代码是 library(simr)。但我在你的代码中没有看到这样的语句。如果 fixef<- 是在 simr 中定义的函数,那么这就可以解释你的问题了... - Limey
2个回答

2
接受的答案是另一种实现这个目标的方法,但是上面出现错误的原因是因为fixef(mod)["Petal.Width"] <- 1中的fixef<-是在simr包中定义的。安装并加载simr包将允许通过fixef进行赋值操作。然而,这种处理模型的方式确实有些奇怪。

确实。使用隐藏函数 lme4:::setParams() 会更好一些... 或者向包的维护者提出导出它的请求... - Ben Bolker

2
错误简单地表示该函数未实现。当您说fixef(mod)["Petal.Width"] <- 1时,实际上您正在执行`fixef<-`((mod)["Petal.Width"], 1) ,因为实际上R中的任何内容都是一个函数。
您可以这样做:
## before manipulation
fixef(mod)
# (Intercept) Petal.Length  Petal.Width 
#  2.51329946   0.89385535  -0.02424226 

## manipulate    
mod@beta[names(fixef(mod)) %in% "Petal.Width"] <- 1

## after manipulation
fixef(mod)
# (Intercept) Petal.Length  Petal.Width 
#   2.5132995    0.8938554    1.0000000 

你可以尝试探究 str(mod),这将揭示其背后的逻辑。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接