r - 如何提取拟合 nls 模型中使用的参数,以便在 do.call 的第二次拟合中使用

标签 r nls do.call

我正在尝试使用来自拟合的 nls 模型的相同原始参数来拟合使用数据子集的第二个模型(用于交叉验证练习)。我可以检索参数(例如 fit$call),但很难将这些参数传递给 do.call

# original model ----------------------------------------------------------
# generate data
set.seed(1)
n <- 100
x <- sort(rlnorm(n, 1, 0.2))
y <- 0.1*x^3 * rlnorm(n, 0, 0.1)
df <- data.frame(x, y)
plot(y~x,df)

# fit model
fit <- nls(y ~ a*x^b, data=df, start=list(a=1,b=2), lower=list(a=0, b=0), algo="port")
summary(fit)
plot(y~x,df)
lines(df$x, predict(fit), col=4)



# perform model fit on subset with same starting arguments ----------------
# df sampled subset
dfsub <- df[sample(nrow(df), nrow(df)*0.5),]
dim(dfsub)
plot(y~x, dfsub)

ARGS <- fit$call # original call information
ARGS$data <- dfsub # substitute dfsub as data
ARGS <- as.list(ARGS) # change class from "call", to "list"

fitsub <- do.call(nls, args = ARGS )
# Error in xj[i] : invalid subscript type 'closure'

此外,作为旁注,fit$data 仅返回数据对象的名称。数据实际上也包含在拟合的 nls 对象中(就像 lm 和其他模型拟合有时所做的那样)?

最佳答案

使用update添加子集参数:

nr <- nrow(df)
update(fit, subset = sample(nr, nr * 0.5) )

关于r - 如何提取拟合 nls 模型中使用的参数,以便在 do.call 的第二次拟合中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36330017/

相关文章:

r - 从字符串中提取十进制数

localization - 使用 NLS/本地化的 Dojo 自定义构建

oracle - ORA-00604 ORA-12705

r - 如何结合 do.call() plot() 和 expression()

r - 在 R 中合并 CSV 文件和不匹配的文件头

r - CSV 文件读取问题

r - 比较字符串与 R 中的逻辑运算符

r - 在 R 中加载具有最新日期的文件

r - 如何向ggplot添加对数非线性拟合?

r - 了解 do.call() 在数据帧行上对 'paste' 与 'function(x) paste(x)' 的处理