Using the fda package I have created the fd object called "curve":
splinebasis = create.bspline.basis(rangeval = c(0,100),
nbasis = 23,
norder = 4)
curve = smooth.basis(x, y, splinebasis)$fd
At this point I can easily plot my fd object through the command:
plot(curve)
Obtaining a fine result.
What I would like to do, is to plot the object using the ggplot2 package, but unfortunatelly I have no clue how to code the ggplot2 s.t. it uses the basis and coefficient to return the continuous curve*.
Here is a simple solution using predict
from the fda
package.
library(fda)
set.seed(1)
x <- 0:100
y <- cumsum(rnorm(101))
splinebasis <- create.bspline.basis(rangeval = c(0,100),
nbasis = 23,
norder = 4)
curve <- smooth.basis(x, y, splinebasis)
# Plot using base graphic engine
plot(curve$fd)
# Plot using ggplot2
library(ggplot2)
xx <- seq(0,100,0.1)
df <- data.frame(x=xx, yhat = predict(curve, newdata=xx))
ggplot(data=df, aes(x=x, y=yhat)) +
geom_line() +
geom_hline(aes(yintercept=0), linetype=2) +
labs(x="time", y="value") +
theme_bw()