0

Please, put in R the following structures:

g <- c(0.00125277273229879, 0.00126154615176554, 0.0012703959424814, 
0.00127932294600281, 0.00128832795839672, 0.00129741176435766, 
0.00130657513720778, 0.00131581896967928, 0.00132514399529097, 
0.00133455111246124, 0.00134404117411893, 0.0013536149592724, 
0.00136327342888814, 0.00137301739609167, 0.00138284774792897, 
0.00139276542262179, 0.00140277127309897, 0.00141286625464083, 
0.00142305128272433, 0.0508421359805228, 0.00186998245686827, 
0.00188362983020243, 0.00189739567535365, 0.00191128104995371, 
0.00192528723908206, 0.00193941540840815, 0.00195366674065997, 
0.00196804251523079, 0.00198254393190719, 0.0019971722359653, 
0.00201192874091554, 0.00202681464654449, 0.0020418313232245, 
0.00205698005034884, 0.00207226214711413, 0.002087678932717, 
0.00210323179458836, 0.00211892205192484, 0.00213475116039167, 
-0.114451596942742, 0.00120694924932032, 0.00121602907494663, 
0.00122519601872553, 0.00123445103025114, 0.00124379508186234, 
0.00125322929373903, 0.00126275455292721, 0.00127237205352726, 
0.00128208273376092, 0.00129188771380808)

gx <- c(122.05, 122.1, 122.15, 122.2, 122.25, 122.3, 122.35, 122.4, 
122.45, 122.5, 122.55, 122.6, 122.65, 122.7, 122.75, 122.8, 122.85, 
122.9, 122.95, 123, 123.05, 123.1, 123.15, 123.2, 123.25, 123.3, 
123.35, 123.4, 123.45, 123.5, 123.55, 123.6, 123.65, 123.7, 123.75, 
123.8, 123.85, 123.9, 123.95, 124, 124.05, 124.1, 124.15, 124.2, 
124.25, 124.3, 124.35, 124.4, 124.45, 124.5)

I would like to find a function which interpolates g ~ gx, but it should have some particular features:

  • it should be expressed in functional form, e.g. if it's a polynomial it should return me poly degree and coefficents (if it was asymptotically Normal it would be great!).
  • I would need to integrate the functional form, then I would appreciate if someone can tell me if R can solve non-numerical integrals.

As an example of the second feature, consider

y <- function(x) {
 x ^ 2
}

Is there any way integrating y with R can return me (x ^ 3) / 3 instead of a numerical value evaluated between integral extremes?

Thanks,

Lisa Ann
  • 3,345
  • 6
  • 31
  • 42

1 Answers1

1

If you can stand linear interpolation, then approxfun will return a function which calculates interpolated values. You may prefer to use lm followed by predict acting on the object which lm returns.

And, as JG pointed out in a comment, if your data set has "sharp curves" or is not densely packed, splinefun will probably give you a better fitting function.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • Would `approxfun()` be better than `splinefun()` to approximate something like a density function (that is something which needs to be as smooth as possible)? – Lisa Ann Oct 16 '12 at 11:27
  • @JohnGay Good point. I knew I was leaving out something useful. Just keep in mind that a spline fit is only as good as the parameters you give it :-) . The OP's data set is pretty closely spaced, which is why I started w/ a linear interpolation. – Carl Witthoft Oct 16 '12 at 12:22