Reputation: 402
I'm stuck at a very specific problem where I have to find a function describing the (normalized) leaf shape of a plant. The problem is not just to find the polynomial that best describes the data, but also that it starts at (0,0) ends at (1,0) and moves through the point of maximum width (x_ymax, 1) without ever going wider.
An alternate option I tried is Hermite interpolation, using those 3 specific points as control points but the function it provides is way off the actual shape of the leaf, unless I provide more control points.
Is there a specific function for this or do I need to make some manual conversion? Or would there be better or alternate options to tackling this problem?
Thanks in advance!
Upvotes: 0
Views: 763
Reputation: 11995
I'm not sure if this would always work, but here is an example of a "Generalized Additive Model" that uses a cyclic spline. When you specify that the model should not have an intercept (i.e. include -1
in formula, then it should pass through y=0. You will have to scale your predictor variable to be between 0 and 1 in order for the ends to pass through the points you mentioned (see here for more info.).
# required model
library(mgcv)
# make data
n <- 200
tmp <- seq(0,20*pi,,n)
x <- tmp / (2*pi)
mon <- x%%1
err <- rnorm(n, sd=0.5)
y <- sin(tmp) + err + 1
plot(x, y, t="l")
df <- data.frame(x, y, mon)
# GAM with intercept
fit1 <- gam(y ~ s(mon, bs = "cc", k = 12), data=df)
summary(fit1)
plot(fit1)
# GAM without intercept
fit2 <- gam(y ~ s(mon, bs = "cc", k = 12) - 1, data=df) # note "-1" for no intercept
summary(fit2)
plot(fit2)
Upvotes: 1