I have the following example code:
library(mfp)
library(ggplot2)
duration <- sample(c(3, 5, 7, 10, 12, 14), 500, TRUE)
data <- data.frame(duration = duration,
score = -0.0125*duration^2 + 0.25*duration - 0.4 + rnorm(500, 0, 0.1))
mfp1 <- mfp(score ~ fp(duration), data)
ggplot(data, aes(x = duration, y = score)) +
geom_point() +
geom_smooth(method = "glm",
formula = mfp1$formula)
where I am generating some example data, fitting a fractional polynomial then plotting the scatter plot with the function on top. I am getting an error, as the formula
parameter in geom_smooth
needs to take a formala in the form y ~ x
, whereas the formula from mfp1
is score ~ I((duration/10)^1) + I((duration/10)^2)
.
Is there a way to convert the formula from mfp1
into a generic xy format for this use?
NB this is example data, the real application will be within a package, and therefore necessitates a general solution.