This query is specific to how the R package brms handles scaling. (I put this question first to a more general forum, but to no avail.)
I want to scale a predictor variable before applying a regression model in R using brms. I then want to plot the original unscaled values of the predictor using conditional effects for intelligibility. Is there a native way to do this using brms, or a means of doing so using ggplot2, familiar to the community?
I have included a superlatively simple example in R below.
library('brms')
df <- datasets::PlantGrowth
df$weight.s <- scale(df$weight)
mod <- brm(weight.s~group,data=df,family = gaussian())
# Plot the various treatments using conditional effects with the scaled predictor
p <- conditional_effects(mod)
plot(p)[[1]]
The above code runs a brms model and plots the output of conditonal effects against the scaled predictor.
I then attempted to rescale them as follows:
SD_scale <- attr(df$weight.s, "scaled:scale")
center <- attr(df$weight.s, "scaled:center")
unscale_trans <- function(x){
scales::trans_new(
"unscale",
function(x) center + x * SD_scale,
function(x) scale(x) )
}
library('ggplot2')
p <- conditional_effects(mod)
plot(p)[[1]] + scale_y_continuous(trans="unscale")
The resultant plot is on the original scale but with grid lines according to the scaled predictor, whereas they ought to be according to the unscaled. I hope that since scaling predictors is widely applied, someone else will have encountered this issue. : )
If all you want is to scale the response variable (as is in your example), you can probably use the transform argument to conditional_effects.
For predictors, I don’t think there is a built-in way to do this withing brms (but @paul.buerkner may correct me if there is built-in support for scaling). I think the best way to approach this is to realize that conditional_effects is a relatively simple function: it calls posterior_epred to build predictions while varying a single parameter and keeping all other at mean (continuous) / reference level (factors). Those predictions are then passed to ggplot. So you should be able to recreate the plot yourself without big effort and once you have it, you can apply any transformations you need to the predictions before plotting them.
If you want to do this within ggplot than this is really more of a ggplot question (and thus not so well suited for this forum), but note that there are two places to apply a transformatio= either via scale_XXX or via coord_trans, I am not a ggplot expert, but I would guess that there is a combination of coord and scale transformation that does what you want (I didn’t try to find it though).
Hey John, could you share your code/how you solved it? I also have scaled predictors and I would like to plot the conditional effects on raw (unscaled) scale. Any help is appreciated. Thank you!
unscale_response ← function(ce, original_scale) {
response_var ← names(ce)[1] # Assuming the response variable is the first element
ce[[response_var]]$estimate__ ← ce[[response_var]]$estimate__ * attr(original_scale, “scaled:scale”) + attr(original_scale, “scaled:center”)
ce
}
unscale_predictor ← function(ce, original_scale) {
predictor_var ← names(ce)[1] # Assuming the predictor variable is the first element
ce[[predictor_var]]$effect1__ ← ce[[predictor_var]]$effect1__ * attr(original_scale, “scaled:scale”) + attr(original_scale, “scaled:center”)
ce
}