#------- shade.tails ------------------- # draw probability density functions of t with critical regions shaded. # by default the function draws the 95% confidence interval on the normal # distribution. # # Input parameters # crit - the critical value of t (always a positive number) # df - degrees of freedom of the t distribution # tail - "upper", "lower" or "both" # xlim - the x axis range is -xlim to +xlim shade.tails <- function(crit=1.96, df = 10000, tail = "both",xlim=3.5) { curve(dt(x,df),-xlim,xlim,ylab="density",xlab="t") ylow = dt(xlim,df) pcrit = pt(crit,df) caption = paste(signif(1-pcrit,3)) if (tail == "both" | tail == "lower") { xx <- seq(-xlim,-crit,0.05) yy <- dt(xx,df) polygon(c(xx,-crit,-xlim),c(yy,ylow,ylow),density=20,angle = -45) text(-crit-0.7,dt(crit,df)+0.02,caption) } if (tail =="both" | tail == "upper") { xx2 <- seq(crit,xlim,0.05) yy2 <- dt(xx2,df) polygon(c(xx2,xlim,crit),c(yy2,ylow,ylow),density=20,angle = 45) text(crit+0.7,dt(crit,df)+0.02,caption) } }