0
votes

I receive the error message 'Computation failed in stat_function(): non-numeric argument to binary operator' when I attempt to plot this function. When I remove the two exp's I instead receive the error 'Computation failed in stat_function(): could not find function "y2<-"' What am I doing wrong?

I have tried different configurations of the ggplot function shown online but receive the 'mapping must be in aes() or aes_()' I don't know much else to try as I am new to 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()

This should provide a graph of y2(t) when t is 0 and 24

1

1 Answers

0
votes

I think you just have a couple of syntax errors in your code. See below for a working version.

exp^(xyz) changed to exp(xyz) and y2(t) variable name changed to 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()