r-spatial / spatialreg

spatialreg: spatial models estimation and testing
https://r-spatial.github.io/spatialreg/
43 stars 11 forks source link

Marginal Effects with errorsarlm objects and impacts #48

Open amikulas opened 6 months ago

amikulas commented 6 months ago

Hello there. This is such a great package - thank you for the work in creating it. I am curious about best practices for calculating average marginal effects for interaction terms while using errorsarlm model objects. Standard packages that do this with many other model objects do not work with Sarlm model objects created by the errorsarlm model prediction.

Consider the model Y ~ X1 + X2 + X1*X2, where Y is continuous, X1 is continuous, and X2 is categorical. I calculate this basic OLS interaction model within the errorsarlm command to consider the role of correlated errors in the OLS, and the roll of spillover from the model Xs. I use the impacts command to view the total effects (direct + indirect + lamda), total SE, and total p-value for each coefficient.

I am trying to calculate the beta coefficient and standard error/p-value for the interaction term at various levels of X1. Typically, one uses the margins package or ggeffects package to do this. However, they do not handle errorsarlm model objects. I have inquiries into ggeffects and margins about expanding thier model capabilities, but the issue of considering the spatail weights matrixes when calculating SE I think will prevent these packages from considering a solution.

I have "brute forced" the total effects beta coefficients from the impacts command into the nested lm regression model object in order to use ggeffects and margins, but the standard errors are wrong, of course. The lm model object does not use the weights matrix for accounting for Rho, lamda, etc.

I can also easily calculate point estimates at different levels of the continuous X variable, but I am unsure how to calculate correct SEs. ad different X1 values.

Are there best practices for considering marginal effects at various levels of the interaction model terms? Or is this completed within the impacts command and I am misunderstanding the output or flexibility of impacts? Alternatively, can I use the impacts output and model object to do the work manually? (stated another way: Is the impacts function flexible to predict beta, SE, and p at various levels?)

Seems like others have similar issues, and I have found no real workaround in the Stack communities. See here.

Attached is a short script with a toy example (nonsense model), but the issue exists for all errorsarlm model objects, I believe.

marginal_effects_help.txt

Thank you for you attention and help. Apologies if this question has been considered already, or if I am misunderstanding or underutilizing the impacts output.

rsbivand commented 6 months ago

Thanks for your interest. The current state of play is approximately at: https://rsbivand.github.io/PG_AGII_2sem/10_talk.html (recent lecture notes) or: https://r-spatial.org/book/17-Econometrics.html#impacts (book section). We think we can see how to interface marginaleffects and maybe tinyplot and tinytable through marginaleffects for continuous variables but so far no code exists in this or other spatial econometrics packages. Discrete variables are far more problematic, and although dummies were used without reservation before, it looks as though reporting mechanisms need to be re-visited.

The point estimates of direct, indirect and total impacts and their standard errors for spatial Durbin error models (SDEM) and SLX models are reported by summary(impacts(<model_object>)) by linear combination. For spatial Durbin (SDM) and spatial lag (SLM aka SAR) models, the standard errors can come from simulation using matrix inversion or truncated matrix traces, or eigenvalues - see lecture notes above.

I guess we need to resolve the handling of categorical variables before coding an interface to marginaleffects. Any contributions very welcome!