1

I'm estimating a series of country and year fixed-effects panel models using the felm function. I would like to calculate the number of countries included in each estimation. I can easily do this if I run my regressions using the plm function, as shown below. However, I don't want to run my regressions using plm for other reasons. Is there a way of calculating the number of id's used in a felm estimation?
a sample code is given below:

library(plm); library(lfe)
data("Grunfeld", package = "plm")
reg_plm <- plm(inv ~ value + capital,   data = Grunfeld, model = "within", effect = "twoways")
reg_felm <- felm(inv ~ value + capital  | 
                  firm + year, data=Grunfeld)

pdim(reg_plm)[["panel.names"]]$id.names 

Note that my data is an unbalanced panel and pdim does not work with a felm object.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
user3227641
  • 157
  • 7

2 Answers2

2

Using str(reg_felm) - as it is usual in such cases, in search of objects - reveals that the fixed effects are stored in fe.

We could display the levels

levels(reg_felm$fe$firm)
# [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

where their number is

length(levels(reg_felm$fe$firm))
# [1] 10

or use a table to see if it's balanced.

reg_felm$fe$firm |> table()
#  1  2  3  4  5  6  7  8  9 10 
# 20 20 20 20 20 20 20 20 20 20 
jay.sf
  • 60,139
  • 8
  • 53
  • 110
0

It may be easier to use nlevels

nlevels(reg_felm$fe$firm)
akrun
  • 874,273
  • 37
  • 540
  • 662