Error in plm random effects Swamy–Arora (swar) estimator with lagged dependent I am using synthetic data with a model that has a lagged dependent variable. When estimating a random effects model using Swamy–Arora estimator (default) with package plm, I get the error
Error in swar(object, data, effect) : 
  the estimated variance of the individual effect is negative

But, if I try with any other of the available estimators (namely, "walhus", "amemiya", "nerlove" or  "kinla"), then everything works fine.
Below is a code that shows the issue.
###########################################################
# Script to show problem with swar estimator of r.e. in plm
# Date: 20151013
###########################################################

# Dependencies
library(MASS) # to sample correlated multinormal
library(plm)

#########################################################################
# CREATE TEST DATA
# This part creates a panel with H individuals and S observations each
# The model has a lagged dependent variable with weight rho
# We also use k multinormal correlated (r) predictors, with weights = 1:k
# We use random effects u = 1:H
# Series start at y0
#########################################################################

k = 5
r = 0.5
S = 1000
R = mat.or.vec(k, k) + r
diag(R) = 1
y0 = 1
rho = 0.9
sigma_e = 3
H = 10

for(h in 1:H){
  # Predictors
  X = mvrnorm(S, rep(0, k), R)

  # Dependent
  y = mat.or.vec(S, 1)
  y[1] = y0
  eps = rnorm(S, 0, sigma_e)

  for(i in 2:S){
    y[i] = rho * y[i-1] + as.numeric(X[i,] %*% 1:k) + eps[i] + h
  }

  # we create a lagged depedendent variable manually to see if this fixes
  # the problem
  temp = data.frame(y[2:S], y[1:(S-1)], X[2:S, ], rep(h, S-1))
  names(temp) = c("y", "y.l", paste("x", 1:5, sep=""), "id")
  temp$t = 1:(S-1)

  if (h == 1) {
    datos = temp
  } else {
    datos = rbind(datos, temp)
  }
}

# Finished

######################################################################
# ESTIMATE THE PANEL
# First we estimate F.E.: it work perfectly
# Then we estimate R.E.
######################################################################

pdatos = pdata.frame(datos, index = c("id", "t"))

# Fixed effects
fitfe = plm(y ~ lag(y, 1) + x1 + x2 + x3 + x4 + x5, data = pdatos)
summary(fitfe) # Beta OK
sqrt(sum(fitfe$residuals ^ 2) / fitfe$df.residual) # sigma_e OK
summary(fixef(fitfe)) # F.E. OK

# Random effects: swar
fitre = plm(y ~ lag(y, 1) + x1 + x2 + x3 + x4 + x5,
            data = pdatos, model = "random", random.method = "swar") # Fails

# manually created lag
fitre = plm(y ~ y.l + x1 + x2 + x3 + x4 + x5,
          data = pdatos, model = "random", random.method = "swar") # Fails

# Random effects: other methods
fitre = plm(y ~ lag(y, 1) + x1 + x2 + x3 + x4 + x5,
            data = pdatos, model = "random", random.method = "walhus") # OK
summary(fitre)
fitre = plm(y ~ lag(y, 1) + x1 + x2 + x3 + x4 + x5,
            data = pdatos, model = "random", random.method = "amemiya") # OK
summary(fitre)
fitre = plm(y ~ lag(y, 1) + x1 + x2 + x3 + x4 + x5,
            data = pdatos, model = "random", random.method = "nerlove") # OK
summary(fitre)
fitre = plm(y ~ lag(y, 1) + x1 + x2 + x3 + x4 + x5,
            data = pdatos, model = "random", random.method = "kinla") # OK
summary(fitre)

# End

 A: The error message is correct; this is not an error with the plm package in particular. The default Swamy and Arora (1972) estimator (random.method="swar" is used if not something else is explicitly stated by the user) is not guaranteed to yield positive estimates for the variance.

Wooldridge (2010), p. 296 has some advice:
„As a practical matter, equation (10.37) is not guaranteed to be
positive, although it is in the vast majority of applications. A
negative value for sigma_c^2 is indicative of negative serial
correlation in u_it, probably a substantial amount, which means that
Assumption RE.3a is violated. Alternatively, some other assumption in
the model can be false. We should make sure that time dummies are
included in the model if aggregate effects are important; omitting
them can induce serial correlation in the implied u_it. When the
intercepts are allowed to change freely over time, the effects of
other aggregate variables will not be identified. If sigma_c^2 is
negative, unrestricted FGLS may be called for; see Section 10.4.3."

Sometimes, just adding more control variables or transforming variables also help to overcome this issue (from a technical viewpoint) if you want to stick with the swar estimator.
