I would like to use a weighted average smoother in ggplot2::geom_smooth()
but it is not one of the permitted options for method =
. Is the way to do it to calculate the smoothed values and add it as a layer on my ggplot scatterplot?
Sample data:
set.seed(0)
x <- seq(0, 50, 0.1)
prob <- - x*(x - 50)/1000
tmp_df <- data.frame(x, prob)
tmp_df <- tmp_df[rep(1:nrow(tmp_df), times = 10),]
tmp_df$success <- rbinom(n = nrow(tmp_df), size = 1, prob = tmp_df$prob)
And using method = 'gam'
on this data:
ggplot(tmp_df, aes(x = x, y = success)) +
geom_point(position = position_jitter(width = 0.2, height = 0.2), size = 0.1) +
geom_smooth()
producing: