The regression runs just fine in Stata, but R gives me error message: "Insufficient number of time periods".
Why? (I have also tried changing the order for index, with "Year", "isin".

When I include companies that are listed for 10 years or more, the regression runs fine.

BUT, using this same data on R and Stata (where companies under 10 years are removed), the results are completely different.

This is what I do in Stata:
encode isin, gen(ID)
format year %ty
xtset ID year
asreg y x1 x2 x3 x4 , fmb newey(1)

Is there another way to run Fama Macbeth in R? Why does the result differ from Stata?
How do I implement the Stata procedure into R?

You mention trying switching the positions of "Year" and "isin" in the index argument with no success. I am not sure why because it is working for me. Here is my full code:

# Load packages ----
library(here)
library(plm)
library(readr)
# Import the data ----
dat <- read_csv(here("data/testdata.csv"))
# Run Fama Macbeth panel regression ----
fm_model <- pmg(formula = Y ~ x1 + x2 + x3 + x4,
data = dat,
index = c("Year","isin"))
fm_model
Model Formula: Y ~ x1 + x2 + x3 + x4
Coefficients:
(Intercept) x1 x2 x3 x4
0.829776 -0.013092 -0.044625 -0.187797 0.021546
summary(fm_model)
Mean Groups model
Call:
pmg(formula = Y ~ x1 + x2 + x3 + x4, data = dat, index = c("Year",
"isin"))
Unbalanced Panel: n = 30, T = 40-144, N = 3024
Residuals:
Min. 1st Qu. Median 3rd Qu. Max.
-3.09429009 -0.35156882 -0.07151865 0.21601332 21.52348689
Coefficients:
Estimate Std. Error z-value Pr(>|z|)
(Intercept) 0.829776 0.268053 3.0956 0.001964 **
x1 -0.013092 0.067968 -0.1926 0.847261
x2 -0.044625 0.016263 -2.7440 0.006070 **
x3 -0.187797 0.101419 -1.8517 0.064069 .
x4 0.021546 0.033033 0.6523 0.514224
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Total Sum of Squares: 2840.8
Residual Sum of Squares: 2112.7
Multiple R-squared: 0.25632

Well, actually it worked for me now! I guess I have been playing around with so many datasets, that I lost track of which I had not tried Thanks a lot!