mon*_*ano 6 regression r spline curve-fitting smoothing
我有几个数据点似乎适合通过它们拟合样条曲线.当我这样做时,我会得到一个相当凹凸不平的合身,比如过度拟合,这不是我理解为平滑的.
是否有一个特殊的选项/参数来恢复像这里一样非常平滑的样条曲线的功能.
的的使用penalty参数smooth.spline没有任何明显的效果.也许我做错了?
这是数据和代码:
results <- structure(
list(
beta = c(
0.983790622281964, 0.645152464354322,
0.924104713597375, 0.657703886566088, 0.788138034115623, 0.801080207252363,
1, 0.858337365965949, 0.999687052533693, 0.666552625121279, 0.717453633245958,
0.621570152961453, 0.964658181346544, 0.65071758770312, 0.788971505000918,
0.980476054183113, 0.670263506919246, 0.600387040967624, 0.759173403408052,
1, 0.986409675965, 0.982996471134736, 1, 0.995340781899163, 0.999855895958986,
1, 0.846179233381267, 0.879226324448832, 0.795820998892035, 0.997586607285667,
0.848036806290156, 0.905320944437968, 0.947709125535428, 0.592172373022407,
0.826847031044922, 0.996916006944244, 0.785967729206612, 0.650346929853076,
0.84206351833549, 0.999043126652724, 0.936879214753098, 0.76674066557003,
0.591431233516217, 1, 0.999833445117791, 0.999606223666537, 0.6224971799303,
1, 0.974537160571494, 0.966717133936379
), inventoryCost = c(
1750702.95138889,
442784.114583333, 1114717.44791667, 472669.357638889, 716895.920138889,
735396.180555556, 3837320.74652778, 872873.4375, 2872414.93055556,
481095.138888889, 538125.520833333, 392199.045138889, 1469500.95486111,
459873.784722222, 656220.486111111, 1654143.83680556, 437511.458333333,
393295.659722222, 630952.170138889, 4920958.85416667, 1723517.10069444,
1633579.86111111, 4639909.89583333, 2167748.35069444, 3062420.65972222,
5132702.34375, 838441.145833333, 937659.288194444, 697767.1875,
2523016.31944444, 800903.819444444, 1054991.49305556, 1266970.92013889,
369537.673611111, 764995.399305556, 2322879.6875, 656021.701388889,
458403.038194444, 844133.420138889, 2430700, 1232256.68402778,
695574.479166667, 351348.524305556, 3827440.71180556, 3687610.41666667,
2950652.51736111, 404550.78125, 4749901.64930556, 1510481.59722222,
1422708.07291667
)
), .Names = c("beta", "inventoryCost"), class = c("data.frame")
)
plot(results$beta,results$inventoryCost)
mySpline <- smooth.spline(results$beta,results$inventoryCost, penalty=999999)
lines(mySpline$x, mySpline$y, col="red", lwd = 2)
Run Code Online (Sandbox Code Playgroud)
李哲源*_*李哲源 12
在建模之前明智地转换数据
根据您的比例results$inventoryCost,日志转换是合适的.为简单起见,我在下面使用x,y.我也在重新排序您的数据,以便x提升:
x <- results$beta; y <- log(results$inventoryCost)
reorder <- order(x); x <- x[reorder]; y <- y[reorder]
par(mfrow = c(1,2))
plot(x, y, main = "take log transform")
hist(x, main = "x is skewed")
Run Code Online (Sandbox Code Playgroud)
左图看起来更好?此外,强烈建议进一步采取转换x,因为它是歪曲的!(见右图).
以下转换是合适的:
x1 <- -(1-x)^(1/3)
Run Code Online (Sandbox Code Playgroud)
立方根(1-x)将使数据更加分散x = 1.我增加了一个,-1以便在x和之间存在积极的单调关系,而不是消极的关系x1.现在让我们检查一下这种关系:
par(mfrow = c(1,2))
plot(x1, y, main = expression(y %~% ~ x1))
hist(x1, main = "x1 is well spread out")
Run Code Online (Sandbox Code Playgroud)
拟合样条曲线
现在我们已准备好进行统计建模.请尝试以下电话:
fit <- smooth.spline(x1, y, nknots = 10)
pred <- stats:::predict.smooth.spline(fit, x1)$y ## predict at all x1
## or you can simply call: pred <- predict(fit, x1)$y
plot(x1, y) ## scatter plot
lines(x1, pred, lwd = 2, col = 2) ## fitted spline
Run Code Online (Sandbox Code Playgroud)
它看起来不错吗?注意,我用过nknots = 10tell smooth.spline来放置10个内部结(按分位数); 因此,我们应该使用惩罚回归样条而不是平滑样条.实际上,smooth.spline()除非你放入all.knots = TRUE(参见后面的例子),否则该函数几乎从不适合平滑样条.
我也放弃了penalty = 999999,因为这与平滑度控制无关.如果你真的想要控制平滑度,而不是让smooth.splineGCV找出最佳的平滑度,你应该使用参数df或spar.我稍后会举例.
要将拟合转换回原始比例,请执行以下操作:
plot(x, exp(y), main = expression(Inventory %~%~ beta))
lines(x, exp(pred), lwd = 2, col = 2)
Run Code Online (Sandbox Code Playgroud)
如您所见,拟合样条曲线与您预期的一样光滑.
有关拟合样条的说明
让我们看看拟合样条曲线的摘要:
> fit
Smoothing Parameter spar= 0.4549062 lambda= 0.0008657722 (11 iterations)
Equivalent Degrees of Freedom (Df): 6.022959
Penalized Criterion: 0.08517417
GCV: 0.004288539
Run Code Online (Sandbox Code Playgroud)
我们使用10节,最终有6个自由度,因此惩罚抑制了大约4个参数.平滑参数GCV在11次迭代后选择了lambda= 0.0008657722.
为什么我们要转变x为x1
Spline受到第二个衍生品的惩罚,但这种惩罚是在所有数据点的平均/综合二阶导数上.现在,看看你的数据(x, y).对于x前0.98,关系是相对稳定; 作为x方法1,关系迅速变得更加陡峭."变化点",0.98,具有非常高的二阶导数,远高于其他位置的二阶导数.
y0 <- as.numeric(tapply(y, x, mean)) ## remove tied values
x0 <- unique(x) ## remove tied values
dy0 <- diff(y0)/diff(x0) ## 1st order difference
ddy0 <- diff(dy0)/diff(x0[-1]) ## 2nd order difference
plot(x0[1:43], abs(ddy0), pch = 19)
Run Code Online (Sandbox Code Playgroud)
看看二阶差异/衍生物中的巨大峰值!现在,如果我们直接拟合样条曲线,则此变化点周围的样条曲线将受到严重惩罚.
bad <- smooth.spline(x, y, all.knots = TRUE)
bad.pred <- predict(bad, x)$y
plot(x, exp(y), main = expression(Inventory %~% ~ beta))
lines(x, exp(bad.pred), col = 2, lwd = 3)
abline(v = 0.98, lwd = 2, lty = 2)
Run Code Online (Sandbox Code Playgroud)
您可以清楚地看到样条曲线在接近数据后遇到一些困难x = 0.98.
当然,有一些方法可以在此变化点之后实现更好的近似,例如,通过手动设置较小的平滑参数或更高的自由度.但我们将走向另一个极端.请记住,惩罚和自由度都是全球性的衡量标准.增加模型复杂度后会得到更好的近似x = 0.98,但也会使其他部分更加坎坷.现在让我们试试45自由度的模型:
worse <- smooth.spline(x, y, all.knots = TRUE, df = 45)
worse.pred <- predict(worse, x)$y
plot(x, exp(y), main = expression(Inventory %~% ~ beta))
lines(x, exp(worse.pred), col = 2, lwd = 2)
Run Code Online (Sandbox Code Playgroud)
如您所见,曲线是坎坷的.当然,我们已经完成了50个数据的数据集,具有45个自由度.
事实上,你最初的滥用smooth.spline()是做同样的事情:
> mySpline
Call:
smooth.spline(x = results$beta, y = results$inventoryCost, penalty = 999999)
Smoothing Parameter spar= -0.8074624 lambda= 3.266077e-19 (17 iterations)
Equivalent Degrees of Freedom (Df): 45
Penalized Criterion: 5.598386
GCV: 0.03824885
Run Code Online (Sandbox Code Playgroud)
哎呀,45度自由,过度拟合!
| 归档时间: |
|
| 查看次数: |
3839 次 |
| 最近记录: |