r - 使用 R 中的假设检验显示关键区域的密度图?

标签 r statistics normal-distribution density-plot hypothesis-test

假设从方差已知的正态分布中采样,因此将采用 z 检验(而不是 t 检验)。

mu0 <- 4                ## Null hypothesis mean value
stdev <- 3              ## Known population standard deviation
signif.level <- 0.05    ## Test significance level
sample.mean <- 6.07     ## Mean of the random sample
n <- 10                 ## Sample size
mu1 <- 6.2              ## Alternative hypotesis mean value to use for error type 2 


hyp.testing <- function(mu0, stdev, signif.level, 
                        sample.mean, n, show_crit, 
                        show_pvalue, show_alt, mu1, 
                        show_beta, show_power, two_sided) {
                      } 

我需要一个密度图,以红色条纹显示关键区域。

我尝试使用多边形,也称为类型 1 错误。我们可以使用多边形解决这个问题吗?

hyp.testing(4,3,0.05,6.07,10)  {

xval <- seq(-3.2, 3.2, length = 1000)
yval <- dnorm(xval)

 plot(xval, yval, type = "l", axes = TRUE, frame = FALSE, lwd = 3, 
 xlab = "", ylab = "")

 x <- seq(qnorm(.95), 3.2, length = 100)

 polygon(c(x, rev(x)),c(dnorm(x), rep(0, length(x))), col = "salmon")

 text(mean(x), mean(dnorm(x))+.02, "9%", cex = 1)

 text(qnorm(.95), .01, "1.645", cex = 1) }

但是,我无法获得所需的输出,如下所示:

预期的输出是这样的:

输出:

enter image description here

最佳答案

您的问题对于您真正想要的东西有点模糊。如果您需要一个函数来跟踪曲线下的面积,您可以使用我的这个函数并根据您的需要进行调整。

area_poly <- function(cur, cutoff, side=c(1,-1), col = "grey", border=NA, ...)
{
  if (side[1]>0 )# on the right
  {
    pos <- min(which(cur$x > cutoff))
    end <- length(cur$x)
  }
  else # on the left
  {
    pos <- max(which(cur$x < cutoff))
    end <- 1
  }
  polygon(x=c(cur$x[end:pos], cur$x[pos], cur$x[end]),
          y=c(cur$y[end:pos], 0, 0), col=col, border=border, ...)
}

它以曲线和截止点作为参数。例如:

cc <- curve(dnorm(x, mean = 4, sd = 3), from = -5, to = 10, n = 100, lwd = 3,
            xlab = "", ylab = "Density", frame = F)
area_poly(cc, cutoff = 6, side = 1, col = "grey50", density = 10)

enter image description here

密度参数控制阴影线。如果您想要全色,请不要指定密度。

关于r - 使用 R 中的假设检验显示关键区域的密度图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60810519/

相关文章:

c# - 自动记录和统计(C#应用程序)

postgresql - 当 n<2 时,postgres STDDEV 聚合行为

python - Python NumPy 中的 np.mean() 与 np.average()?

python - 如何根据高斯分布制作环

r - ggplotly 和 facet_wrap 的 X 轴错误

r - 如何使用 ggplot2 在箱线图中添加一条线

r - 如何将多行合并转换为一列?

c++ - 从 C/C++ 中的对数正态分布生成随机数

R: mixdist 包中的 mix() 返回错误

R Cairo pdf 字体在 Adob​​e Illustrator 中无法读取