R语言 如何在非线性混合效应模型中引入随机效应?

fkaflof6  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(155)

我想建立一个非线性混合效应模型,该模型描述了两个变量"x"和"y"之间的关系,这两个变量通过第三个变量"r"随机变化,使用指数上升到最大值,如以下方程所描述的:y = θ(1-exp(-β * x))。
我已经能够使用nls()创建x和y的非线性模型,但是我还没有成功地将随机效应合并到nlme()中。
当我使用nlme()构建模型时,我得到了一条错误消息:"eval(预测值、数据、环境)中出错:未找到对象"theta""。由于nls()模型使用相同的 Dataframe 运行时没有问题,因此我意外遇到此错误。
要构建数据集:

x = c(33,35,16,8,31,31,31,23,7,7,7,7,11,11,3,3,6,6,32,32,1,17,17,17,25,40,40,6,6,29,29,13,23,23,44,44,43,43,13,4,6,15,17,22,28,8,11,22,32,6,12,20,27,15,29,29,29,29,29,12,12,16,16,12,12,2,49,49,14,14,14,37,2.87,4.86,7.90,11.95,16.90,16.90,18.90,18.89,22.00,24.08,27.14,30.25,31.22,32.26,7,14,19,31,36,7,14,19,31,36,7,16,16,16,16,16,16,32,32,32,32,32,32,11,11,11,13,13,13,13,13,13,13,13,13,13,13,13,9,9)

y = c(39.61,32.66,27.06,21.74,22.18,38.19,35.02,23.13,9.70,14.20,13.40,15.30,18.80,19.00,3.80,4.90,15.00,14.20,24.90,16.56,1.76,29.29,28.49,18.64,27.10,9.47,14.14,10.27,8.44,26.15,25.43,22.00,19.00,13.00,73.19,67.76,32.34,36.86,8.00,1.57,8.33,16.20,14.69,18.95,20.52,4.92,8.28,15.27,18.37,6.60,10.98,12.56,19.04,5.49,21.00,12.90,17.30,11.40,12.20,15.63,15.22,33.80,17.78,19.33,3.86,8.57,30.40,13.39,11.93,4.55,6.18,12.70,2.71,7.23,5.61,22.74,15.71,16.95,18.31,20.78,17.64,20.00,19.52,24.86,30.06,24.92,4.17,11.02,10.08,14.94,25.98,0.00,3.67,3.67,6.69,11.90,5.06,13.21,10.33,0.00,0.00,6.47,8.38,28.57,25.26,28.67,27.92,33.69,29.61,6.11,7.13,6.93,4.81,15.34,4.90,14.94,8.88,10.24,8.80,10.46,10.48,9.19,9.67,9.40,24.98,50.79)

r = c("A","A","A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B","C","C","D","E","E","E","F","G","G","H","H","H","H","I","I","I","J","J","J","J","K","L","L","L","L","L","L","L","L","L","L","L","L","L","L","M","N","N","N","N","N","O","P","P","P","P","P","Q","R","R","S","S","S","T","U","U","U","U","U","U","U","U","U","U","U","U","U","U","V","V","V","V","V","V","V","V","V","V","W","X","X","X","X","Y","Y","Z","Z","Z","Z","Z","Z","AA","AA","AA","AB","AB","AB","AB","AB","AB","AB","AB","AB","AB","AB","AB","AC","AC")

df = data.frame(x,y,r)

建立无"r"随机效应的非线性模型。

nls_test = nls(y~theta*(1-exp(-beta*x)),
              data = df,
              start = list(beta = 0.2, theta = 38), 
              trace = TRUE)

在我的模型中,唯一的固定效应是x,唯一的随机效应是r。我尝试构建了一个nlme()模型来反映这一点,该模型基于nlme包文档(https://cran.r-project.org/web/packages/nlme/nlme.pdf),更具体地说,是上面链接的文档第186页上的这些代码行。
我尝试用我的数据创建的nlme()对象如下所示:

nlme_test = nlme(y ~ theta*(1-exp(-beta*x)),
                 fixed = x~1,
                 random = r~1,
                 data = df,
                 start = c(theta = 38,
                           beta = 0.2))

并导致以下错误。
eval(预测值、数据、环境)出错:未找到对象'theta'
据我所知,这与"theta"未包含在用于构建nlme对象的 Dataframe ("df")中有关,但我不清楚为什么会发生这种情况,因为我找到的大多数错误示例都与predict()函数的使用和缺少列或列名之间的不一致有关。
此外,由于nls()模型(nls_Test)使用相同的start = c(theta = 38,beta = 0.2)并且df中没有"theta"或"beta"数据列,因此工作正常,我有点困惑为什么会收到有关列名错误的错误。
有人有建议或参考资料来帮助我将随机效应纳入我的nlme模型吗?
谢谢!

vptzau2j

vptzau2j1#

扩展我的(现在被删除,因为不完整)评论,我假设这是你想做的,请通过阅读关于nlme(iidee. ?nlme::nlme)的帮助页面仔细确认。

nlme_test <- nlme(y ~ theta*(1-exp(-beta*x)),
                 fixed = theta + beta ~ 1,
                 random = theta + beta ~ 1,
                 groups = ~ r,
                 data = df,
                 start = c(theta = 38,
                           beta = 0.2))

fixedrandom参数不应命名模型公式中的变量,而应命名回归参数。这样,函数就可以知道模型的哪些部分是变量(在data中找到),哪些部分是参数。此外,为了指定如何对数据进行聚类,您遗漏了groups参数。
输出:

summary(nlme_test)
## Nonlinear mixed-effects model fit by maximum likelihood
##   Model: y ~ theta * (1 - exp(-beta * x)) 
## Data: df 
##        AIC      BIC    logLik
##   887.6224 904.6401 -437.8112
## 
## Random effects:
##  Formula: list(theta ~ 1, beta ~ 1)
##  Level: r
##  Structure: General positive-definite, Log-Cholesky parametrization
##          StdDev       Corr 
## theta    1.145839e+01 theta
## beta     1.061366e-05 0.01 
## Residual 6.215030e+00      
## 
## Fixed effects: theta + beta ~ 1 
##           Value Std.Error DF  t-value p-value
## theta 21.532188 2.8853414 96 7.462614   0e+00
## beta   0.104404 0.0251567 96 4.150144   1e-04
##  Correlation: 
##      theta 
## beta -0.548
## 
## Standardized Within-Group Residuals:
##         Min          Q1         Med          Q3         Max 
## -2.89510795 -0.51882772 -0.09466037  0.34471808  3.66855121 
## 
## Number of Observations: 126
## Number of Groups: 29

相关问题