R:从smooth.spline预测内插到更精细的时间尺度

时间:2013-08-02 17:38:25

标签: r

经典问题:来自不同仪器的数据记录在不同的时间尺度上。这里我有每小时的环境数据(envdat),我想在我的响应数据(respdat)中添加一个包含环境数据的列。然而,响应数据每秒都有一个点,除了它是高于阈值的电压的子集(缺少点)。我的目标是将曲线拟合到环境数据(类似于正弦波)并插入每个响应观察的值。见下面的例子:

envdat=structure(list(date.time = structure(c(1369552133, 1369555733, 
1369559333, 1369562933, 1369566533, 1369570133, 1369573733, 1369577333, 
1369580933, 1369584533, 1369588133, 1369591733, 1369595333, 1369598933, 
1369602533, 1369606133, 1369609733, 1369613333, 1369616933, 1369620533, 
1369624133, 1369627733, 1369631333, 1369634933), class = c("POSIXct", 
"POSIXt"), tzone = ""), cool.C = c(9.866, 9.077, 9.077, 6.573, 
6.573, 6.573, 9.669, 9.472, 9.373, 11.334, 11.334, 11.236, 13.75, 
13.942, 13.076, 16.713, 16.808, 16.808, 14.23, 14.325, 14.421, 
11.625, 11.625, 12.207)), .Names = c("time", "temp"), row.names = 63:86, class =     "data.frame")
plot(data$time,data$temp,type="b")
myspline=smooth.spline(data$time,data$temp,spar=.5)
lines(myspline,col="red")
####
respdat=structure(list(time = structure(c(1369606346, 1369606356, 
1369606359, 1369606360, 1369606361, 1369606362, 1369606363, 1369606364, 
1369606365, 1369606366, 1369606367, 1369606368, 1369606369, 1369606370, 
1369606371, 1369606372, 1369606373, 1369606374, 1369606375, 1369606376, 
1369606377, 1369606378, 1369606379, 1369606380, 1369606381), class = c("POSIXct", 
"POSIXt"), tzone = ""), volts = c(2.511094, 2.509647, 2.524574, 
2.954419, 2.756494, 2.59868, 2.94975, 2.87183, 2.846382, 2.788386, 
3.144585, 3.099345, 2.931733, 3.032537, 2.870383, 2.869857, 2.841319, 
2.886822, 2.750839, 3.109142, 3.017413, 3.1194, 2.909508, 2.899776, 
2.903129)), .Names = c("time", "volts"), row.names = c(4036L, 
4046L, 4049L, 4050L, 4051L, 4052L, 4053L, 4054L, 4055L, 4056L, 
4057L, 4058L, 4059L, 4060L, 4061L, 4062L, 4063L, 4064L, 4065L, 
4066L, 4067L, 4068L, 4069L, 4070L, 4071L), class = "data.frame")
print(respdat)

我的方法是:

test=predict(myspline,respdat$time)

这会返回错误。欢迎指导。

1 个答案:

答案 0 :(得分:0)

这是一种将env每小时数据插入秒数据的方法。

library(xts)
## you create the xts object
env.xts <- xts(envdat$temp,envdat$time)
## new index to be used 
new.index <- 
  seq(min(index(env.xts)),max(index(env.xts)), by=as.difftime(1,units='secs'))
## linear approx
na.spline(merge(env.xts,xts(NULL,new.index)))