如何在 dplyr 中 运行 nlxb 和 wrapnls?
How to run nlxb and wrapnls inside dplyr?
我正在尝试使用 wrapnls
并行使用 dplyr
和 broom
(最终 mclapply
)来拟合许多非线性拟合,但我得到了解析评估来自 nlxb
的错误:
Error in parse(text = joe) (from #11) : <text>:1:6: unexpected input
1: b1.10% <- 20
我在使用 do
和 lapply
两种方法时都遇到了这个错误。
library(nlmrt)
library(dplyr)
library(purrr)
library(broom)
data_frame(x = seq(0, 200, 0.1),
y = 1.2*exp(-(times - 10)^2/(2*4.2^2)) + 2.4*exp(-(times - 50)^2/(2*3.8^2)) + 5.3*exp(-(times - 80)^2/(2*5.1^2)) + rnorm(length(times), sd = 0.05)) %>%
do({
xl <- quantile(.$x, 0.1, na.rm = TRUE)
xm <- quantile(.$x, 0.5, na.rm = TRUE)
xh <- quantile(.$x, 0.8, na.rm = TRUE)
starts <- c(a1 = 5, a2 = 5, a3 = 5,
b1 = xl, b2 = xm, b3 = xh,
c1 = 5, c2 = 5, c3 = 5)
fmla <- y ~ a1*exp(-(x - b1)^2/(2*c1^2)) + a2*exp(-(x - b2)^2/(2*c2^2)) + a3*exp(-(x - b3)^2/(2*c3^2))
df <- data_frame(x = .$x, y = .$y)
mod <- wrapnls(fmla, lower = 0, upper = 200, start = starts, data = df)
tidy(mod)
})
有什么办法解决这个问题吗?
问题不在于 do
方面,而是 do 中的代码,因此您可以直接调试该部分。 starts
向量正在获取与分位数连接的 b#
名称:
names(starts)
## [1] "a1" "a2" "a3" "b1.10%" "b2.50%" "b3.80%" "c1" "c2" "c3"
在分位数计算中添加 unname
解决了这个问题。
data_frame(x = seq(0, 200, 0.1),
y = 1.2*exp(-(x - 10)^2/(2*4.2^2)) + 2.4*exp(-(x - 50)^2/(2*3.8^2)) + 5.3*exp(-(x - 80)^2/(2*5.1^2)) + rnorm(length(x), sd = 0.05)) %>%
do({
xl <- quantile(.$x, 0.1, na.rm = TRUE) %>% unname()
xm <- quantile(.$x, 0.5, na.rm = TRUE) %>% unname()
xh <- quantile(.$x, 0.8, na.rm = TRUE) %>% unname()
starts <- c(a1 = 5, a2 = 5, a3 = 5,
b1 = xl, b2 = xm, b3 = xh,
c1 = 5, c2 = 5, c3 = 5)
fmla <- y ~ a1*exp(-(x - b1)^2/(2*c1^2)) + a2*exp(-(x - b2)^2/(2*c2^2)) + a3*exp(-(x - b3)^2/(2*c3^2))
df <- data_frame(x = .$x, y = .$y)
mod <- wrapnls(fmla, lower = 0, upper = 200, start = starts, data = df)
tidy(mod)
})
## term estimate std.error statistic p.value
## 1 a1 2.386492 0.007455097 320.1155 0
## 2 a2 5.296250 0.006437509 822.7174 0
## 3 a3 1.199384 0.007132559 168.1562 0
## 4 b1 49.997697 0.013702894 3648.6960 0
## 5 b2 80.004023 0.007150546 11188.5193 0
## 6 b3 10.077847 0.028644821 351.8209 0
## 7 c1 3.798829 0.013702940 277.2273 0
## 8 c2 5.094727 0.007150573 712.4921 0
## 9 c3 4.175235 0.028944448 144.2499 0
我正在尝试使用 wrapnls
并行使用 dplyr
和 broom
(最终 mclapply
)来拟合许多非线性拟合,但我得到了解析评估来自 nlxb
的错误:
Error in parse(text = joe) (from #11) : <text>:1:6: unexpected input
1: b1.10% <- 20
我在使用 do
和 lapply
两种方法时都遇到了这个错误。
library(nlmrt)
library(dplyr)
library(purrr)
library(broom)
data_frame(x = seq(0, 200, 0.1),
y = 1.2*exp(-(times - 10)^2/(2*4.2^2)) + 2.4*exp(-(times - 50)^2/(2*3.8^2)) + 5.3*exp(-(times - 80)^2/(2*5.1^2)) + rnorm(length(times), sd = 0.05)) %>%
do({
xl <- quantile(.$x, 0.1, na.rm = TRUE)
xm <- quantile(.$x, 0.5, na.rm = TRUE)
xh <- quantile(.$x, 0.8, na.rm = TRUE)
starts <- c(a1 = 5, a2 = 5, a3 = 5,
b1 = xl, b2 = xm, b3 = xh,
c1 = 5, c2 = 5, c3 = 5)
fmla <- y ~ a1*exp(-(x - b1)^2/(2*c1^2)) + a2*exp(-(x - b2)^2/(2*c2^2)) + a3*exp(-(x - b3)^2/(2*c3^2))
df <- data_frame(x = .$x, y = .$y)
mod <- wrapnls(fmla, lower = 0, upper = 200, start = starts, data = df)
tidy(mod)
})
有什么办法解决这个问题吗?
问题不在于 do
方面,而是 do 中的代码,因此您可以直接调试该部分。 starts
向量正在获取与分位数连接的 b#
名称:
names(starts)
## [1] "a1" "a2" "a3" "b1.10%" "b2.50%" "b3.80%" "c1" "c2" "c3"
在分位数计算中添加 unname
解决了这个问题。
data_frame(x = seq(0, 200, 0.1),
y = 1.2*exp(-(x - 10)^2/(2*4.2^2)) + 2.4*exp(-(x - 50)^2/(2*3.8^2)) + 5.3*exp(-(x - 80)^2/(2*5.1^2)) + rnorm(length(x), sd = 0.05)) %>%
do({
xl <- quantile(.$x, 0.1, na.rm = TRUE) %>% unname()
xm <- quantile(.$x, 0.5, na.rm = TRUE) %>% unname()
xh <- quantile(.$x, 0.8, na.rm = TRUE) %>% unname()
starts <- c(a1 = 5, a2 = 5, a3 = 5,
b1 = xl, b2 = xm, b3 = xh,
c1 = 5, c2 = 5, c3 = 5)
fmla <- y ~ a1*exp(-(x - b1)^2/(2*c1^2)) + a2*exp(-(x - b2)^2/(2*c2^2)) + a3*exp(-(x - b3)^2/(2*c3^2))
df <- data_frame(x = .$x, y = .$y)
mod <- wrapnls(fmla, lower = 0, upper = 200, start = starts, data = df)
tidy(mod)
})
## term estimate std.error statistic p.value
## 1 a1 2.386492 0.007455097 320.1155 0
## 2 a2 5.296250 0.006437509 822.7174 0
## 3 a3 1.199384 0.007132559 168.1562 0
## 4 b1 49.997697 0.013702894 3648.6960 0
## 5 b2 80.004023 0.007150546 11188.5193 0
## 6 b3 10.077847 0.028644821 351.8209 0
## 7 c1 3.798829 0.013702940 277.2273 0
## 8 c2 5.094727 0.007150573 712.4921 0
## 9 c3 4.175235 0.028944448 144.2499 0