为了进行功率分析,我想更改我的模型的固定效应系数,以测试各种效应大小下的功率。来自主SIMR软件包pub(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)
,它将揭示这背后的逻辑。
接受的答案是另一种方法,但fixef(mod)["Petal.Width"] <- 1
产生上述错误的原因是因为fixef<-
是在simr包中定义的。安装和加载simr将允许fixef进行分配。然而,这是一种对待模特的奇怪方式。