# Is it ok to run a plm fixed effect model and add a factor dummy variable in R?

Is it ok to run a "plm" fixed effect model and add a factor dummy variable in R as below?

The three factors "Time", "Firm” and "Country" are all separate indices which I want to fix all together.

Instead of making two indices in total by combining "Firm” and "Country", I find the below specification works much better for my case.

Is this an acceptable format?

     plm(y ~ lag(x1, 1) + x2 + x3 + x4 + x5 + factor(Country),
data = DATA, index = c("Firm”,"Time"), model="within")


Some other helper told me that cluster='group' with his code below will refer to "id" and not to "country", so the standard errors will be wrong. He says it seems that clustering by the additional factor with plm is currently not possible, at least he is not aware of anything.

     plm::plm(y ~ d + x1 + x2 + x3 + x4 + factor(Country),
index=c('Firm', 'Time'), model='within',
effect='twoways', dat)
summary(fit2, vcov=plm::vcovHC(fit2, cluster='group',
type='HC1'))\$coe


If you do fixed effects for Firm and Time, then does this necessarily mean I need to cluster according to Firm and Time as well?

What is the implication of the model and result if I do not cluster within the summary command according to Country? Does this mean I am not imposing Country fixed effects even though I might not need its clustering as I am mainly looking into cross-country effects?

With simply Firm and Time fixed effects, I could simply run using summary without any further specifications like clustering. What does this mean?

• Are the ID’s nested within your countries? Jan 25 at 2:39
• @ThomasBilach ID are firms.
– Eric
Jan 25 at 8:01
• Seems okay. But what are you looking to do? You’re estimating a model with country and time fixed effects. Do you just want a model that runs? Jan 25 at 8:48
• @ThomasBilach: Amended my question above. Please check. Also see stackoverflow.com/questions/70842406/…
– Eric
Jan 25 at 11:05

First, it's important to get a handle on what you're actually estimating. I suspect you observe firms over time, but the firms appear to be nested within industries or countries. Here's what you're trying to do in plm:

# The one-way fixed effects model

# library(plm)

plm(y ~ ... + as.factor(country),  # covariates
index = c("firm", "year"),     # panel identifiers
model = "within",              # "within model" (i.e., firm fixed effects)
data = dat)


Note that this equation is only estimating firm fixed effects. To estimate firm and year fixed effects, you must specify effect = "twoways". Your code also includes dummy variables for all countries in your panel. If I may wager a guess, you more than likely have one firm per country. Individuals may move around, but firms usually don't. Thus, if I know the firm, then I know its location (e.g., region, zip code, country, etc.). As a consequence, the country dummies are collinear with the firm fixed effects. Including the country dummies is another way of saying that you want to estimate firm and country fixed effects simultaneously. But given the nested structure present in your data, the firm effects will absorb the country effects. Since the firm effects do take into account the heterogeneity among your countries, they obviate the need to explicitly include dummy variables for each individual country.

If you do fixed effects for Firm and Time, then does this necessarily mean I need to cluster according to Firm and Time as well?

No.

However, it's quite common to one-way cluster on the panel level identifier. The firm fixed effects center each firm's residuals around zero, but it's hardly addressing the intra-firm correlation of the errors. To be clear, the "fixed effects" account for unobserved heterogeneity between units (i.e., firms, industries, states, etc.), but it doesn't address the within-unit dependence among observations. In your setting, clustering at the lower level addresses the temporal interdependence of the firm level observations.

As a general rule, you should never cluster blindly. It depends upon the error structure. Given the nested structure of your data, I would consider clustering at the industry or country level. It's reasonable to assume that the individual firm-level observations belonging to a particular region/country are not independent. Think of all the macro-level factors (e.g., inflation) that may influence the subset of firms located within a given country.

On the other hand, you should also consider clustering at the firm level. It's hard to dismiss the dependence inherent in the firm level observations over time. If you do decide to cluster on firms, you may find your uncertainty estimates are slightly more conservative.

What is the implication of the model and result if I do not cluster within the summary command according to Country?

The plm() function doesn't automatically cluster by the panel identifier(s). You must specify your clustering scheme manually. The problem is you're restricted to clustering by "group" or "time". According to the default properties of plm, cluster = "group" clusters your standard errors by firm—not country.

Be careful, because renaming the index "country" instead of "firm" returns a lengthy error message. In the presence of the firm level observations, R becomes surly; it warns you that you have duplicate id-time pairs. Note, plm requires unique firm-year couples. To overcome this, instantiate a new 'firm-country' variable and use that as your new panel level identifier. This will rid you of the annoying error message, though multi-dimensional clustering does become a bit clunky in this setting.

New packages do support clustering on more than one dimension. It's not uncommon to find, for example, clustering by country and year, or industry and year, depending upon the granularity of your data. To put this in words, clustering by country and year is simultaneously allowing the errors to be correlated for firms in the same country and for firms in the same year.

In R, you could estimate the following:

# A base R solution

mod <- lm(y ~ lag(x1, 1) + x2 + x3 + x4 + x5 + as.factor(firm) + as.factor(year) + as.factor(country), data = dat)

# library(lmtest)
# library(sandwich)

coeftest(
mod, vcov. = vcovCL(mod, cluster = ~ country + year, fix = TRUE)) |>  # double clustering on country and year
{\(.) .[!grepl('\\(|factor', rownames(.)), ]}()


I included the country dummies, though R will safely omit them. Try out this equation with, and without, the country dummies. Your estimates should remain unchanged. The final line of code removes extraneous output. Ignore this if you actually want to see all the fixed effects, even the ones R excludes. But please don't report the fixed effects; they are nuisance parameters. Simple note which fixed effects were estimated and move on.

It's worth highlighting that with many fixed effects, the fixest package offers some great functionality. The feols() function will actually estimate many fixed effects on the fly and adjust your standard errors with a single line of code. Here's one way to specify your equation:

# library(fixest)

feols(y ~ lag(x1, 1) + x2 + x3 + x4 + x5 | country + year + firm, data = dat)


The variables on the right-hand side of the | represent the fixed effects. By default, we're clustering on the first of those variables: country. I recommend clustering at this level, though clustering by country and year isn't outside the realm of possibilities.

The problem with the fixest packages is we can cluster so fast that we never really stop and think about why we're clustering in the first place.

If you can justify your clustering scheme, then go for it!

Always cluster with caution.

• I agree with the answer by Thomas Bilach. I have two minor comments though: 1) I would question your statement about the necessity of keeping plm. My point is: 2) Linear mixed effects (LME) models offer nice way to generate the hierarchy you are looking for, while controlling the same effects (essentially) you are dealing with in plm. see e.g. cran.r-project.org/web/packages/lme4/vignettes/lmer.pdf This might be useful in cases where you have multiple firms per country and country effects + country-specific firm effects make sense. Jan 31 at 9:23