0

我正在尝试使用and (并最终)wrapnls并行使用许多非线性拟合,但是我从以下位置得到解析评估错误:dplyrbroommclapplynlxb

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)
  })

有没有办法解决?

4

1 回答 1

1

问题不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
于 2017-08-28T14:26:33.543 回答