英文:
Nonlinear mixed effects model - object not found
问题
我想在R中使用以下公式创建非线性混合效应模型:h=1.30+(dbh^2/(b0+b1*dbh)^2)
。
在这个方程中,b0是一个随机因子,计算方式如下:a0+a1*BA+a2*VH+a3*QM+a4*NH
,其中a0
、a1
、a2
、a3
、a4
是线性回归系数。
我咨询了不同的聊天机器人。它们推荐了不同的包和语法来执行此操作。但都没有成功运行。
感谢您的建议。
请看这段代码,它是由Bing聊天机器人创建的:
library(lme4)
# 定义非线性函数
h <- function(dbh, b0, b1) {
1.30 + (dbh^2 / (b0 + b1 * dbh)^2)
}
# 定义随机因子
b0 <- function(a0, a1, BA, a2, VH, a3, QM, a4, NH) {
a0 + a1 * BA + a2 * VH + a3 * QM + a4 * NH
}
# 定义一些起始值
start_list <- list(
# 非线性参数
nlpars = c(b1 = 0.5),
# 线性参数
theta = c(a0 = 0.1, a1 = 0.2, a2 = 0.3, a3 = 0.4, a4 = 0.5)
)
# 使用start参数拟合模型
model <- nlmer(h(dbh) ~ h(dbh, b0(a0, a1, BA, a2, VH, a3, QM, a4, NH), b1) ~ (a0 | ID), data = your_data, start = start_list)
运行这段代码会出现以下错误:
Error in h(dbh) : argument "b0" is missing, with no default
编辑:@GregorThomas
我将模型公式的左侧修改为:h(dbh, b0, b1)
。R给出了以下错误:
Error in eval(predvars, data, env) : object 'b1' not found.
这是dput命令的输出:
structure(list(dbh = c(22L, 87L, 110L, 27L, 69L, 34L, 49L, 89L,
49L, 87L, 46L, 66L, 74L, 48L, 81L, 26L, 89L, 66L, 100L, 50L,
74L, 29L, 82L, 58L, 75L, 63L, 100L, 54L, 54L, 64L), h = c(11.4,
32.1, 31.3, 35.3, 40, 23.6, 34, 35, 30, 43, 36, 41.3, 40, 33.3,
40.6, 21.4, 39.2, 42, 44, 36.5, 39.6, 24, 29, 39.5, 37, 42, 41.3,
34.5, 37, 40), BA = c(27.71, 27.71, 43.35, 43.35, 26.48, 26.48,
34.33, 34.33, 35.29, 35.29, 28.61, 28.61, 39.86, 18.1, 18.1,
29.8, 29.8, 30.2, 30.2, 47.1, 47.1, 30.69, 30.69, 31.78, 31.78,
45.49, 45.49, 9.34, 9.34, 34.65), VH = c(393.76, 393.76, 672.33,
672.33, 388.41, 388.41, 526.63, 526.63, 538.45, 538.45, 396.61,
396.61, 592.49, 249.89, 249.89, 445.99, 445.99, 466.5, 466.5,
710.59, 710.59, 447, 447, 477.61, 477.61, 726.05, 726.05, 122.4,
122.4, 553.31), QMD = c(36.8, 36.8, 53.9, 53.9, 43.3, 43.3, 54,
54, 51.4, 51.4, 36.1, 36.1, 62.5, 27.2, 27.2, 43.5, 43.5, 46.2,
46.2, 48, 48, 37.3, 37.3, 47.4, 47.4, 60.2, 60.2, 25.7, 25.7,
60.6), NH = c(260L, 260L, 190L, 190L, 180L, 180L, 150L, 150L,
170L, 170L, 280L, 280L, 130L, 310L, 310L, 200L, 200L, 180L, 180L,
260L, 260L, 280L, 280L, 180L, 180L, 160L, 160L, 180L, 180L, 120L
)), row.names = c(NA, 30L), class = "data.frame")
英文:
I want to create nonlinear mixed effect model in R using this formula: h=1.30+(dbh^2/(b0+b1*dbh)^2)
.
in this equation b0 is a random factor and is computed as: a0+a1*BA+a2*VH+a3*QM+a4*NH
where a0
, a1
, a2
, a3
, a4
are linear regression coefficients.
I have consulted with different chatbots. They recommend different packages and syntaxes for doing so. None of them run successfully.
Your recommendation appreciated.
look at this code which was created by bing chatbot:
library(lme4)
# Define the nonlinear function
h <- function(dbh, b0, b1) {
1.30 + (dbh^2 / (b0 + b1 * dbh)^2)
}
# Define the random factor
b0 <- function(a0, a1, BA, a2, VH, a3, QM, a4, NH) {
a0 + a1 * BA + a2 * VH + a3 * QM + a4 * NH
}
# Define some starting values
start_list <- list(
# Nonlinear parameters
nlpars = c(b1 = 0.5),
# Linear parameters
theta = c(a0 = 0.1, a1 = 0.2, a2 = 0.3, a3 = 0.4, a4 = 0.5)
)
# Fit the model using nlmer with start argument
model <- nlmer(h(dbh) ~ h(dbh, b0(a0, a1, BA, a2, VH, a3, QM, a4, NH), b1) ~ (a0 | ID), data = your_data, start = start_list)
Running this code gave me this error:
> Error in h(dbh) : argument "b0" is missing, with no default
Edit: @GregorThomas
I edited the left hand side of the model formula as: h(dbh, b0, b1). R gave me this error:
> Error in eval(predvars, data, env) : object 'b1' not found.
here is the output of dput command:
structure(list(dbh = c(22L, 87L, 110L, 27L, 69L, 34L, 49L, 89L,
49L, 87L, 46L, 66L, 74L, 48L, 81L, 26L, 89L, 66L, 100L, 50L,
74L, 29L, 82L, 58L, 75L, 63L, 100L, 54L, 54L, 64L), h = c(11.4,
32.1, 31.3, 35.3, 40, 23.6, 34, 35, 30, 43, 36, 41.3, 40, 33.3,
40.6, 21.4, 39.2, 42, 44, 36.5, 39.6, 24, 29, 39.5, 37, 42, 41.3,
34.5, 37, 40), BA = c(27.71, 27.71, 43.35, 43.35, 26.48, 26.48,
34.33, 34.33, 35.29, 35.29, 28.61, 28.61, 39.86, 18.1, 18.1,
29.8, 29.8, 30.2, 30.2, 47.1, 47.1, 30.69, 30.69, 31.78, 31.78,
45.49, 45.49, 9.34, 9.34, 34.65), VH = c(393.76, 393.76, 672.33,
672.33, 388.41, 388.41, 526.63, 526.63, 538.45, 538.45, 396.61,
396.61, 592.49, 249.89, 249.89, 445.99, 445.99, 466.5, 466.5,
710.59, 710.59, 447, 447, 477.61, 477.61, 726.05, 726.05, 122.4,
122.4, 553.31), QMD = c(36.8, 36.8, 53.9, 53.9, 43.3, 43.3, 54,
54, 51.4, 51.4, 36.1, 36.1, 62.5, 27.2, 27.2, 43.5, 43.5, 46.2,
46.2, 48, 48, 37.3, 37.3, 47.4, 47.4, 60.2, 60.2, 25.7, 25.7,
60.6), NH = c(260L, 260L, 190L, 190L, 180L, 180L, 150L, 150L,
170L, 170L, 280L, 280L, 130L, 310L, 310L, 200L, 200L, 180L, 180L,
260L, 260L, 280L, 280L, 180L, 180L, 160L, 160L, 180L, 180L, 120L
)), row.names = c(NA, 30L), class = "data.frame")
答案1
得分: 0
以下是翻译好的内容:
这里有几个问题。
按标准定义,这 不是 一个 "混合模型";混合模型总是涉及到一个 分组变量,并且允许顶层(线性或非线性模型)的参数在分组变量的各个级别之间变化。而你拥有的是一种层次模型,但第二级模型(即 b0
作为 BA
、VH
、QMD
、NH
函数的模型)是一个线性模型,而不是从联合分布中抽样。
你可以使用 nlme
包中的 gnls
("广义非线性最小二乘法")来完成这个任务:
library(nlme)
gnls(h ~ 1.30 + (dbh^2/(b0 + b1*dbh^2)),
params = list(b0 ~ BA + VH + QMD + NH, b1 ~ 1),
data = dd,
start = list (b0 = rep(1,5), b1 = 1))
绘制拟合(预测) vs 观察值图:
par(las = 1, bty = "l")
plot(predict(m1), dd$h)
abline(a=0, b= 1, col = 2, lwd = 2)
此外,你可能应该知道,无论是好是坏,在当前情况下 Stack Overflow 上由聊天机器人生成的代码是备受争议的 ...
英文:
There are several issues here.
This is not a "mixed model" by the standard definition; mixed models always involve a grouping variable, and allow parameters of the top-level (linear or nonlinear model) to vary across the levels of the grouping variable. What you have is a sort of hierarchical model, but the second-level model (i.e. the model for b0
as a function of BA
, VH
, QMD
, NH
) is a linear model, not a sample from a joint distribution.
You can get this done using gnls
("generalized nonlinear least squares"), from the nlme
package:
library(nlme)
gnls(h ~ 1.30 + (dbh^2/(b0 + b1*dbh^2)),
params = list(b0 ~ BA + VH + QMD + NH, b1 ~ 1),
data = dd,
start = list (b0 = rep(1,5), b1 = 1))
Plot fitted (predicted) vs observed:
par(las = 1, bty = "l")
plot(predict(m1), dd$h)
abline(a=0, b= 1, col = 2, lwd = 2)
You should also probably be aware that, for better or worse, chatbot-generated code is highly controversial on Stack Overflow at present ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论