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

I'm running multiple linear regressions in R. I initially ran with the "pooled" effect and it worked fine. When I try to do the same with the effect "within" and "random" it presents the following error:

Error in class (x)

Can anyone give me a light of the mistake I'm making? I take the opportunity to apologize if I you're 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)
Author: Tomás Barcellos, 2020-05-01

1 answers

Brief explanation (or not so much)

The error is in the function call plm. See that the Formula, first argument, should reflect the relationship between the variables present in the second argument (data).

Thus, what happens is that R searches X and Y in pdata, it does not find these variables there, because they do not exist, and then, following the scope rules of R, it will fetch them in its global environment and finds them. But ai occurs that y defined in the environment global has a mismatch with function plm and that error appears.

Let's play the error and its solution.

Error playback

Since you did not offer a way for us to reproduce your data, I will use the data from the function documentation, 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)

Solution

To solve this problem, but swap the Y ~ X in the formula for the variables that make them up. 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 the other types of models:

mod2 <- plm(inv ~ value + capital, data = Grunfeld, model = "pooling")
mod3 <- plm(inv ~ value + capital, data = Grunfeld, model = "within")
 1
Author: Tomás Barcellos, 2020-05-02 00:48:35