2

Assume that sampling from a Normal distribution with known variance, so will employ a z-test (not t-test).

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) {
                      } 

I need a density plot showing the critical region in red stripes.

I tried using a polygon which is also known as error of type 1. Can we solve this using polygon?

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) }

But, I am unable to get the desired output as follows:

The expected output is something like this:

output:

enter image description here

  • Some clarifications needed: What is the function `hyp.testing` supposed to do? Do you just want to do a hypothesis test for the sample mean 6.07 against the null of mean 4? Then `qnorm(0.05, 4, 3, FALSE)` will give you the 95% rejection region (one-sided hypothesis), and `(6.07 - 4) / (3/sqrt(10))` will give you the test statistic. – broti Mar 23 '20 at 09:58
  • I would like to write this code in a user - defined function hyp.testing() for instance, hyp.testing(4,3,0.05,6.07,10) { ***Write the code here*** } – Special Circumstances Agent Mar 23 '20 at 10:12
  • Yes, but can you specify the output of the function? Terms like "two_sided" etc. are not very informative – broti Mar 23 '20 at 10:26
  • Yes, need to plot both one-sided and two-sided test plots – Special Circumstances Agent Mar 23 '20 at 19:31

1 Answers1

2

Your question is a bit vague on what you actually want. If you need a function to trace the area under your curve, you can use this function of mine and adapt it to your needs.

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, ...)
}

It takes a curve and a cutoff as argument. eg :

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

The density argument controls shading lines. If you want a full color, don't specify density.

RoB
  • 1,833
  • 11
  • 23