[R] problem with spline

Michael Bedward michael.bedward at gmail.com
Sat Oct 2 08:45:17 CEST 2010


I'm not overly familiar with the spilne function in R but I was under
the impression that for a hermite spline fit the xs had to be strictly
increasing (as in your toy example) whereas in your actual data the x
values increase then decrease.

Michael

On 2 October 2010 11:59, Matthew Finkbeiner
<matthew.finkbeiner at mq.edu.au> wrote:
> I am trying to interpolate missing values using spline and am running
> into some strange problems.
>
> first, this works just fine:
>
> x<- c(1:10, rep(NA, 3), 14:20)
> y<- c(rnorm(10), rep(NA,3), rnorm(7))
> s<- spline(x,y, n=length(x))
>
> plot(x,y)
> lines(s, col="blue")
>
> but look at what happens with my real data (sorry for the long vectors here):
>
> x<- c(-9.816, -9.801, -9.801, -9.795, -9.798, -9.788, -9.742, -9.731,
> -9.713, -9.669, -9.617, -9.534, -9.494, -9.496, -9.51, -9.506,
> -9.325, -9.13, -9.11, -9.085, -9.297, -9.812, -10.464, -11.146,
> -11.88, -12.645, -13.446, -14.273, -15.087, -15.884, -16.645,
> -17.326, -17.943, -18.421, -18.777, -18.937, -18.895, -18.675,
> -18.268, -17.691, -16.899, -15.944, -14.805, -13.464, -11.948,
> -10.276, -8.446, -6.457, -4.333, -2.08, 0.32, 2.826, 5.478, 8.287,
> 11.212, 14.251, 17.421, 20.695, 24.098, 27.577, 31.129, 34.758,
> 38.451, 42.213, 46.059, 49.983, 53.994, 58.039, 62.101, 66.146,
> 70.18, 74.193, 78.202, 82.2, 86.118, 90.026, 93.879, 97.654,
> 101.407, 105.092, 108.771, 112.43, 116.029, 119.594, 123.085,
> 126.466, 129.719, 132.929, 136.054, 139.171, 142.304, 145.433,
> 148.571, 151.734, 154.868, 157.928, 160.975, 164.032, 167.076,
> 170.2, 173.202, 176.351, 179.666, 183.066, 186.481, 189.87, 193.187,
> 196.481, 199.737, 202.923, 206.087, 209.163, 212.239, 215.281,
> 218.178, 220.938, 223.657, 226.244, 228.697, 231.093, 233.407,
> 235.68, 237.892, 240.027, 242.067, 243.99, 245.799, 247.47, 248.961,
> 250.272, 251.309, 252.594, 252.689, 252.235, 251.467, 250.327,
> 248.846, 246.98, 244.729, 242.073, 238.978, 235.454, 231.469,
> 227.051, 222.203, 216.964, 211.35, 205.45, 199.252, 192.786,
> 186.09, 179.17, 172.032, 164.702, 157.151, 149.387, 141.464,
> 133.295, 124.897, 116.303, 107.484, 98.493, 89.347, 80.079, 70.723,
> 61.31, 51.862, 42.382, 32.88, 23.343, 13.814, 4.298, -5.217,
> -14.684, -24.092, -33.446, -42.682, -51.809, -60.819, -69.714,
> -78.456, -87.038, -95.481, -103.756, -111.838, -119.762, -127.535,
> -135.123, -142.557, -149.842, -157.029, -164.093, -171.029, -177.841,
> -184.501, -191.032, -197.427, -203.642, -209.706, -215.62, -221.404,
> -227.025, -232.511, -237.868, -243.095, -248.172, -253.14, -257.94,
> -262.593, -267.079, -271.4, -275.533, -279.483, -283.262, -286.843,
> -290.246, -293.466, -296.507, -299.405, -302.154, -304.763, -307.225,
> -309.549, -311.74, -313.787, -315.657, -317.324, -318.807, -320.083,
> -321.154, -322.036, NA, NA, NA, NA, NA, NA, -322.72, -323.285,
> -323.76, -324.099, -324.261, -325.107)
>
> y<- c(-3.982, -3.98, -3.98, -3.982, -3.977, -3.971, -3.956, -3.943,
> -3.933, -3.913, -3.874, -3.819, -3.706, -3.562, -3.411, -3.278,
> -3.185, -3.024, -2.877, -2.757, -2.81, -2.981, -2.998, -2.85,
> -2.646, -2.43, -2.218, -1.98, -1.668, -1.264, -0.735, -0.051,
> 0.822, 1.916, 3.24, 4.832, 6.723, 8.919, 11.412, 14.185, 17.25,
> 20.591, 24.215, 28.126, 32.304, 36.729, 41.383, 46.249, 51.32,
> 56.579, 62.024, 67.648, 73.456, 79.446, 85.607, 91.943, 98.439,
> 105.074, 111.832, 118.696, 125.646, 132.676, 139.776, 146.956,
> 154.199, 161.501, 168.841, 176.201, 183.55, 190.863, 198.138,
> 205.374, 212.557, 219.671, 226.701, 233.647, 240.492, 247.223,
> 253.851, 260.383, 266.83, 273.188, 279.433, 285.571, 291.574,
> 297.426, 303.128, 308.686, 314.089, 319.374, 324.544, 329.61,
> 334.576, 339.45, 344.217, 348.87, 353.417, 357.861, 362.202,
> 366.45, 370.6, 374.656, 378.61, 382.458, 386.186, 389.781, 393.248,
> 396.58, 399.789, 402.884, 405.867, 408.741, 411.513, 414.172,
> 416.713, 419.15, 421.496, 423.743, 425.884, 427.913, 429.828,
> 431.648, 433.359, 434.976, 436.508, 437.949, 439.316, 440.605,
> 441.821, 442.966, 444.033, 445.974, 447.71, 448.515, 449.296,
> 450.045, 450.786, 451.506, 452.205, 452.886, 453.55, 454.185,
> 454.791, 455.364, 455.889, 456.364, 456.771, 457.117, 457.385,
> 457.577, 457.697, 457.749, 457.731, 457.651, 457.502, 457.283,
> 457.008, 456.648, 456.22, 455.723, 455.15, 454.5, 453.781, 452.993,
> 452.158, 451.28, 450.37, 449.439, 448.483, 447.511, 446.52, 445.515,
> 444.486, 443.438, 442.38, 441.326, 440.295, 439.298, 438.345,
> 437.443, 436.612, 435.856, 435.186, 434.622, 434.177, 433.834,
> 433.591, 433.454, 433.404, 433.417, 433.473, 433.563, 433.665,
> 433.774, 433.889, 434.005, 434.133, 434.292, 434.482, 434.709,
> 434.971, 435.269, 435.59, 435.925, 436.27, 436.625, 436.98, 437.347,
> 437.717, 438.101, 438.51, 438.955, 439.449, 439.994, 440.606,
> 441.285, 442.036, 442.852, 443.712, 444.62, 445.567, 446.549,
> 447.569, 448.637, 449.766, 450.991, 452.341, 453.822, 455.48,
> 457.346, 459.428, NA, NA, NA, NA, NA, NA, 461.692, 464.045, 466.409,
> 468.787, 471.254, 473.328)
>
> s<- spline(x,y, n=length(x))
> plot(x,y)
> lines(s, col="blue")
>
>
> Is this a bug?  Or am I doing something very wrong?
>
> Thanks for any help.
>
> Matthew
>
> ______________________________________________
> R-help at r-project.org 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.
>



More information about the R-help mailing list