本文介绍了smooth.spline包装的stat_smooth(在ggplot2中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 对不起,如果这个问题是微不足道的,但我想弄清楚如何在R中绘制某种类型的自然立方样条(NCS),它完全躲过了我。 在上一个问题中,我学会了如何绘制由ggplot中的ns()命令生成的NCS,但我有兴趣了解如何绘制略有不同的NCS,在 pspline 包。据我所知,这是唯一一个可以根据给定数据集自动选择适当CV平滑惩罚的软件包。 理想情况下,我可以提供smooth.Pspline作为ggplot2中stat_smooth图层的一种方法。我现在的代码是: pre $ plot plot 我想用smooth.Pspline的功能替换lm公式。我做了一些Google搜索,并找到解决方案由Hadley编写的非常相似的B样条函数smooth.spline。但我无法完美地适应这种平滑。有没有人有这方面的经验? 非常感谢! 解决方案您只需检查 predict.smooth.Pspline 如何返回预测值。 在内部工作中 stat_smooth ,调用 predictdf 来创建平滑线。 predictdf 是 ggplot2 的一个内部(非导出)函数(它被定义为这里)它是一个标准的S3方法。 sm.spline 返回类 smooth.Pspline stat_smooth 来说,您需要为类创建 predictdf 的方法.Pspline 。 $ smP $ b 函数(公式,数据,...){ M sm.spline(x = M [,2],y = M [,1] ) #s3 method for predictdf(在stat_smooth中调用) predictdf.smooth.Pspline< - 函数(model,xseq,se,level){ pred data.frame(x = xseq,y = c(pred))} 一个例子(使用 mgcv :: gam 来比较pspline)。 mgcv 非常棒,在拟合方法和平滑样条线选择方面有很大的灵活性(尽管不是CV,只有GCV / UBRE / REML / ML) d d + geom_point()+ stat_smooth(method = smP,se = FALSE,color ='red',formula = y〜x)+ stat_smooth(method ='gam',color ='blue',formula = y〜s(x,bs ='ps')) Sorry if this question is trivial, but I'm trying to figure out how to plot a certain type of natural cubic spline (NCS) in R and it's completely eluded me.In a previous question I learned how to plot the NCS generated by the ns() command in ggplot, but I'm interested in how to plot a slightly different NCS generated the smooth.Pspline command in the pspline package. As far as I know this is the only package that automatically selects the proper smoothing penalty by CV for a given dataset.Ideally I would be able to provide smooth.Pspline as a method to a stat_smooth layer in ggplot2. My current code is like:plot <- ggplot(data_plot, aes(x=age, y=wOBA, color=playerID, group=playerID))plot <- plot + stat_smooth(method = lm, formula = y~ns(x,4),se=FALSE)I'd like to replace the "lm" formula with smooth.Pspline's functionality. I did a little bit of googling and found a solution to the very similar B-spline function smooth.spline, written by Hadley. But I haven't been able to adapt this to smooth.Pspline perfectly. Does anyone have experience with this?Thanks so much! 解决方案 You simply need to inspect how predict.smooth.Pspline returns the predicted values.In the internal workings of stat_smooth, predictdf is called to create the smoothed line. predictdf is an internal (non-exported) function of ggplot2 (it is defined here) it is a standard S3 method.sm.spline returns an object of class smooth.Pspline, therefore for stat_smooth to work you need to create method for predictdf for class smooth.Pspline.As such the following will work.smP <- function(formula,data,...){ M <- model.frame(formula, data) sm.spline(x =M[,2],y =M[,1])}# an s3 method for predictdf (called within stat_smooth)predictdf.smooth.Pspline <- function(model, xseq, se, level) { pred <- predict(model, xseq) data.frame(x = xseq, y = c(pred))}An example (with a pspline fitted using mgcv::gam as comparison). mgcv is awesome and gives great flexibility in fitting methods and smoothing spline choices (although not CV, only GCV/UBRE/REML/ML)d <- ggplot(mtcars, aes(qsec, wt))d + geom_point() + stat_smooth(method = smP, se= FALSE, colour='red', formula = y~x) +stat_smooth(method = 'gam', colour = 'blue', formula = y~s(x,bs='ps')) 这篇关于smooth.spline包装的stat_smooth(在ggplot2中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-05 20:41