问题描述
我有一个R图,我想在其中显示 IF 红色 曲线(现在位于底部)图未正确显示)乘以常数,它可以 匹配 蓝色 曲线当前正在显示。
I have an R plot in which I want to show that IF the "red" curve (which is now at the bottom of plot not showing correctly) be multiplied by a constant, it can match the "blue" curve currently showing.
我想知道如何最好地 扩大规模 红色曲线,以使其与蓝色曲线完全 匹配 ?
I'm wondering how best I can scale up the "red" curve so that it exactly matches the "blue" curve?
(我的R代码在图片下方。)
(My R code is provided below the picture.)
这是我的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
的缩放值具有与<$ c $相同的范围(最小和最大) c> cc1 $ y 。我已经使用 scales
包的 rescale
函数来做到这一点
Basically, we need to scale values of cc2$y
proportionally such that the scaled values of cc2$y
have the same range (minimum and maximum) as cc1$y
. I have used rescale
function of scales
package to do that
# 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
修改而来的缩放
库的功能,如果您想在不加载库的情况下使用它
Here is the rescale2
modified from rescale
function of scales
library if you want to use it without loading the library
rescale2 = function (x, to = c(0, 1))
{
(x - min(x))/diff(range(x)) * diff(to) + to[1]
}
这篇关于放大一条曲线,使其可以在R图中沿着另一条曲线显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!