首页 文章

R中的组的线性插值

提问于
浏览
2

假设以下数据:

Date        V1              V2
1 1996-01-04 0.04383562 days 0.1203920
2 1996-01-04 0.12054795 days 0.1094760
..............
3 1996-02-01 0.04383562 days 0.1081815
4 1996-02-01 0.12054795 days 0.1092450
..............
5 1996-03-01 0.04109589 days 0.1553875
6 1996-03-01 0.13687215 days 0.1469690

对于每个组日期(我为了方便起见我用它们区分它们),我想做一个简单的线性插值:对于 V1=0.08 我会得到什么 V2 .

What I have tried :首先使用 approx 的最合理的方法:

IV<-data %>% group_by(Date) %>% approx(V1,V2,xout=0.08)

但我得到这个错误:

Error in approx(., V1, V2, xout = 0.08) : 
  invalid interpolation method
In addition: Warning message:
In if (is.na(method)) stop("invalid interpolation method") :
  the condition has length > 1 and only the first element will be used

然后我试过:

Results<-unsplit(lapply(split(data,data$Date),function(x){m<-lm(V2~V1,x)
                                                       cbind(x,predict(m,0.08))}),data$Date)

有错误:

Error in model.frame.default(formula = x[, 3] ~ x[, 2], data = x, drop.unused.levels = TRUE) : 
  invalid type (list) for variable 'x[, 3]'

我也试过没有结果的 dplyr 包:

IV<-data %>% group_by(Date) %>% predict(lm(V2~V1,data=data,0.08)

这给出了错误:

Error in UseMethod("predict") : 
  no applicable method for 'predict' applied to an object of class "c('grouped_df', 'tbl_df', 'tbl', 'data.frame')"

谢谢 .

1 回答

  • 3

    您在 approx 中获得的错误是因为您在使用 %>% 时将 data.frame 作为第一个参数传递 . 所以你的电话是 approx(df, v1, v2, xout=0.08) .

    您可以在单行中使用 data.table 完成 approx 调用:

    library(data.table)
    #created as df instead of dt for use in dplyr solution later
    df <- data.frame(grp=sample(letters[1:2],10,T),
                 v1=rnorm(10),
                 v2=rnorm(10))
    
    dt <- data.table(df)
    
    dt[, approx(v1,v2,xout=.08), by=grp]
    
    #output
       grp    x          y
    1:   b 0.08 -0.5112237
    2:   a 0.08 -1.4228923
    

    在第一关,留在 tidyverse 我的解决方案是不是很整洁;在管道中可能有更简洁的方法来做到这一点,但我认为很难击败 data.table 解决方案 .

    解压缩到 magrittr 管道:

    library(dplyr)
    
    df %>% 
        group_by(grp) %>% 
        summarise(out=list(approx(v1,v2,xout=.08))) %>% 
        ungroup() %>% 
        mutate(x=purrr::map_dbl(out,'x'),
               y=purrr::map_dbl(out,'y')) %>% 
        select(-out)
    
    #output
    # A tibble: 2 × 3
         grp     x          y
      <fctr> <dbl>      <dbl>
    1      a  0.08 -1.4228923
    2      b  0.08 -0.5112237
    

相关问题