【问题标题】:Calculate Concentration Index by Region and Year (panel data)按地区和年份计算集中指数(面板数据)
【发布时间】:2012-10-04 07:52:42
【问题描述】:

这是我的第一篇文章,并且非常坚持尝试构建我的第一个函数,该函数使用公司 = obs 的面板数据(年=1998:2007)计算赫芬达尔衡量公司总产出的指标。按年份(1998-2007)和地区(“West”、“Central”、“East”、“NE”),并且在通过函数传递参数时遇到问题。我想我需要使用两个循环(一个用于时间,一个用于区域)。任何帮助都会很有用.. 我真的不想将我的数据子集 400 多次才能让 herfindahl 测量一次。提前致谢!

下面我提供: 1)我的起始代码(只返回一个值); 2) 期望输出(2 箱,包含按 1)年和按 2)年区域的 hefindahl 措施); 3) 原始数据

1) 我的入门代码

myherf<- function (x, time, region){
time = year # variable is defined in my data and includes c(1998:2007)
region = region # Variable is defined in my data, c("West", "Central","East","NE")
    for (i in 1:length(time)) {
      for (j in 1:length(region)) {
        herf[i,j] <- x/sum(x)
        herf[i,j] <- herf[i,j]^2
        herf[i,j] <- sum(herf[i,j])^1/2        
      }
    }
  return(herf[i,j])
}

myherf(extractiveoutput$x, i, j)
herf[i, j]


2) 我想要的结果是以下两个向量:

A. (1x10 vector)              
Year  herfindahl(yr)  
1998    x                        
1999    x                       
...                             
2007    x                       

B. (1x40 vector)  
Year  Region   hefindahl(yr-region)  
1998  West      x                                     
1998  Central   x                                    
1998  East      x    
1998  NE        x    
...  
2007  West      x    
2007  Central   x  
2007  East      x   
2007  northeast x   

3) 原始数据

Obs. industry year  region    grossoutput  
1         06 1998    Central 0.048804830  
2         07 1998    Central 0.011222478  
3         08 1998    Central 0.002851575  
4         09 1998    Central 0.009515881  
5         10 1998    Central 0.0067931  
...  

12        06 1999    Central 0.050861447  
13        07 1999    Central 0.008421093  
14        08 1999    Central 0.002034649  
15        09 1999    Central 0.010651283  
16        10 1999    Central 0.007766118  
...  
111       06 1998       East 0.036787413  
112       07 1998       East 0.054958377  
113       08 1998       East 0.007390260  
114       09 1998       East 0.010766598  
115       10 1998       East 0.015843418  
...  
436       31 2007       West 0.166044176  
437       32 2007       West 0.400031011  
438       33 2007       West 0.133472059  
439       34 2007       West 0.043669662  
440       45 2007       West 0.017904620  

【问题讨论】:

  • 什么是herf?我会建议一种 data.table 方法,但你会清楚 herf 是什么
  • 我认为您需要从定义herf &lt;- matrix(nrow=length(time),ncol=length(region)) 开始(并且您可能希望返回整个矩阵herf,而不是herf[i,j]

标签: r function loops panel inequality


【解决方案1】:

您可以使用ineq 库中的conc 函数。使用data.table,解决方案变得非常简单和快速。

library(ineq)
library(data.table)

# convert your data.frame into a data.table
  setDT(df)

# calculate inequality of grossoutput by region and year
  df[, .(inequality = conc(grossoutput, type = "Herfindahl")), by=.(region, year) ]

【讨论】:

    猜你喜欢
    • 2019-04-20
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2018-03-15
    • 2022-01-17
    • 2011-06-24
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多