在组和年份上循环功能

时间:2019-12-02 17:03:50

标签: r

我对许多国家的样本进行了几年的抽样调查,其中包含有关产出(GDP)的信息。我想使用我在R-Bloggers here中找到的函数来计算“输出差距”,但希望考虑到年份,它可以遍历样本中的所有国家/地区,并将结果存储在矩阵(跨行绑定)。

该函数如下所示:

hp <- function(data,l=1600){
  #h-p filter code from Farnsworth
  hpfilterq <- function(x=data,lambda=l){
    eye <- diag(length(x))
    result <- solve(eye+lambda*crossprod(diff(eye,lag=1,d=2)),x)
    return(result)
  }
  hpfiltered<-hpfilterq(data)
  hpgap <- data - hpfiltered
  #
  t1<-1:length(data)
  t2<-t1^2
  t3<-t1^3
  t1<-ts(t1)
  t2<-ts(t2)
  t3<-ts(t3)
  #
  datats<-ts(data)
  myseries<-ts.union(datats,t1,t2,t3)
  #
  polynomial1 <- lm(datats ~ t1,data=myseries)
  polynomial2 <- lm(datats ~ t1 + t2,data=myseries)
  polynomial3 <- lm(datats ~ t1 + t2 + t3,data=myseries)
  #
  returndata<-data.frame(hpgap,polynomial1$residuals,polynomial2$residuals,polynomial3$residuals)
  colnames(returndata) <- c("H-P Gap", "Poly1","Poly2","Poly3")
  return(returndata)
}

假设我的样本如下:

   country year   output
1      AUS 2000 49709.21
2      AUS 2001 59805.90
3      AUS 2002 46501.57
4      AUS 2003 53521.78
5      AUS 2004 53824.41
6      AUS 2005 55001.43
7      AUS 2006 48356.12
8      AUS 2007 55125.00
9      AUS 2008 58551.84
10     AUS 2009 57805.95
11     AUS 2010 64858.86
12     AUS 2011 67395.81
13     AUS 2012 69043.00
14     AUS 2013 73789.00
15     AUS 2014 77869.09
16     BEL 2000  7110.00
17     BEL 2001  7235.10
18     BEL 2002  7204.10
19     BEL 2003  7327.60
20     BEL 2004  7558.70
21     BEL 2005  7123.10
22     BEL 2006  7539.00
23     BEL 2007  7943.40
24     BEL 2008  8052.50
25     BEL 2009  7509.60
26     BEL 2010  8455.50
27     BEL 2011  8749.40
28     BEL 2012  9694.10
29     BEL 2013  9614.40
30     BEL 2014  8707.50

我想将功能“ hp”应用于“ AUS”,

    H-P Gap      Poly1       Poly2        Poly3
1   2393.2324  2751.8407 -3684.68922 -3536.276248
2  10838.8666 11069.4941  7391.47697  7412.678826
3  -4118.0018 -4013.8596 -5357.75042 -5414.832333
4   1239.8878  1227.3108  1793.15960  1698.566717
5   -135.2596  -249.0938  1802.10804  1702.622415
6   -657.9475  -851.1054  2261.06288  2181.148204
7  -9031.2784 -9275.4400 -5526.69186 -5570.726477
8  -4024.7279 -4285.5977  -324.65619  -324.656186
9  -2394.8369 -2637.7883  1110.95990  1154.994518
10 -4970.1209 -5162.7119 -2050.54360 -1970.628924
11   224.6324   111.1625  2162.36431  2261.849928
12   881.4586   869.0828  1434.93163  1529.524516
13   633.1949   737.2392  -606.65163  -549.569720
14  3474.5127  3704.2066    26.18952     4.987662
15  5646.3875  6005.2600  -431.26992  -579.682899

“ BEL”,

      H-P Gap      Poly1        Poly2      Poly3
1   291.55895  311.04333   -4.1179412 -188.99755
2   253.89032  266.24190   86.1497479   59.73838
3    59.93946   65.34048   -0.4624273   70.64511
4    19.96547   18.93905   46.6455333  164.48089
5    86.68999   80.13762  180.5736296  304.50392
6  -514.57784 -525.36381 -372.9781383 -273.42758
7  -266.08305 -279.36524  -95.8097705  -40.95538
8   -30.94904  -44.86667  149.0787330  149.07873
9   -92.93293 -105.66810   77.8873723   23.03298
10 -808.67250 -818.46952 -666.0838526 -765.63441
11  -37.24743  -42.47095   57.9650582  -65.96523
12   81.16801   81.52762  109.2341047   -8.60125
13  850.02282  856.32619  790.5232870  719.41575
14  594.71530  606.72476  426.6326050  453.04398
15 -487.48754 -470.07667 -785.2379412 -600.35833

和所有其他国家/地区,并将它们存储在矩阵(或某种形式的列表)中:

        H-P Gap       Poly1         Poly2        Poly3
1    2393.23236  2751.84068 -3684.6892235 -3536.276248
2   10838.86665 11069.49406  7391.4769723  7412.678826
3   -4118.00184 -4013.85956 -5357.7504192 -5414.832333
4    1239.88784  1227.31082  1793.1596021  1698.566717
5    -135.25961  -249.09380  1802.1080361  1702.622415
6    -657.94746  -851.10542  2261.0628828  2181.148204
7   -9031.27839 -9275.44005 -5526.6918577 -5570.726477
8   -4024.72789 -4285.59767  -324.6561855  -324.656186
9   -2394.83690 -2637.78829  1110.9598994  1154.994518
10  -4970.12091 -5162.71191 -2050.5436029 -1970.628924
11    224.63238   111.16247  2162.3643075  2261.849928
12    881.45859   869.08285  1434.9316306  1529.524516
13    633.19493   737.23923  -606.6516335  -549.569720
14   3474.51272  3704.20660    26.1895151     4.987662
15   5646.38752  6005.25998  -431.2699235  -579.682899
16    291.55895   311.04333    -4.1179412  -188.997549
17    253.89032   266.24190    86.1497479    59.738375
18     59.93946    65.34048    -0.4624273    70.645114
19     19.96547    18.93905    46.6455333   164.480888
20     86.68999    80.13762   180.5736296   304.503916
21   -514.57784  -525.36381  -372.9781383  -273.427580
22   -266.08305  -279.36524   -95.8097705   -40.955381
23    -30.94904   -44.86667   149.0787330   149.078733
24    -92.93293  -105.66810    77.8873723    23.032983
25  -808.67250  -818.46952  -666.0838526  -765.634411
26   -37.24743   -42.47095    57.9650582   -65.965228
27    81.16801    81.52762   109.2341047    -8.601250
28   850.02282   856.32619   790.5232870   719.415746
29   594.71530   606.72476   426.6326050   453.043978
30  -487.48754  -470.07667  -785.2379412  -600.358333 

无需使用效率极低的hp(data$output[c(1:15)])hp(data$output[c(16:30)])

1 个答案:

答案 0 :(得分:2)

您可以结合使用splitlapply在新函数中应用hp函数,这将自动将结果导出为list中的data.frames按国家

hp_per_country <- function(x) {
  data_list <- split(x$output, x$country)
  result <- lapply(data_list, FUN = hp)
} #note that `return` is assumed in R for the last object computed by a function
overall_hp_results <- hp_per_country(input_data)

如果这对计算很重要,则假定input_data按年份排序。