[英]smooth.Pspline wrapper for stat_smooth (in ggplot2)
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. 对不起,如果这个问题很简单,但是我想弄清楚如何在R中绘制某种类型的自然三次样条(NCS)并且它完全没有我。
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. 在之前的一个问题中,我学会了如何在ggplot中绘制由ns()命令生成的NCS,但我对如何在pspline包中生成稍微不同的NCS生成smooth.Pspline命令感兴趣 。 As far as I know this is the only package that automatically selects the proper smoothing penalty by CV for a given dataset.
据我所知,这是唯一一个自动为CV给定数据集选择适当平滑罚分的包。
Ideally I would be able to provide smooth.Pspline as a method to a stat_smooth layer in ggplot2. 理想情况下,我可以提供smooth.Pspline作为ggplot2中stat_smooth图层的方法。 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. 我想用smooth.Pspline的功能替换“lm”公式。 I did a little bit of googling and found a solution to the very similar B-spline function smooth.spline, written by Hadley.
我做了一些谷歌搜索,并找到了一个非常相似的B样条函数smooth.spline的解决方案 ,由Hadley编写。 But I haven't been able to adapt this to smooth.Pspline perfectly.
但是我无法使其适应光滑.Pspline非常完美。 Does anyone have experience with this?
有任何人对此有经验吗?
Thanks so much! 非常感谢!
You simply need to inspect how predict.smooth.Pspline
returns the predicted values. 您只需要检查
predict.smooth.Pspline
如何返回预测值。
In the internal workings of stat_smooth
, predictdf
is called to create the smoothed line. 在
stat_smooth
的内部工作中, predictdf
来创建平滑线。 predictdf
is an internal (non-exported) function of ggplot2
(it is defined here ) it is a standard S3 method. predictdf
是内部(非导出)函数ggplot2
(它被定义在这里 )它是一个标准方法,S3。
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
. sm.spline
返回类smooth.Pspline
的对象,因此stat_smooth
工作,您需要为类smooth.Pspline
创建predictdf
方法。
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::gam
作为比较安装了mgcv::gam
)。 mgcv
is awesome and gives great flexibility in fitting methods and smoothing spline choices (although not CV, only GCV/UBRE/REML/ML) mgcv
非常棒,在拟合方法和平滑样条选择方面具有很大的灵活性(尽管不是CV,只有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'))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.