假设从方差已知的正态分布中采样,因此将采用 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) }
但是,我无法获得所需的输出,如下所示:
预期的输出是这样的:
输出:
最佳答案
您的问题对于您真正想要的东西有点模糊。如果您需要一个函数来跟踪曲线下的面积,您可以使用我的这个函数并根据您的需要进行调整。
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)
密度参数控制阴影线。如果您想要全色,请不要指定密度。
关于r - 使用 R 中的假设检验显示关键区域的密度图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60810519/