[R-sig-teaching] Plotting the P value
Joshua Wiley
jwiley.psych at gmail.com
Sun Apr 3 05:09:05 CEST 2011
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:
> 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:
>> Dear All R users:
>>
>>
>>
>> Can someone help me with the plotting the p-values under the normal curve (say).
>>
>>
>>
>> Thank you so much.
>>
>>
>>
>> Ali
>>
>>
>>
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>>
>> _______________________________________________
>> R-sig-teaching at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>>
>>
>
>
>
> --
> Paul E. Johnson
> Professor, Political Science
> 1541 Lilac Lane, Room 504
> University of Kansas
>
> _______________________________________________
> R-sig-teaching at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>
--
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/
More information about the R-sig-teaching
mailing list