是否有一个R库可以估算多元自然三次样条函数(或类似函数)? [英] Is there an R library that estimates a multivariate natural cubic spline (or similar) function?
问题描述
note: originally posted on Cross Validated (stats SE) on 07-26-2011, with no correct answers to date.
背景
我有一个模型, f ,其中 Y = f( X )
I have a model, f, where Y=f(X)
X 是来自 m 参数和 Y 的样本的 nxm 矩阵是模型输出的 nx 1 向量.
X is an n x m matrix of samples from m parameters and Y is the n x 1 vector of model outputs.
f 是计算密集型的,所以我想通过( X ,Y)使用多元三次样条来近似 f 点,这样我就可以在更多点上评估 Y .
f is computationally intensive, so I would like to approximate f using a multivariate cubic spline through (X,Y) points, so that I can evaluate Y at a larger number of points.
问题
是否有一个R函数可以计算 X 和 Y 之间的任意关系?
Is there an R function that will calculate an arbitrary relationship between X and Y?
具体来说,我正在寻找splinefun
函数的多元版本,该函数针对单变量情况生成样条函数.
Specifically, I am looking for a multivariate version of the splinefun
function, which generates a spline function for the univariate case.
例如这是splinefun
在单变量情况下的工作方式
e.g. this is how splinefun
works for the univariate case
x <- 1:100
y <- runif(100)
foo <- splinefun(x,y, method = "monoH.FC")
foo(x) #returns y, as example
该函数精确地插入点的测试成功:
The test that the function interpolates exactly through the points is successful:
all(y == foo(1:100))
## TRUE
我尝试过的事情
我已经查看了 mda 软件包,似乎应该可以进行以下操作:
I have reviewed the mda package, and it seems that the following should work:
library(mda)
x <- data.frame(a = 1:100, b = 1:100/2, c = 1:100*2)
y <- runif(100)
foo <- mars(x,y)
predict(foo, x) #all the same value
但是该功能不能在设计点上精确插值:
however the function does not interpolate exactly through the design points:
all(y == predict(foo,x))
## FALSE
我也找不到在gam
,marss
或earth
程序包中实现三次样条的方法.
I also could not find a way to implement a cubic-spline in either the gam
, marss
, or earth
packages.
推荐答案
实际上,有几个软件包可以做到这一点.我使用的是具有rcs
的"rms"软件包,但是生存软件包也具有pspline
,而样条软件包具有ns
函数{}. 自然样条线"(由ns构成)也是三次样条线.您将需要在多变量公式中使用"*"运算符来形成多变量拟合函数,以创建交叉"样条项.
您提供的示例不够丰富.
Actually several packages can do it. The one I use is the "rms" package which has rcs
, but the survival package also has pspline
and the splines package has the ns
function {}. "Natural splines" (constructed with ns) are also cubic splines. You will need to form multivariate fitting function with the '*' operator in the multivariate formula creating "crossed" spline terms.
that the example you offered was not sufficiently rich.
我想我很困惑您想要完全适合的人. R是一个统计包.近似估计是目标.通常,精确拟合是一个更大的问题,因为它们会导致多重共线性.
I guess I am confused that you want exact fits. R is a statistical package. Approximate estimation is the goal. Generally exact fits are more of a problem because they lead to multicollinearity.
这篇关于是否有一个R库可以估算多元自然三次样条函数(或类似函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!