Skip to content

Plotting the P value

6 messages · Ali Zanaty, Paul Johnson, Derek Ogle +3 more

#
Run this:

http://pj.freefaculty.org/R/WorkingExamples/Normal1_2009_plotmathExample.R

Your eyes will dance with joy! As I demonstrate:

http://pj.freefaculty.org/R/WorkingExamples/Normal-2009.pdf

I've got a few handy examples in that same folder, browse all you want.

pj
On Sat, Apr 2, 2011 at 4:12 PM, Ali Zanaty <zanaty2005 at yahoo.com> wrote:

  
    
#
Ali,

I'm not completely sure of your purpose but I have my students use plot.htest() in my NCStats package (available at rforge.net/NCStats) to plot the p-value following the results of z.test() (from TeachingDemos package), t.test(), or chisq.test().  You might also consider distrib() in NCStats (it is more general and is simply a wrapper to pnorm(), qnorm(), etc. and won't, for example, show a two-tailed alternative).  Examples of both are in the help pages -- ?plot.htest or ?distrib.
#
Look at the power.examp function in the TeachingDemos function.  It may be what you want, or you could take some of the code from it to do what you want.

The function Pvalue.norm.sim (also in TeachingDemos) may also be of interest.
#
Hi Ali,

This is along the lines of Paul Johnson's plot, but more functiony and
sans fancy labels.  I just wrote this to speed up including some
examples of the shapes of different distributions and the effects on
"critical" regions in the tails for introductory psychology students
so it does not cover too many distributions.  But it is easy to use
and quickly outputs to PDF for easy inclusion in TeX documents.  With
a bit of tweaking, you could make the fill transparent and plot
several overlapping distributions on the same graph (e.g., for power
or ....)

HTH,

Josh

PlotDist <- function (alpha, from = -5, to = 5, n = 1000, filename = NULL,
    alternative = c("two.tailed", "greater", "lesser"), distribution =
c("normal",
        "t", "F", "chisq", "binomial"), colour = "black", fill = "skyblue2",
    ...)
{
    alternative <- match.arg(alternative)
    alt.alpha <- switch(alternative, two.tailed = alpha/2, greater = alpha,
        lesser = alpha)
    MyDen <- switch(distribution, normal = dnorm, t = dt, F = df,
        chisq = dchisq, binomial = dbinom)
    MyDist <- switch(distribution, normal = qnorm, t = qt, F = qf,
        chisq = qchisq, binomial = qbinom)
    crit.lower <- MyDist(p = alt.alpha, lower.tail = TRUE, ...)
    crit.upper <- MyDist(p = alt.alpha, lower.tail = FALSE, ...)
    cord.x1 <- c(from, seq(from = from, to = crit.lower, length.out = 100),
        crit.lower)
    cord.y1 <- c(0, MyDen(x = seq(from = from, to = crit.lower,
        length.out = 100), ...), 0)
    cord.x2 <- c(crit.upper, seq(from = crit.upper, to = to,
        length.out = 100), to)
    cord.y2 <- c(0, MyDen(x = seq(from = crit.upper, to = to,
        length.out = 100), ...), 0)
    if (!is.null(filename)) pdf(file = filename)
    curve(MyDen(x, ...), from = from, to = to, n = n, col = colour,
        lty = 1, lwd = 2, ylab = "Density", xlab = "Values")
    if (!identical(alternative, "greater")) {
        polygon(x = cord.x1, y = cord.y1, col = fill)
    }
    if (!identical(alternative, "lesser")) {
        polygon(x = cord.x2, y = cord.y2, col = fill)
    }
    if (!is.null(filename)) dev.off()
}
On Sat, Apr 2, 2011 at 2:55 PM, Paul Johnson <pauljohn32 at gmail.com> wrote:

  
    
1 day later
#
On Sat, Apr 2, 2011 at 11:12 PM, Ali Zanaty <zanaty2005 at yahoo.com> wrote:
See this thread [1] for additional pointers on doing this. Regards
Liviu

[1] http://www.mail-archive.com/r-sig-teaching at r-project.org/msg00278.html