在R中的不平衡面板上的简单移动平均值 [英] Simple moving average on an unbalanced panel in R

查看:249
本文介绍了在R中的不平衡面板上的简单移动平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用不平衡,不规则间隔的横截面时间系列。我的目标是为数量向量获得一个滞后移动平均向量,由主题分段。



换句话说,以下的Quanatities已经观察到Subject_1:
[1,2,3,4,5]。我首先需要将它延迟1,产生[NA,1,2,3,4]。



然后我需要采取3阶的移动平均, [NA,NA,NA,(3 + 2 + 1)/ 3,(4 + 3 + 2)/ 3]



上述需要主题

 #构造示例平衡面板DF 
面板< - data.frame(
as。 factor(sort(rep(1:6,5))),
rep(1:5,6),
rnorm(30)

colnames ; - c(Subject,Day,Quantity)

#Make panel DF unbalanced
panelUNB < - subset )
panelUNB < - panelUNB [-c(15,16),]

面板是平衡的,我会首先滞后数量变量使用包 plm 和函数 lag
然后我将使用函数 rollmean 从包 zoo

  panel $ QuantityMA < -  ave(panel $ Quantity,panel $ subject,FUN = function(x)rollmean (
x,3,align =right,fill = NA,na.rm = TRUE))


$ b b

这将产生适用于平衡'面板'DF的正确结果。



问题是 plm lag 依赖于均匀间隔的系列来生成索引变量,而rollapply要求所有主体的观察数量(窗口大小)相等。 p>

有一个解决方案StackExchange与data.table提示我的问题的解决方案:生成不平衡面板数据集的滚动平均值



解决方案可以被修改以产生固定长度移动平均值而不是滚动累积平均值。

解决方案

我自己的问题,一种方法是通过split-lapply(滚动平均)-unlist:

  Temp < panelUNB,split(Quantity,Subject))
Temp < - lapply(Temp,FUN = function(x)rollapplyr(
x,2,align =right,fill = NA,na.rm = TRUE,FUN = mean))
QuantityMA <-unlist(Temp)



< QuantityMA矢量必须添加回主panelUNB帧。似乎工作。



如果任何人有另一个也许更优雅的解决方案,欢迎您与我们分享。


I am working with an unbalanced, irregularly spaced cross-sectional time series. My goal is to obtain a lagged moving average vector for the "Quantity" vector, segmented by "Subject".

In other words, say the the the following Quanatities have been observed for Subject_1: [1,2,3,4,5]. I first need to lag it by 1, yielding [NA,1,2,3,4].

Then I need to take a moving average of order 3, yielding [NA,NA,NA,(3+2+1)/3,(4+3+2)/3]

The above needs to be done for all Subjects.

# Construct example balanced panel DF
panel <- data.frame(
  as.factor(sort(rep(1:6,5))),
  rep(1:5,6),
  rnorm(30)                
)
colnames(panel)<- c("Subject","Day","Quantity")

#Make panel DF unbalanced
panelUNB <- subset(panel,as.numeric(Subject)!= Day)
panelUNB <- panelUNB[-c(15,16),]

If the panel were balanced, i would first lag the "Quantity" variable using package plm and functionlag. Then I would take the moving average of the lagged "Quanatity" like so, using function rollmean from package zoo:

panel$QuantityMA <- ave(panel$Quantity, panel$Subject, FUN = function(x) rollmean(
                     x,3,align="right",fill=NA,na.rm=TRUE))

This will yield the proper result when applied to the balanced 'panel' DF.

The problem is that plm and lag rely on the series being evenly spaced to generate an index variable, while rollapply demands that the number of observations (windowsize) is equal for all subjects.

There is solution on StackExchange with data.table that hints at a solution to my problem: Producing a rolling average of an unbalanced panel data set

Perhaps this solution can be modified to produce a fixed-length moving average instead of a "rolling cumulative average."

解决方案

So, to answer my own question, one way to do it is through split-lapply(rollingaverage)-unlist:

Temp <-with(panelUNB, split(Quantity, Subject))
Temp <- lapply(Temp, FUN=function (x) rollapplyr(
   x,2,align="right",fill=NA,na.rm=TRUE, FUN=mean))
QuantityMA <-unlist(Temp)

The "QuantityMA" vector would then have to be added back to the main "panelUNB" frame. Seems to be working. Lagging can be accomplished on an unbalanced panel with ddply.

If anyone has another, perhaps more elegant, solution, you're welcome to share it.

这篇关于在R中的不平衡面板上的简单移动平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆