简体   繁体   English

基于当前观测值和组中的其他观测值的egen中的if语句(以计算移动平均值)

[英]if statement in egen based on current observation and other observations in group (to calculate moving average)

Suppose I have the data generated by this: 假设我有由此产生的数据:

clear all
set seed 100
set obs 36
egen group = seq(), from(1) to(2) block(18)
egen year = seq(), from(2000) to(2005) block(3)
egen month = seq(), from(1) to(3)
gen y = round(runiform()*10)
sort group month year
replace y = . in 3 
replace y = . in 7 
replace y = . in 11
replace y = . in 19
replace y = . in 28

We will focus on the first 6 observations for expository purposes: 出于说明目的,我们将重点关注前6个观察值:

group   year    month   y
1       2000    1       10
1       2001    1       1
1       2002    1       
1       2003    1       9
1       2004    1       5
1       2005    1       6

What I want to do is to use egen to create a moving average of y . 我想做的是使用egen创建y的移动平均值。 In other words, take the average based on the previous 3 years before the current year (including current year); 换句话说,取本年(包括当年)之前的前三年的平均值; if the year is not in the data, do not use that year. 如果年份不在数据中,请不要使用该年份。 For year 2000 , the moving average is 10 . 对于2000 ,移动平均数为10 We want to ignore missings in the calculation; 我们要忽略计算中的缺失; but only ever go back 3 years. 但只有3年了 For the row corresponding to year 2005 , it would be (20/3). For 对于与2005相对应的行,它将为(20/3). For (20/3). For 2004 , it would be 5 (and not 10/3`). (20/3). For 2004年, it would be 5 (and not 10 / 3`)。

Here is some incorrect code to try to get at this. 这是一些不正确的代码试图解决。

bys group month: egen avg = mean(temp) if year>year[_n]-3 & year<=year[_n]

This produces missing values everywhere. 这到处都会产生缺失值。 What I am trying to do is to calculate a separate number for each month day, but have this use data from the whole bysort group, assuming that the data meet the criteria of being 3 years back. 我想做的是为每个月的天计算一个单独的数字,但是要使用整个分类组的数据,前提是该数据符合3年以前的标准。

In my line of incorrect code, in the first group month group, I want it to start at obs. 在我的错误代码行中,在第一个group month组中,我希望它从obs开始。 1 . 1 It should compute the average for all observations in value of year is greater than 1997 and less than or equal to 2000 . 它应该计算所有观测值的平均值,即年份值大于1997且小于或等于2000 In this case, it is only the first observation. 在这种情况下,这只是第一次观察。

Then it goes to observation 2 . 然后转到观察2 It uses 2001 for the values of year[_n] and computes the average based on the first two observations, because these are the ones that meet the criteria. 它使用2001作为year [_n]的值,并根据前两个观察值计算平均值,因为这两个满足条件。

Is what I am trying to describe possible using egen ? 我想用egen描述的可能吗? This is a general question that extends beyond the moving average application. 这是一个超出移动平均应用范围的一般问题。

Also, if it is not possible, then is the following the best solution to calculate the moving average (once again only going back 3 years and ignoring missings in the calculation)?: 另外,如果不可能的话,以下是计算移动平均数的最佳解决方案(仅再次追溯到3年并忽略计算中的缺失)吗?:

sort group month year
forvalues i = 1/3 {
    bys group: gen y_`i' = y[_n-`i']
}

bys group month: egen avg = mean(y) if year>year[_n]
egen ma_3 = rowmean(y y_1 y_2 y_3)

You can use tsegen (from SSC) to calculate statistics over a rolling window of time. 您可以使用tsegen (来自SSC)来计算滚动时间范围内的统计信息。 I'm not sure I understand how you group your observations since you have a month variable but the following appears to do what you are looking for: 由于您有一个月变量,因此我不确定我是否了解如何对观察结果进行分组,但是以下内容似乎可以满足您的需求:

clear all
set seed 100
set obs 36
egen group = seq(), from(1) to(2) block(18)
egen year = seq(), from(2000) to(2005) block(3)
egen month = seq(), from(1) to(3)
gen y = round(runiform()*10)
sort group month year
replace y = . in 3 
replace y = . in 7 
replace y = . in 11
replace y = . in 19
replace y = . in 28

* create a panel variable by grouping the group and month variable
isid group month year, sort
egen group_month = group(group month)

* declare data to be a time-series
tsset group_month year

* calculate a moving average over 3 years
tsegen avg = rowmean(L(0/2).y)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM