绘制“镜像曲线"到原始曲线(“y"值很好,“x"值不匹配) [英] Plotting "mirror curve" to the original curve ("y" values are good, "x" values do not match)

查看:48
本文介绍了绘制“镜像曲线"到原始曲线(“y"值很好,“x"值不匹配)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

图形化:
红色曲线是原始曲线,回归的结果.蓝色曲线是红色曲线的单调版本.问题是它在增加而不是减少!我怎样才能转动"这条蓝色曲线以适应红色曲线?

Graphically:
The red curve is the original curve, the result of the regression. The blue curve is the monotonic version of the red curve. The thing is that it is increasing instead of decreasing! How can I "turn" this blue curve to fit to the red one?

## data
x <- c(1.009648,1.017896,1.021773,1.043659,1.060277,1.074578,1.075495,1.097086,1.106268,1.110550,1.117795,1.143573,1.166305,1.177850,1.188795,1.198032,1.200526,1.223329,1.235814,1.239068,1.243189,1.260003,1.262732,1.266907,1.269932,1.284472,1.307483,1.323714,1.326705,1.328625,1.372419,1.398703,1.404474,1.414360,1.415909,1.418254,1.430865,1.431476,1.437642,1.438682,1.447056,1.456152,1.457934,1.457993,1.465968,1.478041,1.478076,1.485995,1.486357,1.490379,1.490719)
y <- c(0.5102649,0.0000000,0.6360097,0.0000000,0.8692671,0.0000000,1.0000000,0.0000000,0.4183691,0.8953987,0.3442624,0.0000000,0.7513169,0.0000000,0.0000000,0.0000000,0.0000000,0.1291901,0.4936121,0.7565551,1.0085108,0.0000000,0.0000000,0.1655482,0.0000000,0.1473168,0.0000000,0.0000000,0.0000000,0.1875293,0.4918018,0.0000000,0.0000000,0.8101771,0.6853480,0.0000000,0.0000000,0.0000000,0.0000000,0.4068802,1.1061434,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000,0.6391678)
fit1 <- c(0.5102649100,0.5153380934,0.5177234836,0.5255544980,0.5307668662,0.5068087080,0.5071001179,0.4825657520,0.4832969250,0.4836378194,0.4842147729,0.5004039310,0.4987301366,0.4978800742,0.4978042478,0.4969807064,0.5086987191,0.4989497612,0.4936121200,0.4922210302,0.4904593166,0.4775197108,0.4757040857,0.4729265271,0.4709141776,0.4612406896,0.4459316517,0.4351338346,0.4331439717,0.4318664278,0.3235179189,0.2907908968,0.1665721429,0.1474035158,0.1443999345,0.1398517097,0.1153991839,0.1142140393,0.1022584672,0.1002410843,0.0840033244,0.0663669309,0.0629119398,0.0627979240,0.0473336492,0.0239237481,0.0238556876,0.0084990298,0.0077970954,0.0000000000,-0.0006598571)
fit2 <- c(-0.0006598571,0.0153328298,0.0228511733,0.0652889427,0.0975108758,0.1252414661,0.1270195143,0.1922510501,0.2965234797,0.3018551305,0.3108761043,0.3621749370,0.4184150225,0.4359301495,0.4432114081,0.4493565757,0.4510158144,0.4661865431,0.4744926045,0.4766574718,0.4796937554,0.4834718810,0.4836125426,0.4839450098,0.4841092849,0.4877317306,0.4930561638,0.4964939389,0.4970089201,0.4971376528,0.4990394601,0.5005881678,0.5023814257,0.5052125977,0.5056691690,0.5064254338,0.5115481820,0.5117259449,0.5146054557,0.5149729419,0.5184178197,0.5211542908,0.5216215426,0.5216426533,0.5239797875,0.5273573222,0.5273683002,0.5293994824,0.5295130266,0.5306236672,0.5307303109)

## picture
plot(x, y)
lines(x, fit1, col=2)  # red curve
lines(x, fit2, col=4)  # blue curve

lines(x, fit2[length(fit2):1])

当然,由于 X 值的结构,这不起作用.

This, of course, does not work due to the structure of X values.

方法论:
对象fit2"是函数rearrangement()的输出.它总是单调递增.所以换句话说,我不确定如何将 x 值匹配到 y.

library(Rearrangement)
fit2 <- rearrangement(x=as.data.frame(x), y=fit1)

推荐答案

编辑 处理问题的更好方法:

Edit Better way to deal with your problem:

由于您的曲线是单调递减的,而 rearrangement 只返回单调递增的曲线:

Since your curve is monotonically decreasing, and rearrangement only returns monotonically increasing curves:

## rearrange the negative fit1
fit3 <- rearrangement(x=as.data.frame(x), y = - fit1)

## plot the negative rearranged fit3
plot(x, y)
lines(x, -fit3); points(x, -fit3, col=2)
lines(x, fit2); points(x, fit2, col=3)

因此不需要为绘图进行花哨的差异"重排.您在 fit3 中获得的 x 值与您的数据和 fit1 中的相同.

So no fancy ´diff´ rearrangements for plotting is needed. The x values you get in fit3 are the same from your data and fit1.

假设您有一个对象 fit 可以与 predict 一起使用的另一种方法(例如,如果您使用了类似 glm做回归):

Another approach under the assumption that you have an object fit that can be used with predict (if for example you used something like glm to do the regression):

## New x data, equidistant
newx <- data.frame(x = seq(1, 1.5, 0.01))

## Predict using the fitted model 
pr <- predict(fit, type = "response", newdata = newx)

## Make the result monotonic
re <- rearrangement(x = newx, y = pr)

## Plot reversing the order of `newx`
lines(rev(newx$x), re)

希望能帮到你,

亚历克斯

这篇关于绘制“镜像曲线"到原始曲线(“y"值很好,“x"值不匹配)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆