R x[,!na.check]中的错误:面板数据中的尺寸数量不正确
我想对我的数据进行面板回归,包括192次观察(每年48次观察,总共4年) 由于缺少观察结果,我使用R中的“mice”包来插补我的数据(插补10次)。插补后,我使用聚合方法合并了10个插补数据集(事实上,我使用了前面提到的精确方法) 最后,利用plm软件包建立了面板回归模型。但是,在plm命令之后,我看到了以下消息R x[,!na.check]中的错误:面板数据中的尺寸数量不正确,r,panel-data,imputation,R,Panel Data,Imputation,我想对我的数据进行面板回归,包括192次观察(每年48次观察,总共4年) 由于缺少观察结果,我使用R中的“mice”包来插补我的数据(插补10次)。插补后,我使用聚合方法合并了10个插补数据集(事实上,我使用了前面提到的精确方法) 最后,利用plm软件包建立了面板回归模型。但是,在plm命令之后,我看到了以下消息 Error in x[, !na.check] : incorrect number of dimensions 我不明白为什么我的数据维度有问题。我在stackoverflow上
Error in x[, !na.check] : incorrect number of dimensions
我不明白为什么我的数据维度有问题。我在stackoverflow上也找不到相关资源。读者可以在下面查看我的R代码
dat <- read.csv("panel.csv")
# apply the mice package for imputation
library(mice)
impute <- mice(dat,m = 10, maxit = 50,meth = 'pmm',seed = 500)
dat2 <- complete(impute, action = "long")
### Aggregate dataset for model building
# Find the most frequent level in a factor variable
getmode <- function(v) {
levels(v)[which.max(table(v))]
}
# Return either the mean or mode depending on the type of variable passed to it
my_summary <- function(x, id, ...){
if (is.numeric(x)) {
return(tapply(x, id, mean))
}
if (is.factor(x)) {
return(tapply(x, id, getmode))
} }
# Finally, use lapply to calculate the summaries
dat_panel <- data.frame(lapply(dat2, my_summary, id = dat2$.id))
### Now, we will built a panel model with fixed effects
# First, we build a model for ROA
library(plm)
reg <- plm(formula <- ROA ~ Year.of.IPO + TTassets + Debt.ratio + TTdonation,
index <- c("Year","Level"),
data <- dat_panel,
method <- "within")
我还提供了数据集的前几行(根据某人的要求)
如果不查看数据,很难知道问题出在哪里(因此如果需要更多帮助,您可以
dput
数据集的一部分),但在您给我们的代码中,您似乎使用dat
作为回归的数据,而您的插补数据是dat2
。这就是问题所在吗?如果没有,dim(dat2)的输出是什么?您好,我已经编辑了我的文章。请看一看!我自己找到了原因。在plm命令中,我需要使用“=”而不是“如果不查看数据,很难知道问题出在哪里”(因此,如果需要更多帮助,您可以dput
数据集的一部分),但在您给我们的代码中,您似乎使用了dat
作为回归的数据,而您的插补数据是dat2
。这是问题吗?如果不是,那么dim(dat2)的输出是什么
?嗨,奥利奥,我已经编辑了我的帖子。请看一看!我自己找到了原因。在plm命令中,我需要使用“=”而不是“
dim(dat_panel)
[1] 192 11
str(dat_panel)
'data.frame': 192 obs. of 11 variables:
$ .imp : chr [1:192(1d)] "1" "1" "1" "1" ...
..- attr(*, "dimnames")=List of 1
.. ..$ : chr "1" "10" "100" "101" ...
$ .id : chr [1:192(1d)] "1" "10" "100" "101" ...
..- attr(*, "dimnames")=List of 1
.. ..$ : chr "1" "10" "100" "101" ...
> head(dat_panel)
Year Level Stock.Code Year.of.IPO ROA ROE TTassets Debt.ratio TTdonation
1 2013 Conglomerates 1 45 8.52 10.150 19.87659 0.1130966 17.30594
18 2013 Conglomerates 19 46 4.47 6.200 19.66903 0.1525779 17.57671
24 2013 Conglomerates 25 33 9.25 18.820 16.36257 0.2645771 12.31321
29 2013 Conglomerates 30 26 -4.30 -7.920 13.16387 0.0000000 12.45592
32 2013 Consumer Goods 33 10 -16.63 -18.023 12.43860 0.3243540 12.23134
35 2013 Consumer Goods 36 44 -2.56 -2.250 13.05192 0.0000000 11.08727