Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.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/jsf/5.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
按年份对面板数据(dataframe)进行Winsorizing并针对特定条件进行修剪_R_Outliers_Panel Data - Fatal编程技术网

按年份对面板数据(dataframe)进行Winsorizing并针对特定条件进行修剪

按年份对面板数据(dataframe)进行Winsorizing并针对特定条件进行修剪,r,outliers,panel-data,R,Outliers,Panel Data,我正在尝试对面板数据和每年的数据进行winsorize(用与平均值相差2或3个标准差的值替换异常值)。我安装了robustHD包,其中包含这样一个函数,但是我无法在我的数据集上实现它 我的数据集看起来类似于格伦菲尔德(尽管我有NA),在同一年里有不同的公司。(1935-1954) 我现在想做的是在所有公司中只对某些列(即inv和value)进行winsorize,并将其存储在具有相同结构的数据框中。我尝试了以下代码: 目标基本上是获得原始数据帧的winsorized副本(所有内容的结构都相同)。

我正在尝试对面板数据和每年的数据进行winsorize(用与平均值相差2或3个标准差的值替换异常值)。我安装了robustHD包,其中包含这样一个函数,但是我无法在我的数据集上实现它

我的数据集看起来类似于格伦菲尔德(尽管我有NA),在同一年里有不同的公司。(1935-1954)

我现在想做的是在所有公司中只对某些列(即inv和value)进行winsorize,并将其存储在具有相同结构的数据框中。我尝试了以下代码:

目标基本上是获得原始数据帧的winsorized副本(所有内容的结构都相同)。 正如下面提出的,plyr是分割数据帧和应用函数的好方法,但我无法让它工作

library(plm)
library(robustHD)
library(plyr)

data("Grunfeld", package="plm")

#Winsorize data  each year (over all firms) therefore split dataframe for each year and apply the winsorize function)

Grunfeld.w<-ddply(Grunfeld, .(year) function(x) winsorize(x$inv,x$value))
库(plm)
图书馆(robustHD)
图书馆(plyr)
数据(“格伦菲尔德”,package=“plm”)
#每年(所有公司)对数据进行Winsorize,因此每年拆分数据框并应用Winsorize功能)

可以创建一个列表来预先存储数据帧。列表的长度必须为n,其中n是一年中唯一值的数量

library(plm)
library(robustHD)
data("Grunfeld", package="plm")

## determine unique values in year and their length
unique_years <- unique(Grunfeld$year)
n_unique_years <- length(unique_years)

## create an empty list of length 20
Grunfeld.w <- vector("list", length=n_unique_years)

for(i in 1:n_unique_years){
  Grunfeld.w[[i]]  <- winsorize(subset(Grunfeld, year==unique_years[i], 
                                       select=c(inv, value)))

  ## add the year field to each insorized data frame
  Grunfeld.w[[i]] <- cbind(Grunfeld.w[[i]], year=unique_years[i])
}
编辑 如果要以与原始数据相同的方式排列新数据,可以使用
rownames
提取原始顺序:

temp <- temp[order(as.numeric(rownames(temp))), ]

## Add the winsorized variables to the original data
names(temp)[1:2] <- c("inv_wins", "value_wins")
Grunfeld_new <- data.frame(Grunfeld, temp[, c("inv_wins", "value_wins")])

temp感谢您的回答!不知何故,到data.frame的转换不起作用。另外,如何以与原始数据相同的方式重新排列winsorized数据?实际上,
do.call(“rbind”,Grunfeld.w)
将列表转换为矩阵,而不是data.frame。所以我已经纠正了这个错误。我试图实现你的代码,但不幸的是它与我的NA不兼容,我得到了以下错误:eigen中的错误(R,symmetric=TRUE):在'x'>回溯()中无限或缺失值6:stop(“x'中的无限或缺失值”)5:eigen(R,symmetric=TRUE)4:winsorize.matrix(as.matrix(x,…)3:winsorize(as.matrix(x,…))2:winsorize.data.frame(子集(mydata,time==unique_years[i],您可以删除缺少的值并应用
winsorize
。然后在
winsorize
for
循环中的
winsorize
中包含
。我认为这比输入缺少的数据然后winsorize要省钱得多,这让我很难理解。您会在哪里包含
na.ommit
在此之前删除缺失的值?干杯
## convert the list to one data frame
temp <- data.frame(do.call("rbind", Grunfeld.w))
library(dplyr)
Grunfeld_gt1940 <- filter(Grunfeld, year>1940) ## the "gt" stands for "greater than". 
temp <- temp[order(as.numeric(rownames(temp))), ]

## Add the winsorized variables to the original data
names(temp)[1:2] <- c("inv_wins", "value_wins")
Grunfeld_new <- data.frame(Grunfeld, temp[, c("inv_wins", "value_wins")])