Search code examples
rcurve

Using the curve() function to show changes in a logistic curve


I have a question asking to use curve()or geom_function() to show how the logistic curve changes as each parameter (a and b) varies from positive to negative.

They gave a logistic curve of: y = (exp(a + bx)/1 + exp(a + bx)

Within the curve() function I can see where to put the expression but it is not recognizing when I put this equation in.

curve(expr, from = NULL, to = NULL, n = 101, add = FALSE,
  type = "l", xname = "x", xlab = xname, ylab = NULL,
  log = NULL, xlim = NULL, …)

I have tried: curve(expr = (exp(a + bx)/1 + exp(a + bx)), from = -10, to = 10, n = 10)

where I made up numbers for the range and the n. But it does not recognize it as a function or expression. I have to somehow be able to make two plots, one where a varies, and one where b varies over a number of values.

Does anyone know how to input the expression into the formula and then manipulate the variables?


Solution

  • You must separate * and / properly. Define curve as lg and then by loop, draw curve.

    lg <- function(x, a = 1, b = 1){
      exp(a+b*x) / (1+exp(a+b*x))
    }
    # curves with changing b
    for (b in c(1:5)){
      curve(expr = lg(x, 1, b), from = -5, to = 5, n = 100, add= TRUE, col = b)
    }
    

    b

    # curves with changing a
    for (a in c(1:5)){
      curve(expr = lg(x, a, 1), from = -5, to = 5, n = 100, add= TRUE, col = a)
    }
    

    a