Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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
R x[,!na.check]中的错误:面板数据中的尺寸数量不正确_R_Panel Data_Imputation - Fatal编程技术网

R x[,!na.check]中的错误:面板数据中的尺寸数量不正确

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上

我想对我的数据进行面板回归,包括192次观察(每年48次观察,总共4年)

由于缺少观察结果,我使用R中的“mice”包来插补我的数据(插补10次)。插补后,我使用聚合方法合并了10个插补数据集(事实上,我使用了前面提到的精确方法)

最后,利用plm软件包建立了面板回归模型。但是,在plm命令之后,我看到了以下消息

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