## Generate example data
set.seed(1234)
t <- runif(1000, -10, 10)
y <- 2*sin(t) + -0.06*t^2 + rnorm(length(t))
## Fit model
model_fit <- lgspline(t, y)
## Generate predictions for new data
newdata <- matrix(sort(rnorm(10000)), ncol = 1) # Ensure matrix format
preds <- predict(model_fit, newdata)
## Compute derivative
deriv1_res <- predict(model_fit, newdata,
take_first_derivatives = TRUE)
deriv2_res <- predict(model_fit, newdata,
take_second_derivatives = TRUE)
## Visualize results
oldpar <- par(no.readonly = TRUE) # Save current par settings
layout(matrix(c(1,1,2,2,3,3), byrow = TRUE, ncol = 2))
## Plot function
plot(newdata[,1], preds,
main = 'Fitted Function',
xlab = 't',
ylab = "f(t)", type = 'l')
## Plot first derivative
plot(newdata[,1],
deriv1_res$first_deriv,
main = 'First Derivative',
xlab = 't',
ylab = "f'(t)", type = 'l')
## Plot second derivative
plot(newdata[,1],
deriv2_res$second_deriv,
main = 'Second Derivative',
xlab = 't',
ylab = "f''(t)", type = 'l')
par(oldpar) # Reset to original par settings
Run the code above in your browser using DataLab