放大一条曲线,使其可以沿着 R 图中的另一条曲线显示

Scaling up a curve line such that it can be shown along side another curve line in R plot

我有一个 R 图,我想在其中显示 IF "red" curve(现在位于图的底部未正确显示)乘以一个常数,它可以匹配 当前显示的“bluecurve

我想知道如何才能扩大"red"曲线 以便它 完全匹配 "blue" 曲线?

(我的R代码在图片下方)

这是我的 R 代码:

SIGMA = 2                  # Population SIGMA known
observations = seq(1, 30)  # observations drawn
n = length(observations)   # number of observations
x_bar = mean(observations) # mean of observations
SE = SIGMA / sqrt(n)       # 'S'tandard 'E'rror of the mean
x.min = x_bar - 4*SE
x.max = x_bar + 4*SE

Like = function(x) sapply(lapply(x, dnorm, x = observations, SIGMA), prod) # multiplication of densities to obtain Likelihood values

curve(dnorm(x, x_bar, SE), from = x.min, to = x.max, col = 'blue', lwd = 3, lty = 2 ) # Sampling Distribution of x_bar
curve(Like, from = x.min, to = x.max, col = 'red', lwd = 3, add = T) # Likelihood function of MU

基本上,我们需要按比例缩放 cc2$y 的值,以便 cc2$y 的缩放值与 cc1$y 具有相同的范围(最小值和最大值)。我已经使用 scales 包的 rescale 函数来做到这一点

# Sampling Distribution of x_bar
cc1 = curve(dnorm(x, x_bar, SE), from = x.min, to = x.max, col = 'blue', lwd = 3, lty = 2 )

# Likelihood function of MU
cc2 = curve(Like, from = x.min, to = x.max, col = 'red', lwd = 3, add = T)

library(scales)
scale_factor = mean(rescale(cc2$y, range(cc1$y)) / cc2$y) #APPROXIMATE

plot(cc1, type = "l")
lines(cc2$x, cc2$y * scale_factor, col = "red")

这里是rescale2修改自scales库的rescale函数如果你想在不加载库的情况下使用它

rescale2 = function (x, to = c(0, 1))
{
    (x - min(x))/diff(range(x)) * diff(to) + to[1]
}