strengejacke / ggeffects

Estimated Marginal Means and Marginal Effects from Regression Models for ggplot2
https://strengejacke.github.io/ggeffects
Other
549 stars 35 forks source link

ggeffects capabilities with spatail regression model objects #474

Open amikulas opened 6 months ago

amikulas commented 6 months ago

This is a great package. Incredibly useful for plotting models with complicated interaction terms.

I use spatial regression and I am trying to use ggeffects with a model object, type Sarlm (produced from the spatialreg R package, errorsarlm model).

When I try to use any ggeffects command, I get errors like this:

> ggpredict(Sarlm_model,
          terms = c("var1_continuous"))

Error in show_pretty_message && model_info$is_binomial :  invalid 'y' type in 'x && y'

Additionally, margins does not work with these model objects. When I attempt margins, I get errors like:

> margins(Sarlm_model, variables = "var1_continuous")

Error in x$terms %||% attr(x, "terms") %||% stop("no terms component nor attribute") : 
  no terms component nor attribute

It does not look like ggeffects currently supports Sarlm objects based on the model object list here.

These model objects also do not work for any commands in the margins package. They can be accessed by the insights package.

I have two questions. The first is if you know of ggeffects work arounds for these types of model objects. I have looked into how ggeffects uses the insight package to index model parameters, and I do not think the issue is with insight. The issue may lie with margins, however, if ggeffects employs margins dependencies. My issue may also be related to closed issue number 93.

The second is if you would consider and update that allows ggeffects to work with Sarlm objects. I know many (at least online, here and here) are trying to figure out how to model and plot interaction effects with spatial data sets and spatial regression model output.

The issue may be in that these models predict coefficients (direct effect), lag coefficients (indirect effect), and a lamda (effect of spatially autocorrelated OLS errors). These coefficients effects are uninterpretable unless you calculate the total effect (direct + indirect-lag effect + error effect), total SE, and total p-value for each model term. The spatailreg::impacts function does this.

strengejacke commented 6 months ago

Do you have a reproducible example?

amikulas commented 6 months ago

I do. The attached rscript should work as is if you have the packages installed. Be warned, Mac users may need additional dependencies to use the spdep and sf packages, as they access the machine's existing geospatial programming.

ggeffects_help.txt