How to generate natural cubic spline in R?

9 messages
Open this post in threaded view
|
Report Content as Inappropriate

How to generate natural cubic spline in R?

 Suppose I have two var x and y,now I want to fits a natural cubic spline in x to y,at the same time create new var containing the smoothed values of y. How can I get it? ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 If one enters: ??"spline" ... You get quite a few matches. The one in the stats functions that   probably answers your specific questions is: "splinefun {stats}                  R Documentation Interpolating Splines               Description Perform cubic (or Hermite) spline interpolation of given data points,   returning either a list of points obtained by the interpolation or a   function performing the interpolation." "splinefun returns a function with formal arguments x and deriv, the   latter defaulting to zero. This function can be used to evaluate the   interpolating cubic spline (deriv=0), or its derivatives (deriv=1,2,3)   at the points x, where the spline function interpolates the data   points originally specified. This is often more useful than spline." Perhaps you need to review from you basic intro material regarding help.search("text")     # or ??"text"    # possibilities. -- David Winsemius On Mar 30, 2009, at 10:58 PM, minben wrote: > Suppose I have two var x and y,now I want to fits a natural cubic > spline in x to y,at the same time create new var containing the > smoothed values of y. How can I get it? > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 Hi, if you are looking for *natural* cubic splines (linear beyond the outer knots), you could use rcs() in Frank Harrell's Design package. HTH, Stephan David Winsemius schrieb: > If one enters: > > ??"spline" > > ... You get quite a few matches. The one in the stats functions that > probably answers your specific questions is: > > "splinefun {stats}                  R Documentation > Interpolating Splines               Description > Perform cubic (or Hermite) spline interpolation of given data points, > returning either a list of points obtained by the interpolation or a > function performing the interpolation." > > "splinefun returns a function with formal arguments x and deriv, the > latter defaulting to zero. This function can be used to evaluate the > interpolating cubic spline (deriv=0), or its derivatives (deriv=1,2,3) > at the points x, where the spline function interpolates the data points > originally specified. This is often more useful than spline." > > Perhaps you need to review from you basic intro material regarding > help.search("text")     # or > ??"text"    # possibilities. > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 In reply to this post by minben Hello:       If B-splines will suffice, there are many capabilities in R for that.  My favorite is the 'fda' package, but 'splines' and other packages are also good.       The "splinefun" function in the "base" package returns a function to compute spline interpolations optionally using a natural spline.   However, that is an interpolation spline and therefore does no smoothing.       To find other options for natural splines, I suggest you try the CRAN packages splines, mboost, pspline, Design, and mgcv.  I found them using the "RSiteSearch" packages available from R-Forge via 'install.packages("RSiteSearch", repos="http://r-forge.r-project.org")', which also identified the "siggenes" package (which is not on CRAN).  I don't know if any of these actually use natural splines, but this gives you a reasonably short list to consider.       Hope this helps.       Spencer Graves p.s.  The following are the commands I used with the "RSiteSearch" package:     library(RSiteSearch) natSpl <- RSiteSearch.function('natural spline') str(natSpl) summary(natSpl) natSpl[1:23, c(1, 4, 5, 7)]       minben wrote: > Suppose I have two var x and y,now I want to fits a natural cubic > spline in x to y,at the same time create new var containing the > smoothed values of y. How can I get it? > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 In reply to this post by Stephan Kolassa The splinefun documentation indicates that "natural" is one of the   types of cubic spline options available. Does rcs actually do fitting? Such would not be my expectation on   reading the documentation and I do not see any examples of such   functionality in the help pages. -- David Winsemius On Mar 31, 2009, at 6:09 AM, Stephan Kolassa wrote: > Hi, > > if you are looking for *natural* cubic splines (linear beyond the   > outer knots), you could use rcs() in Frank Harrell's Design package. > > HTH, > Stephan > > > David Winsemius schrieb: >> If one enters: >> ??"spline" >> ... You get quite a few matches. The one in the stats functions   >> that probably answers your specific questions is: >> "splinefun {stats}                  R Documentation >> Interpolating Splines               Description >> Perform cubic (or Hermite) spline interpolation of given data   >> points, returning either a list of points obtained by the   >> interpolation or a function performing the interpolation." >> "splinefun returns a function with formal arguments x and deriv,   >> the latter defaulting to zero. This function can be used to   >> evaluate the interpolating cubic spline (deriv=0), or its   >> derivatives (deriv=1,2,3) at the points x, where the spline   >> function interpolates the data points originally specified. This is   >> often more useful than spline." >> Perhaps you need to review from you basic intro material regarding >> help.search("text")     # or >> ??"text"    # possibilities. > > David Winsemius, MD Heritage Laboratories West Hartford, CT ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 Hi David, David Winsemius schrieb: > The splinefun documentation indicates that "natural" is one of the types > of cubic spline options available. That sounds good, didn't know that... rcs() has the advantage of coming with a book (Harrell's "Regression Modeling Strategies"). > Does rcs actually do fitting? Such would not be my expectation on > reading the documentation and I do not see any examples of such > functionality in the help pages. Nope, but you can include rcs() within fitting functions, lm(foo~rcs(bar,3)), which makes more sense to me than having a spline function fit... Looks like better encapsulation to me. Best, Stephan ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 In reply to this post by David Winsemius David Winsemius wrote: > The splinefun documentation indicates that "natural" is one of the types > of cubic spline options available. > > Does rcs actually do fitting? Such would not be my expectation on > reading the documentation and I do not see any examples of such > functionality in the help pages. It expands a predictor into the truncated power basis functions for the natural cubic spline.  This is an easier basis to use than the B-spline basis.  rcs calls the Hmisc function rcspline.eval which you may want to look at first. Frank -- Frank E Harrell Jr   Professor and Chair           School of Medicine                       Department of Biostatistics   Vanderbilt University ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. Frank Harrell Department of Biostatistics, Vanderbilt University
Open this post in threaded view
|
Report Content as Inappropriate

Re: How to generate natural cubic spline in R?

 In reply to this post by Stephan Kolassa >>>>> "SK" == Stephan Kolassa <[hidden email]> >>>>>     on Tue, 31 Mar 2009 22:20:50 +0200 writes:     SK> Hi David,     SK> David Winsemius schrieb:     >> The splinefun documentation indicates that "natural" is one of the types     >> of cubic spline options available.     SK> That sounds good, didn't know that... rcs() has the advantage of coming     SK> with a book (Harrell's "Regression Modeling Strategies"). well,  and  splinefun() comes with R !!!     >> Does rcs actually do fitting? Such would not be my expectation on     >> reading the documentation and I do not see any examples of such     >> functionality in the help pages.     SK> Nope, but you can include rcs() within fitting functions,     SK> lm(foo~rcs(bar,3)), which makes more sense to me than having a spline     SK> function fit... Looks like better encapsulation to me. well,  but  ns()  has been part of the S language for ages, and part of R since its very early days, exactly for the purpose to be used in something like           lm(y ~ ns(.) + ..) So while rcs() may have extra merits, (I assume it does, as I assume Frank Harrell will have known about ns()), in order to solve the OP's problem, I still believe that "standard R" [and "good old S version 3 for that matter] contains all functionality needed. Martin Maechler, ETH Zurich     SK> Best,     SK> Stephan     SK> ______________________________________________     SK> [hidden email] mailing list     SK> https://stat.ethz.ch/mailman/listinfo/r-help    SK> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html    SK> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.