Error in class(x) <- setdiff(class(x), "pseries") :

Asked

Viewed 103 times

0

I’m running multiple linear regressions on R. Initially I circled with the "pooled" effect and it worked fine. When I try to do the same with the effect "Within" and "Random" shows the following error:

Error in class(x) <- setdiff(class(x), "pseries") : invalid to set the class to Matrix unless the Dimension attribute is of length 2 (was 0)

Can anyone shed any light on the mistake I’m making? I take this opportunity to apologize if I’m doing something really stupid.

##INSTALAÇÃO DE PACOTES##
list.of.packages <- c("AER","tidyverse", "gdata","DT","ggthemes", "RCurl", "readxl", "zoo", "ggcorrplot", "reprex", "ggmap","ggalt",  "devtools", "gapminder", "gganimate", "cowplot", "xts", "PerformanceAnalytics", "fmsb", "viridis", "tidyverse", "sandwich", "lmtest", "qqman", "car", "dplyr", "stargazer", "ggplot2", "foreign","openintro", "nlme", "OIdata", "gdata", "pdflscape", "doBy","ivpack", "gplots", "psych","plm","cluster.datasets", "readxl")
new.packages <- list.of.packages[!(list.of.packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
lapply(list.of.packages, require, character.only = TRUE)
##

## CAMINHO DO PROJETO## 
library(readxl)
library(plm)
Dados <- read_excel("Testes/Dissertreg.xlsx")


##DECLARAÇÃO de VARIÁVEIS
Y <- cbind(Produtividade)
X <- cbind(Racao, Silagem, Preco)

##SETANDO DADOS EM PAINEL
pdata <- pdata.frame(Dados, index=c("id", "t")) 


##ESTATÍSTICA DESCRITIVA
summary(Y)
summary(X)

##Pooled OLS Estimator
pooling <- plm(Y ~ X, data = pdata, model = "pooling")
summary(pooling)

##Efeito Fixo ou within
fixo <- plm(Y ~ X, data = pdata, model = "within")
summay(fixo)

##Efeito Aleatório
random <- plm(Y ~ X, data = pdata, model = "random")
summary(random)

1 answer

1


Brief explanation (or not so much)

Error is in function call plm. Note that the formula, first argument, should reflect the relationship between the variables present in the second argument (data).

Thus, what happens is that the R quest X and Y in pdata, do not find these variables there, because they do not exist, and then, following the scope rules of R, will pick them up in your global environment and finds. But then it occurs that Y defined in the global environment has an incompatibility with the function plm and that error appears.

Let’s reproduce the error and its solution.

Reproduction of the Error

As you did not offer a way to reproduce your data, I will use the function documentation data, but I will reproduce the errors.

library(plm)
data("Grunfeld", package = "plm")
Y <- cbind(Grunfeld$inv)
X <- cbind(Grunfeld$value, Grunfeld$capital)
erro <- plm(Y ~ X, data = Grunfeld, model = "random")

Error in class(x) <- setdiff(class(x), "pseries") : invalid specify the class as matrix unless the dimension attribute has length 2 (0 found)

Solution

To solve this problem, but change the Y ~ X in the formula for the variables that compose them. So we would have:

mod1 <- plm(inv ~ value + capital, data = Grunfeld, model = "random")
mod1

#> Model Formula: inv ~ value + capital
#> 
#> Coefficients:
#> (Intercept)       value     capital 
#>   -57.83441     0.10978     0.30811 

And the same goes for other types of models:

mod2 <- plm(inv ~ value + capital, data = Grunfeld, model = "pooling")
mod3 <- plm(inv ~ value + capital, data = Grunfeld, model = "within")
  • By the way, the mistake comes from a cbind the most, Y <- Grunfeld$inv Enough and the rest of the code already runs smoothly. With cbind the response vector is a class object "matrix".

  • It worked perfectly. Thank you very much.

Browser other questions tagged

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