Edit: I have updated this question with the solution below:
I am trying to understand how pglm estimates fixed effects models for binomial dependent variables by replicating the same model with the dummy variable method and the variance decomposition method in R. I am able to replicate the linear models produced via plm perfectly, but I cannot figure out how to replicate pglm's estimates via the same procedures. Would anyone know what I am missing or doing wrong here?
Below illustrates how I replicate plm and pglm using linear models and nonlinear models with plm's "Produc" dataset. If the required libraries are installed, The script should work if copied and pasted into directly into one's R console.
#0) set seed
set.seed(1)
#1) load libraries
library(plm)
library(data.table)
library(bife)
#2) prepare data
#a) prepare base data
#i) load produc
data("Produc", package = "plm")
#ii) convert to data table
Produc <- data.table(Produc)
#iii) create some fake dummy variables, rescale independent variables, remove the rest of the df
df <- Produc[,.(
binvar = rbinom(nrow(Produc),1,.02),
dv.binvar = rbinom(nrow(Produc),1,.04 * abs(scale(pcap)) * abs(scale(emp))),
pcap = pcap/10000,
pc = pc/10000,
emp = emp/10000,
state = state
)]
#iv) make binvar time-invariant
df[, ("binvar") := lapply(.SD, max), by = "state", .SDcols = "binvar"]
#b) prepare variance decomposition version of data
#i) find mean difference for all variables
df.decomp <- df[, lapply(.SD, function(x) x - mean((x))), .SDcols = c("pcap", "pc", "emp", "binvar", "dv.binvar"), by = "state"]
#ii) add non-transformed versions of dummy variables back into the data
df.decomp[,':='(
ti.binvar = df$binvar,
ti.dv.binvar = df$dv.binvar
)]
#3) linear models
#a) estimate linear models
m.plm <- plm(dv.binvar ~ pcap + pc + emp + emp:binvar, data = df, index = c("state"))
m.lm <- lm(dv.binvar ~ pcap + pc + emp + emp:binvar + state - 1, data = df)
m.decomp <- lm(dv.binvar ~ pcap + pc + emp + emp:ti.binvar - 1, data = df.decomp)
#b) format results of linear models
r.plm <- round(summary(m.plm)$coefficients,2)
r.lm <- round(summary(m.lm)$coefficients,2)
r.lm <- r.lm[!grepl("state", row.names(r.lm)),]
r.decomp <- round(summary(m.decomp)$coefficients,2)
#c) print results of linear models
r.plm
r.lm
r.decomp
#all estimates are the same
#4) nonlinear models
#a) estimate nonlinear models
#m.pglm <- pglm(dv.binvar ~ pcap + pc + emp + emp:binvar, data = df, index = c("state"), family = binomial)
m.bife <- bife(dv.binvar ~ pcap + pc + emp + emp:binvar | state, data = df, model = "logit")
m.glm <- glm(dv.binvar ~ pcap + pc + emp + emp:binvar + state - 1, data = df, family = binomial)
m.gdecomp <- glm(ti.dv.binvar ~ pcap + pc + emp + emp:ti.binvar - 1, data = df.decomp, family = binomial)
#b) format results of nonlinear models
r.bife <- round(summary(m.bife)$cm,2)
r.glm <- round(summary(m.glm)$coefficients,2)
r.glm <- r.glm[!grepl("state", row.names(r.glm)),]
r.gdecomp <- round(summary(m.gdecomp)$coefficients,2)
#c) print results of nonlinear models
r.bife
r.glm
r.gdecomp
#The variance decomposition method is very different!
Thanks, for all of your feedback. It seems I made two key errors:
Computational implementation: I assumed that the default method in pglm is "within", as it is in plm. Moreover, it appears that pglm does not even support within person fixed effects! I have updated the syntax in my question so that it uses bife. This returns the same estimates as my dummy variable method, but not my variance decomposition model.
The variance decomposition or "demeaning" method cannot work because the model is nonlinear. Demeaning variables wholly changes their relationship with the dependent variable. Moreover, demeaning the dependent variable expands its range beyond 0-1.
It seems that the best method for implementing logistic fixed effects models in R is to implement a "pseudo-demeaning" algorithm, such as that the one used by bife. A detailed explanation for this method is described in: Fernández-Val, I., & Weidner, M. (2018). Fixed effects estimation of large-t panel data models. Annual Review of Economics, 10, 109-138.