Say I have a data frame coefs
where each row contains model coefficients for a curve.
coefs <- structure(list(a1 = c(1.22228259789383, 1.2064168157394, 1.09555089661994, 0.943947433470916, 0.883490658557721, 0.46125552320107), d = c(0.385227755933488, 0.457271644919152, 0.340063262461958, 0.305629949064525, 0.42459163183877, 0.425710112988664), g = c(0, 0, 0, 0, 0, 0), u = c(1, 1, 1, 1, 1, 1)), .Names = c("a1", "d", "g", "u"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -6L))
I'd like to use each row of the data frame to add a new curve to the plot based a defined function: (you may recognize it as a 2PL item response model)
TWOPL <- function(x,a1,b) {
1 / (1 + exp(-a1*(x-(b))))
}
Based on this and this question I tried the following ggplot command but get the error that computation failed:
library(ggplot2)
p <- ggplot(coefs, aes(x = 0))
p + stat_function(fun = TWOPL) + xlim(-5,5)
I know that I need a way to give the various coefficients to the function. As a test, I tried the function with a fixed parameters to create 1 curve and it works, for example:
#1 curve based on fixed parameters
TWOPL_copy <- function(x) {
1 / (1 + exp(-1.22*(x-(.385))))
}
p <- ggplot(data.frame(x = 0), aes(x = 0))
p + stat_function(fun = TWOPL_copy) + xlim(-5,5)
I'm wondering how I might send each row of the data frame to ggplot. An ideal next step would be to differentiate the colors of each line somehow.