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

Error when attempting to modify lmer fixed effect: could not find function "fixef<-"

为了 运行 功效分析,我想更改模型的固定效应系数以测试各种效应大小下的功效。从主要的 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        

谢谢。

错误只是说这个功能没有实现。当你说 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) 这将揭示其背后的逻辑。