Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 按组和年份循环函数_R - Fatal编程技术网

R 按组和年份循环函数

R 按组和年份循环函数,r,R,我收集了几年来许多国家的样本,其中包含了有关产出(GDP)的信息。我想用我在R-Bloggers上找到的一个函数来计算“产出差距”,但我希望它能在我样本中的所有国家(考虑年份)循环,结果存储在一个矩阵中(跨行绑定) 该函数如下所示: hp <- function(data,l=1600){ #h-p filter code from Farnsworth hpfilterq <- function(x=data,lambda=l){ eye <- diag(le

我收集了几年来许多国家的样本,其中包含了有关产出(GDP)的信息。我想用我在R-Bloggers上找到的一个函数来计算“产出差距”,但我希望它能在我样本中的所有国家(考虑年份)循环,结果存储在一个矩阵中(跨行绑定)

该函数如下所示:

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)
}
我想将函数“hp”应用于“AUS”

“贝尔”

以及所有其他国家,并将其存储在矩阵(或某种列表)中:


不必使用非常低效的:
hp(数据$output[c(1:15)])
hp(数据$output[c(16:30)])

您可以将
split
lappy
组合起来,在新函数中应用
hp
函数,这将自动将结果导出为
数据.帧的
列表

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)

hp\u per_country您可以组合
split
lappy
在新函数中应用
hp
函数,这将自动将结果导出为
数据的
列表

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)

hp\u per\u country谢谢,效果非常好。这是非常有帮助的功能,分割国家和运行每个自动银行的计算,工作完美。这是非常有帮助的功能分割国家和运行自动计算每个国家
      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_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)