感谢@ user20650和@李哲源Zheyuan Li,这是我提出的解决方案:
SharedPreferences
我试图平滑动物轨道的锯齿状路径,以更准确地确定它们的长度。数据采用(x,y)2D坐标的形式。
我拥有的示例数据集相当大(3600行),以更好地说明问题的范围。它在此处以.Rdata文件形式提供:
SharedPreferences
将smooth.spine()应用于整个数据集是不合适的,因为这些动物蜿蜒得很多(走路等等)。
然后,我有了一个想法:将数据拆分为更小的路径并将smooth.spline()应用于每个列表元素。最终目标是将列表重新整合到一个连续,平滑的轨道中。
# Example data set: df
# 3600 observations/points
# Create a vector of the cumulative distances between all of the points
require(Momocs)
cumdist <- coo_perimcum(df)
# Apply splines parametrically - define a spline interpolated mapping R --> R^2 of some curve c
# c(t) = (x(t), y(t))
# 't' is the set of cumulative distances (as defined above)
# Set the number of points to some fraction of the number of observations in the data set (5% in this case)
splines <- cbind.data.frame(x = c(spline(cumdist, df[, 1], method = "natural",
n = ceiling(nrow(df)*0.05))$y),
y = c(spline(cumdist, df[, 2], method = "natural",
n = ceiling(nrow(df)*0.05))$y))
plot(df, col = "gray")
lines(splines, col = "red", lwd = 2)
distance <- function(df, mm) # data frame must be in the form (x,y); mm = pixel to mm conversion factor
{
require(Momocs)
cumdist <- coo_perimcum(df) # calculates the cumulative Euclidean distance between points
splines <- cbind.data.frame(x = c(spline(cumdist, df[, 1], method = "natural",
n = ceiling(nrow(df)*0.05))$y),
y = c(spline(cumdist, df[, 2], method = "natural",
n = ceiling(nrow(df)*0.05))$y))
assemble <- Mod(diff(splines$x+1i*splines$y))*mm
distance <- sum(assemble)/1000 # sum the distances and convert to meters
distance
}
distance(df, 0.444444)
distance(splines, 0.444444)
产生错误:
with(df, plot(x,y, type = "l"))
我可能在这里遗漏了一些非常简单的东西......有什么想法吗?