在dplyr中插入样条曲线

时间:2016-01-08 22:25:17

标签: r dplyr spline

我正在尝试为以下示例数据插入样条线:

trt    depth    root    carbon
A       2        1        14
A       4        2        18
A       6        3        18
A       8        3        17
A      10        1        12
B       2        3        16
B       4        4        18
B       6        4        17
B       8        2        15
B      10        1        12

以下列方式:

new_df<-df%>%
  group_by(trt)%>%
  summarise_each(funs(splinefun(., x=depth, method="natural")))

我得到Error: not a vector,但我不明白为什么不。我没有以正确的方式表达这个功能吗?

2 个答案:

答案 0 :(得分:4)

您想要一个包含内插值的数据集吗?如果是这样,我会在计算样条曲线之前扩展数据集以包含所需的 x 位置。

这些点的分辨率在expand.grid()函数的第二行中确定。只需确保原始深度点是扩展深度点的子集(例如,不要使用像by=.732那样不均匀的东西。

library(magrittr)
ds <- readr::read_csv("trt,depth,root,carbon\nA,2,1,14\nA,4,2,18\nA,6,3,18\nA,8,3,17\nA,10,1,12\nB,2,3,16\nB,4,4,18\nB,6,4,17\nB,8,2,15\nB,10,1,12")

ds_depths_possible <- expand.grid(
  depth            = seq(from=min(ds$depth), max(ds$depth), by=.5), #Decide resolution here.
  trt              = c("A", "B"),
  stringsAsFactors = FALSE
  )

ds_intpolated <- ds %>% 
  dplyr::right_join(ds_depths_possible, by=c("trt", "depth")) %>% #Incorporate locations to interpolate
  dplyr::group_by(trt) %>% 
  dplyr::mutate(
    root_interpolated     = spline(x=depth, y=root  , xout=depth)$y,
    carbon_interpolated   = spline(x=depth, y=carbon, xout=depth)$y
  ) %>% 
  dplyr::ungroup()
ds_intpolated

输出:

Source: local data frame [34 x 6]

     trt depth  root carbon root_interpolated carbon_interpolated
   (chr) (dbl) (int)  (int)             (dbl)               (dbl)
1      A   2.0     1     14          1.000000            14.00000
2      A   2.5    NA     NA          1.195312            15.57031
3      A   3.0    NA     NA          1.437500            16.72917
4      A   3.5    NA     NA          1.710938            17.52344
5      A   4.0     2     18          2.000000            18.00000
6      A   4.5    NA     NA          2.289062            18.21094
7      A   5.0    NA     NA          2.562500            18.22917
8      A   5.5    NA     NA          2.804688            18.13281
9      A   6.0     3     18          3.000000            18.00000
10     A   6.5    NA     NA          3.132812            17.88281
..   ...   ...   ...    ...               ...                 ...

root carbon

在上图中,小点和&amp;线是插值的。观察到了巨大的脂肪点。

library(ggplot2)
ggplot(ds_intpolated, aes(x=depth, y=root_interpolated, color=trt)) +
  geom_line() +
  geom_point(shape=1) +
  geom_point(aes(y=root), size=5, alpha=.3, na.rm=T) +
  theme_bw()

ggplot(ds_intpolated, aes(x=depth, y=carbon_interpolated, color=trt)) +
  geom_line() +
  geom_point(shape=1) +
  geom_point(aes(y=carbon), size=5, alpha=.3, na.rm=T) +
  theme_bw()

如果你想要一个额外的例子,这里有一些最近的codeslides。对于某些缺失点我们需要滚动中位数,对于其他一些点我们需要线性stats::approx()。另一个选项也是stats::loess(),但它的参数与approx()spline()不相似。

答案 1 :(得分:0)

我放弃了尝试获取dplyr::summarise_each(并且还尝试了dplyr :: summarize,因为您选择的函数似乎不符合您希望多列输入只返回两个函数。)我是不确定在dply中是否可行。以下是可能被称为规范方法的方法:

 lapply( split(df, df$trt), function(d) splinefun(x=d$depth, y=d$carbon) )
#-------------

$A
function (x, deriv = 0L) 
{
    deriv <- as.integer(deriv)
    if (deriv < 0L || deriv > 3L) 
        stop("'deriv' must be between 0 and 3")
    if (deriv > 0L) {
        z0 <- double(z$n)
        z[c("y", "b", "c")] <- switch(deriv, list(y = z$b, b = 2 * 
            z$c, c = 3 * z$d), list(y = 2 * z$c, b = 6 * z$d, 
            c = z0), list(y = 6 * z$d, b = z0, c = z0))
        z[["d"]] <- z0
    }
    res <- .splinefun(x, z)
    if (deriv > 0 && z$method == 2 && any(ind <- x <= z$x[1L])) 
        res[ind] <- ifelse(deriv == 1, z$y[1L], 0)
    res
}
<bytecode: 0x7fe56e4853f8>
<environment: 0x7fe56efd3d80>

$B
function (x, deriv = 0L) 
{
    deriv <- as.integer(deriv)
    if (deriv < 0L || deriv > 3L) 
        stop("'deriv' must be between 0 and 3")
    if (deriv > 0L) {
        z0 <- double(z$n)
        z[c("y", "b", "c")] <- switch(deriv, list(y = z$b, b = 2 * 
            z$c, c = 3 * z$d), list(y = 2 * z$c, b = 6 * z$d, 
            c = z0), list(y = 6 * z$d, b = z0, c = z0))
        z[["d"]] <- z0
    }
    res <- .splinefun(x, z)
    if (deriv > 0 && z$method == 2 && any(ind <- x <= z$x[1L])) 
        res[ind] <- ifelse(deriv == 1, z$y[1L], 0)
    res
}
<bytecode: 0x7fe56e4853f8>
<environment: 0x7fe56efc4db8>