【问题标题】:Scaling up a curve line such that it can be shown along side another curve line in R plot放大曲线,使其可以沿着 R 图中的另一条曲线显示
【发布时间】:2017-09-27 01:53:36
【问题描述】:

我有一个 R 图,我想在其中显示 IFredcurve(现在位于图没有正确显示)被乘以一个常数,它可以匹配蓝色曲线 正在展示中。

我想知道如何最好地放大 “红色”曲线,使其完全匹配“蓝色”曲线?

(我的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

【问题讨论】:

    标签: r plot statistics distribution


    【解决方案1】:

    基本上,我们需要按比例缩放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")
    

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

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

    【讨论】:

    • 亲爱的 d.b,您能否解释一下 rescale 在您的回复中的具体功能?
    猜你喜欢
    • 2021-03-12
    • 2018-12-02
    • 1970-01-01
    • 2018-11-20
    • 2021-07-19
    • 2018-09-10
    • 1970-01-01
    • 2016-07-28
    • 1970-01-01
    相关资源
    最近更新 更多