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

Viewed 328

In order to run power analyses, I'd like to alter the fixed effect coefficients of my model to test power at various effect sizes. From the primary SIMR package pub (https://besjournals.onlinelibrary.wiley.com/doi/full/10.1111/2041-210X.12504), I should be able to modify fixed effects after a model is run with the following code template:

fixef(model1)["x"] <- number

However, when I attempt to do this with my own models, I receive the following error:

could not find function "fixef<-"

If I just run fixef(model)["x"], the function runs correctly. Here is a reproducible example:

#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

If anyone knows why this is occurring, or an alternative method to modify the fixed effect coefficient values in a lmer model object, I'd be grateful to have your help with this.

> 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        

Thank you.

1 Answers

The error simply says that this function is not implemented. When you say fixef(mod)["Petal.Width"] <- 1 you're actually doing `fixef<-`((mod)["Petal.Width"], 1) , because actually anything in R is a function.

What you can do is:

## 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 

You may want to explore str(mod) which will reveal the logic behind this.

Related