Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Let’s say we have data
(See this previous post for more details.) Nearly-isotonic regression, introduced by Tibshirani et al. (2009) (Reference 1), generalizes isotonic regression by solving the problem
where
It turns out that, due to properties of the optimization problem, the nearly-isotonic regression fit can be computed for all
How is nearly-isotonic regression a generalization of isotonic regression? The term
Why might you want to use nearly-isotonic regression? One possible reason is to check if the assumption monotonicity is reasonable for your data. To do so, run nearly-isotonic regression with cross-validation on
You can perform nearly-isotonic regression in R with the neariso
package. The neariso()
function returns fits for an entire path of
Note 1: The formulation for nearly-isotonic regression above assumes that the points
to account for the different-sized gaps. The neariso
package only seems to handle the case where the
Note 2: The animation above was created by generating separate .png files for each value of magick
package. My initial hope was to create an animation that would transition smoothly between the different fits using the gganimate
package but the transitions weren’t as smooth as I would have imagined them to be:
Does anyone know how this issue could be fixed? Code for the animation is below, full code available here.
p <- ggplot(df, aes(x = x, y = beta)) + geom_path(col = "blue") + geom_point(data = truth_df, aes(x = x, y = y), shape = 4) + labs(title = "Nearly isotonic regression fits", subtitle = paste("Lambda = ", "{lambda[as.integer(closest_state)]}")) + transition_states(iter, transition_length = 1, state_length = 2) + theme_bw() + theme(plot.title = element_text(size = rel(1.5), face = "bold")) animate(p, fps = 5)
References:
- Tibshirani, R. J., Hoefling, H., and Tibshirani, R. (2011). Nearly-isotonic regression.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.