# Within panel estimator - how to perform standard error adjustment?

I am fitting a within panel model via glm in R and I am trying to understand standard error adjustment as in plm package (to account for loss of degrees of freedom via demeaning):

Consider the following within model fitted via glm:

library(plm)
data("Produc", package = "plm")

df_train_within <- Produc %>%
group_by(state) %>%
mutate(gsp = gsp - mean(gsp),pcap = pcap - mean(pcap))

fit.glm <- glm(gsp ~ pcap, data=df_train_within)


Output glm model:

summary(fit.glm)$coefficients[2,1:2] Estimate Std. Error 2.8493965 0.1189785  Now consider the output from the model fitted via plm: fit.plm <- plm(gsp ~ pcap, data = Produc, model = "within", index = c("state","year")) summary(fit.plm)$coefficients[1,1:2]

Estimate   Std. Error
2.8493965  0.1225697


As can be seen, the coefficient estimates are the same for glm and plm, but the standard error is too low for glm. According to Cameron/Trivedi we need to inflate the residual variance by factor 1/[N(T-1)-K]*[N*T-K]. Performing this adjustment for the glm standard error using N=48, T=17, K=1 I was not able to re-produce the standard error reported in the plm package.

Can anyone help me in explaining how the adjustment is performed in the plm-package? I wasn't able to find the relevant piece of source code which could also be very helpful

The degrees of freedom adjustment for fixed effects models in plm is handled by the internal function plm.fit. Here is the relevant part:

if (model == "within") {
pdim <- pdim(data)
card.fixef <- switch(effect,
individual = pdim$$nT$$n,
time = pdim$$nT$$T,
twoways = pdim$$nT$$n + pdim$$nT$$T - 1)
df <- df.residual(result) - card.fixef
vcov <- result\$vcov * df.residual(result)/df
}


Please take heed: As the way you estimate the model with glm includes an intercept and plm does not include an intercept, your manual adjustment would need to take care of that as well if you want the numbers to match excactly.