二元运算符错误的非数字参数

Non-numeric argument to binary operator error

我收到错误消息“计算在 stat_function() 中失败: 当我尝试绘制此函数时,二元运算符的非数字参数。当我删除两个 exp 时,我反而收到错误 'Computation failed in stat_function(): could not find function "y2<-"' 我做错了什么?

我尝试了在线显示的 ggplot 函数的不同配置,但收到 'mapping must be in aes() or aes_()' 我不知道还有什么可以尝试的,因为我是 R 的新手。

Library(tidyverse)

calculate_blood_amount <- function(t, parms){

c_0 <- parms[1]
k_a <- parms[2]
k_c <- parms[3]

y2(t) <- ((k_a * c_0)/(k_c - k_a)) * exp^(-k_a * t) + ((-k_a * c_0)/(k_c - 
k_a)) * exp^(-k_c * t)

return(y2(t))

}

Time1 = data.frame(t = c(0, 24))

parms1 = c(500, 0.168, 1.436)

ggplot(data = Time1, aes(x = t)) +
stat_function(fun = calculate_blood_amount,
            args = list(parms = parms1)) +
 theme_bw()

当 t 为 0 和 24 时,这应该提供 y2(t) 的图表

我认为您的代码中存在一些语法错误。请参阅下面的工作版本。

exp^(xyz) 更改为 exp(xyz) 并且 y2(t) 变量名称更改为 y2_t.

library(tidyverse)

calculate_blood_amount <- function(t, parms){

  c_0 <- parms[1]
  k_a <- parms[2]
  k_c <- parms[3]

  y2_t <- ((k_a * c_0)/(k_c - k_a)) * exp(-k_a * t) + ((-k_a * c_0)/(k_c - k_a)) * exp(-k_c * t)

  return(y2_t)

}

Time1 = data.frame(t = c(0, 24))

parms1 = c(500, 0.168, 1.436)

ggplot(data = Time1, aes(x = t)) +
  stat_function(fun = calculate_blood_amount,
                args = list(parms = parms1)) +
  theme_bw()