绘制"镜像曲线"到原始曲线(" y"值很好," x"值不匹配)

时间:2014-06-12 23:06:42

标签: r plot

图形:
红色曲线是原始曲线,是回归的结果。蓝色曲线是红色曲线的单调版本。事情是,它正在增加而不是减少!我怎样才能转过来#34;这条蓝色曲线适合红色曲线?

## 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

pic

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

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

方法论:
对象&#34; fit2&#34;是函数重排()的输出。它总是单调增加。换句话说,我不确定如何将值 x y 匹配。

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

2 个答案:

答案 0 :(得分:3)

我们不能反转y的原因是x的时间间隔不是常数。

基本上,您需要做的是不仅要反转y,还要反转连续的x值对之间的间隙宽度向量。我们可以通过以下方式完成后者:

rev(diff(x))

然后我们只需要得到它们的累积总和并添加最小x值,这样我们就没有间隙宽度而是x值本身:

min(x) + cumsum(c(0, rev(diff(x))))

这些是您的新x值,您可以绘制:

lines(min(x) + cumsum(c(0, rev(diff(x)))), rev(fit2))

enter image description here

答案 1 :(得分:1)

修改更好的方法来解决您的问题:

由于曲线单调递减,rearrangement仅返回单调增加的曲线:

## 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)

enter image description here

因此不需要用于绘图的花哨''''重排'。您在fit3中获得的x值与您的数据和fit1相同。


假设您有一个可以与fit一起使用的对象predict的假设(例如,您使用glm之类的东西进行回归)的另一种方法:

## 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)

希望它有所帮助,

亚历