Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Easier way to get the quasi-demeaned variables #2

Open
tappek opened this issue Dec 4, 2022 · 1 comment
Open

Easier way to get the quasi-demeaned variables #2

tappek opened this issue Dec 4, 2022 · 1 comment

Comments

@tappek
Copy link

tappek commented Dec 4, 2022

Hi there,
I was wondering whether you are aware of the functionality in the plm package to get the quasi-demeaned variables without doing the calcuclation yourself (as in here

qdmean/R/qdmean.R

Lines 105 to 112 in 10f15b6

data <- data.frame(model$model, index(model), summary(model)$ercomp$theta)
names(data) <- c(names(model$model), names(index(model)), "theta")
# average over the predictor, given the grouping variable (both user supplied)
data$xmean <- as.numeric(ave(data[[predictor]], data[[group]],
FUN = function(x) mean(x, na.rm = T)))
x.full <- as.numeric(data[[predictor]])
# generate x.qdmean to return
x.qdmean <- x.full - data$theta*data$xmean
)?

Just use model.matrix() on a plm model and you get the model matrix (quasi-demeaned variables of the RHS) and pmodel.repsonse for the quasi-demeaned response.

Buidling upon your example in the help page:

library(plm)
model.plm <- plm(ln_wage ~ wks_work + union, index = c("idcode", "year"),
         data = nlswork,
         model = "random", random.method = "swar", effect = "individual")

wks_work.quasidemeaned <- model.matrix(model.plm)[ , "wks_work"]
resp.demeaned  <- pmodel.response(model.plm)

Works likewise for demeaned variables of FE models.

One caveat though: for unbalanced two-way RE models, model.response and pmodel.repsonse output the original variables due to the specific estimation technique used for these models.

@andyphilips
Copy link
Owner

Thanks @tappek ! I'll forward this over to my coauthor who wrote the R portion of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants