Error using package plm - Error in x[, ! na.check] : (subscript) Logical subscript Too long

Asked

Viewed 158 times

4

I am using the plm package to perform analysis in a panel. To structure the base as a panel, I used the function pdata.frame of the same package. With this, I had as return a panel with the following features:

inserir a descrição da imagem aqui

Dai, I would like to run a regression using instrumental variables with the following formula:

saida <- plm(formula = X4  ~  X1 | . ~  X2 + X3 + Ano.Mes + id, data = as.matrix(painel),model = "within", index = c("id", "Ano.Mes"))

However, I get the following error:

Error in x[, ! na.check] : (subscript) Submit your request

Does anyone have any suggestions to fix this error?

I created this example with the Iris data structure of the R database and the same error occurs:

data(iris)
tst <- iris[1:120,]
tst$mes <- rep(1:12,times = 10)
tst$ano <- unlist(lapply(2007:2016, function(x) rep(x, times = 12)))
tst$ano.mes <- paste(tst$ano,"m",tst$mes, sep = "")
painel <- pdata.frame(tst, index = c("Species","ano.mes"))
modelo <- plm(formula = Sepal.Length ~ Sepal.Width | . ~ Petal.Length + Petal.Width + Species + ano.mes, data = as.matrix(painel), index = c("Species", "mes.ano"), model = "within")
  • It would be interesting if we could reproduce your code in order to verify what error is in it. Without having access to more information, my hypothesis is that the logical vector na.check has more elements than the number of columns of x. However, I don’t know what it is x, nor how na.check was built. So I suggest that the question be edited and more details about it be included.

  • has some reason to transform the painel in matrix in the code? I find it strange you turn it to pdata.frame and then use data = as.matrix(painel). It doesn’t end up undoing the transformation?

    1. It would be very difficult to divulge my data here for analysis to be done on them, so I created an example with the database "iris" of the R database. I put it in the same format as my data and ran the model, having the same error. (This code is in the answer below) 2) I do pdata.frame basically to create rownames. The as.Matrix is used, because plm asks the input data structure to be a matrix. At the end of the code, we have a Matrix with the rownames of a panel..

2 answers

1

The first problem is:

  1. the package receives a data.frame as input. This can be found in the documentation.

date to date.,

  1. When you are adjusting the model with the matrix, it gives this error. When you adjust the model with the data.frame, it gives the following error. Probably why you thought the input should be an array.

Error in model.frame.default(Terms(formula, lhs = lhs, rhs = rhs, data = date, : Object is not a Matrix

This is a problem from before running the model. This error is in the function that transforms its formula into a matrix that will be consumed by the model next. That is, it is not a problem specific to the package plm.

This mistake happening, the only thing I can think of is that its formula is incorrect. In fact, I don’t know this specification using y ~ a + b | . ~ a + b + c + d

Note that this no longer works:

> model.frame.default(. ~ Petal.Length + Petal.Width + Species, data = painel)
Error in eval(expr, envir, enclos) : object '.' not found

All this to say, Is your formula correct? Did you not want a formula like: Sepal.Length ~ Sepal.Width | Petal.Length + Petal.Width + Species + ano.mes where the model normally runs?

1


Well I see here that the error may be due to the fact that two formulas are being defined where there should be only one define.

modelo <- plm(formula = Sepal.Length **~** Sepal.Width | .  **~** Petal.Length + Petal.Width + Species + ano.mes, data = (painel), index = c("Species", "mes.ano"), model = "within")

item "." refers to the previously defined formula.

More can be seen on https://cran.r-project.org/web/packages/plm/vignettes/plm.pdf

4.3. Formulas There are Circumstances Where standard formula are not very usefull to > describe a model, notably while using instrumental variable like estimators: to Deal with These situations, we use the Formula package. The Formula package provides a class which unables to Construct Multipart formula, each part being separated by a pipe Sign. plm provides a pFormula Object which is a Formula with specific methods.

Using the problem that friend Daniel addressed and the problem I’m pointing out a possible solution is:

modelo <- plm(formula = Sepal.Length ~ Sepal.Width | . + Petal.Length + Petal.Width + Species + ano.mes, data = (painel), index = c("Species", "mes.ano"), model = "within") 

hope I helped. Hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.