I want to run a panel regression on my data, consisting of 192 observations (48 observations each year, 4 years in total).
Since there are missing observations, I used "mice" package in R to impute my data (the imputation is done 10 times). After imputation, I used aggregation method to merge the 10 imputed dataset (Indeed, I used the exact method mentioned here)
Finally, I built the panel regression model with plm package. However, I saw the following message after the plm command.
Error in x[, !na.check] : incorrect number of dimensions
I don't understand why I have problems with the dimensions of my data. I can't find relevant resource here on stackoverflow as well. Readers can view my R codes below.
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")
Can someone please explain why there is an error? I appreciate your explanations!
I am providing more information about my data. Indeed, I am using "dat_panel" to built my regression model. The dimension of the dataset is 192 rows x 11 columns. The structure of the dataset can be seen below
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" ...
I also provide the first few rows of my dataset (as requested by someone)
> 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