R拟合多项式通过(a)定点数据

时间:2022-01-13 21:21:33

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.

我遇到了一个非常具体的问题,我必须找到一个函数来描述一个植物的(标准化的)叶子形状。问题不只是找到最能描述数据的多项式,而且它从(0,0)开始(1,0)结束,并通过最大宽度(x_ymax, 1)的点移动,而不会变宽。

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.

我尝试的另一种选择是Hermite插值,使用这3个特定点作为控制点,但是它提供的功能是脱离了叶子的实际形状,除非我提供更多的控制点。

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!

提前谢谢!

1 个解决方案

#1


1  

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.).

我不确定这是否可行,但这里有一个使用循环样条的“广义相加模型”的例子。当你指定模型不应该有一个截距(即在公式中包含-1时,它应该通过y=0)。您将不得不将预测变量的大小调整为0到1之间,以便最终通过您所提到的点(参见此处了解更多信息)。

Example

# 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)

R拟合多项式通过(a)定点数据

#1


1  

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.).

我不确定这是否可行,但这里有一个使用循环样条的“广义相加模型”的例子。当你指定模型不应该有一个截距(即在公式中包含-1时,它应该通过y=0)。您将不得不将预测变量的大小调整为0到1之间,以便最终通过您所提到的点(参见此处了解更多信息)。

Example

# 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)

R拟合多项式通过(a)定点数据