我正在尝试使用na.spline
(zoo
包的一部分)来替换某些导入的速度数据中的NA
值,并使用三次样条插值。
na.spline
正在修改NA
值;但是,它也修改了最初等于0的值。
ex <- data.frame(speed)
ex$speed2 <- na.spline(ex$speed)
我的数据集是~1400的值。我在下面列出了前40个值。在这里,您可以看到speed
中的原始speed 2
值和错误插值的结果:
speed speed2
NA 8.639277e-06
0.000000 0.000000e+00
0.000000 0.000000e+00
0.000000 0.000000e+00
0.000000 -1.694066e-21
0.000000 0.000000e+00
0.000000 -2.710505e-20
0.000000 0.000000e+00
0.000000 -4.336809e-19
0.000000 0.000000e+00
0.000000 6.938894e-18
0.000000 0.000000e+00
0.000000 1.110223e-16
2.661698 2.661698e+00
3.107128 3.107128e+00
7.319669 7.319669e+00
10.800864 1.080086e+01
17.855491 1.785549e+01
18.250267 1.825027e+01
28.587002 2.858700e+01
36.405397 3.640540e+01
38.467383 3.846738e+01
38.685956 3.868596e+01
43.917737 4.391774e+01
40.829615 4.082962e+01
43.519173 4.351917e+01
45.597497 4.559750e+01
43.252656 4.325266e+01
45.581646 4.558165e+01
48.258325 4.825832e+01
48.269969 4.826997e+01
50.905045 5.090505e+01
53.258165 5.325817e+01
58.391370 5.839137e+01
59.278440 5.927844e+01
58.720518 5.872052e+01
56.933438 5.693344e+01
62.062116 6.206212e+01
59.860849 5.986085e+01
60.183378 6.018338e+01
有没有人看过类似问题或者有其他方法用插值数据替换NA
值?
答案 0 :(得分:3)
在内部它这样做(不涉及动物园):
y <- c(NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2.661698, 3.107128,
7.319669, 10.800864, 17.855491, 18.250267, 28.587002, 36.405397,
38.467383, 38.685956, 43.917737, 40.829615, 43.519173, 45.597497,
43.252656, 45.581646, 48.258325, 48.269969, 50.905045, 53.258165,
58.39137, 59.27844, 58.720518, 56.933438, 62.062116, 59.860849,
60.183378)
x <- xout <- seq_along(y)
na <- is.na(y)
splinefun(x[!na], y[!na])(xout)
,并提供:
[1] 8.639280e-06 0.000000e+00 0.000000e+00 0.000000e+00 3.388132e-21
[6] 0.000000e+00 -2.710505e-20 0.000000e+00 4.336809e-19 0.000000e+00
[11] 6.938894e-18 0.000000e+00 0.000000e+00 2.661698e+00 3.107128e+00
[16] 7.319669e+00 1.080086e+01 1.785549e+01 1.825027e+01 2.858700e+01
[21] 3.640540e+01 3.846738e+01 3.868596e+01 4.391774e+01 4.082961e+01
[26] 4.351917e+01 4.559750e+01 4.325266e+01 4.558165e+01 4.825832e+01
[31] 4.826997e+01 5.090505e+01 5.325816e+01 5.839137e+01 5.927844e+01
[36] 5.872052e+01 5.693344e+01 6.206212e+01 5.986085e+01 6.018338e+01
另请注意,这会将与输入中的零值对应的输出组件归零:
na.fill(y != 0, 1) * na.spline(y)