strengejacke / sjPlot

sjPlot - Data Visualization for Statistics in Social Science

Home Page:https://strengejacke.github.io/sjPlot

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Custom back-transformation function not working with stan_glm model in plot_model

jamey5k opened this issue · comments

@strengejacke

I am using plot_model to create a marginal effects plot from a negative binomial regression model from the stanglm function in rstanarm. Some variables in the raw data (Var1, Var2, Var3) for the model were center measured and scaled to a SD of 1 (i.e., Var1_scale). I would like to back-transform these for plotting. To do this, I created a function:

BackTransform <- function(x) 
  
{x * attr(x,"scaled:scale") + attr(x,"scaled:center")}

I use plot_model to create plots, and this works fine with the untransformed data:

plot_model(Figure_Model, type = "pred", terms = c("Var1_scale","Var2","Var3_scale"), )

However, when I try to use my custom BackTransform function, I get an error:
plot_model(Figure_Model, type = "pred", terms = c("Var1_scale [BackTransform]","Var2","Var3_scale"), )

Error in attributes(.Data) <- c(attributes(.Data), attrib) :
length of 'dimnames' [2] not equal to array extent

When I have tested built-in functions, such as [Exp] the code does work.

Does anybody have insight as to why this issue is happening and if it can be addressed?
Thanks!