代码之家  ›  专栏  ›  技术社区  ›  cliu

从acf输出列表中提取特定元素

  •  0
  • cliu  · 技术社区  · 4 年前

    我输出了一系列 acf 结果和想要提取的只是滞后1的自相关系数。谁能给我一个快速的指示?非常感谢。

    #A snippet of a series of acf() results
    $`25`
    
    Autocorrelations of series ‘x’, by lag
    
         0      1      2      3      4      5      6      7      8      9     10     11     12 
     1.000  0.366 -0.347 -0.399 -0.074  0.230  0.050 -0.250 -0.213 -0.106  0.059  0.154  0.031 
    
    $`26`
    
    Autocorrelations of series ‘x’, by lag
    
         0      1      2      3      4      5      6      7      8      9     10     11 
     1.000  0.060  0.026 -0.163 -0.233 -0.191 -0.377  0.214  0.037  0.178 -0.016  0.049 
    
    $`27`
    
    Autocorrelations of series ‘x’, by lag
    
         0      1      2      3      4      5      6      7      8      9     10     11     12 
     1.000 -0.025 -0.136  0.569 -0.227 -0.264  0.218 -0.262 -0.411  0.123 -0.039 -0.192  0.130 
    #For this example, the extracted values will be 0.366, 0.060, -0.025, the values can either 
    be in a list or matrix 
    

    编辑

    #`acf` in base R was used  
    p <- acf.each() 
    #sapply was tried but it resulted this 
    sapply(acf.each(), `[`, "1")
              1             2             3                  
    acf    0.7398        0.1746        0.4278              
    type   "correlation" "correlation" "correlation" 
    n.used    24            17            14                 
    lag       1             1             1                     
    series    "x"          "x"           "x"                       
    snames   NULL          NULL          NULL
    
    
    
    
    
              
    
    1 回复  |  直到 4 年前
        1
  •  2
  •   akrun    4 年前

    结构似乎是一个 list .我们可以用 sapply 提取

    sapply(lst1, function(x) x$acf[2])
    

    数据

    lst1 <- list(acf(ldeaths), acf(ldeaths))