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

2024-01-01

我有一个 R 图,我想在其中展示IF the "red" curve(现在位于图的底部,未正确显示)乘以一个常数, 它可以match the "blue" curve目前显示。

我想知道我怎样才能最好scale up the “红”曲线以便它准确地matches the “蓝色”曲线?

(我的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。我用过rescale的函数scales包来做到这一点

# 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修改自rescale的函数scales库(如果您想在不加载库的情况下使用它)

rescale2 = function (x, to = c(0, 1))
{
    (x - min(x))/diff(range(x)) * diff(to) + to[1]
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐