r - 如何在长数据帧的数据子集中应用非线性回归?
问题描述
我有一个数据框,其中包含来自不同实验的多个结果。我想将每个实验的数据分别拟合到一个特定的函数,但是,我的 R 知识很浅。有什么帮助吗?
在下面的 MWE 中,参数的起始值是针对ex1
数据的
ex <- c(rep("ex1", times=4), rep("ex2", times=4))
x <- rep(c(0,60,120,240), times = 2)
y <- c(0,3.73,3.08,4.07,0,1.4,2.6,2.6)
df <- data.frame(ex, x, y)
m <- nls(formula = y ~ ((a^2)*b*x)/(1+(a*b*x)),
data = df,
start = list(a=4.071, b=0.0253))
起始值:
对于 ex1: a = 4.071
,b = 0.0253
对于 ex2: a = 2.584
,b = 0.0155
解决方案
1) 一个 nls 使用分组它可以在一个 nls 调用中完成,如下所示:
fo <- y ~ ((a[ex]^2)*b[ex]*x)/(1+(a[ex]*b[ex]*x))
st <- list(a = c(4.071, 2.504), b = c(0.0253, 0.0155))
m <- nls(fo, df, start = st)
m
给予:
Nonlinear regression model
model: y ~ ((a[ex]^2) * b[ex] * x)/(1 + (a[ex] * b[ex] * x))
data: df
a1 a2 b1 b2
3.76849 3.58989 0.06521 0.00385
residual sum-of-squares: 0.6989
Number of iterations to convergence: 8
Achieved convergence tolerance: 4.731e-06
2)映射或者我们可以遍历对每个组执行单独的 nls 的组。st
是从上面。
fo1 <- y ~ ((a^2)*b*x)/(1+(a*b*x))
st1 <- split(as.data.frame(st), levels(df$ex))
L <- Map(nls, data = split(df, df$ex), st1, MoreArgs = list(formula = fo1))
L
给出这个nls
对象列表:
$ex1
Nonlinear regression model
model: y ~ ((a^2) * b * x)/(1 + (a * b * x))
data: dots[[1L]][[2L]]
a b
3.76848 0.06521
residual sum-of-squares: 0.4922
Number of iterations to convergence: 7
Achieved convergence tolerance: 3.774e-06
$ex2
Nonlinear regression model
model: y ~ ((a^2) * b * x)/(1 + (a * b * x))
data: dots[[1L]][[2L]]
a b
3.58989 0.00385
residual sum-of-squares: 0.2067
Number of iterations to convergence: 9
Achieved convergence tolerance: 3.302e-06
3)更新第三种方法是首先运行组合的nls,然后为每个组设置子集。 fo1
并且st
是从上面。
m2 <- nls(fo1, df, start = sapply(st, mean))
lvs <- levels(df$ex)
L2 <- lapply(lvs, function(lv) do.call("update", list(m2, subset = df$ex == lv)))
它还提供了一个nls
对象列表。
4) nlsList 另一种方法是使用nlme包中的nlsList。st
是从上面。
library(nlme)
fo2 <- y ~ ((a^2) * b * x)/(1 + (a * b * x)) | ex
m3 <- nlsList(fo2, df, start = sapply(st, mean))
m3
给出以下nlsList
对象:
Call:
Model: y ~ ((a^2) * b * x)/(1 + (a * b * x)) | ex
Data: df
Coefficients:
a b
ex1 3.768487 0.065212898
ex2 3.589892 0.003850294
Degrees of freedom: 8 total; 4 residual
Residual standard error: 0.4179985
残差平方和
对于 (1),残差平方和为
tapply(resid(m)^2, df$ex, sum)
对于 (2) 它是:
sapply(L, deviance)
并且对于 (3) 和 (4) 与 L2 或 m3 代替 L 相同。
推荐阅读
- arrays - 按日期和时间对集合进行排序
- python - 训练测试拆分中 Shuffle 和 Random_State 的区别?
- android - 使用 udpsrc 播放的 Gstreamer 视频在 Android 上几秒钟后停止
- reactjs - 用于获取调用的开玩笑单元测试用例反应本机
- flutter - Flutter video_player 配置
- mongodb - Mongodb:编写过滤器查询以使用 pymongo 搜索特定数据
- c++ - 如何在我的整数之间包含空格作为输出
- r - 尝试使用 i 多次迭代我的函数
- powershell - Get-AdUser 无法转换为 system.string 类型
- r - R将函数列表应用于矩阵的每一行,函数将行作为输入