R : Copyright 2005, The R Foundation for Statistical Computing Version 2.1.1 (2005-06-20), ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for a HTML browser interface to help. Type 'q()' to quit R. > ### *
> ### > attach(NULL, name = "CheckExEnv") > assign(".CheckExEnv", as.environment(2), pos = length(search())) # base > ## add some hooks to label plot pages for base and grid graphics > setHook("plot.new", ".newplot.hook") > setHook("persp", ".newplot.hook") > setHook("grid.newpage", ".gridplot.hook") > > assign("cleanEx", + function(env = .GlobalEnv) { + rm(list = ls(envir = env, all.names = TRUE), envir = env) + RNGkind("default", "default") + set.seed(1) + options(warn = 1) + delayedAssign("T", stop("T used instead of TRUE"), + assign.env = .CheckExEnv) + delayedAssign("F", stop("F used instead of FALSE"), + assign.env = .CheckExEnv) + sch <- search() + newitems <- sch[! sch %in% .oldSearch] + for(item in rev(newitems)) + eval(substitute(detach(item), list(item=item))) + missitems <- .oldSearch[! .oldSearch %in% sch] + if(length(missitems)) + warning("items ", paste(missitems, collapse=", "), + " have been removed from the search path") + }, + env = .CheckExEnv) > assign("..nameEx", "__{must remake R-ex/*.R}__", env = .CheckExEnv) # for now > assign("ptime", proc.time(), env = .CheckExEnv) > grDevices::postscript("tdist-Examples.ps") > assign("par.postscript", graphics::par(no.readonly = TRUE), env = .CheckExEnv) > options(contrasts = c(unordered = "contr.treatment", ordered = "contr.poly")) > options(warn = 1) > library('tdist') > > assign(".oldSearch", search(), env = .CheckExEnv) > assign(".oldNS", loadedNamespaces(), env = .CheckExEnv) > cleanEx(); ..nameEx <- "tdist" > > ### * tdist > > flush(stderr()); flush(stdout()) > > ### Name: TmixDist > ### Title: Distribution of a linear combination of independent Student's > ### t-variables > ### Aliases: tdist > ### Keywords: distribution > > ### ** Examples > > # Plot the cdf of the random variable T = t_1 + 2*t_2 + 3*t_3 + 4*t_4 + Z, > # where Z is a random variable with standard normal distribution > # and t_1, t_2, t_3 and t_4 are random variables with Student's t distribution > # with 1, 2, 3 and 4 degrees of freedom. The random variables are assumed > # to be stochastically independent. > > funx = numeric(0) > dff = c(1, 2, 3, 4, Inf) > lambda = c(1, 2, 3, 4, 1) > res = tdist(funx, dff, lambda, 1) > plot(res$xfun, res$yfun, type = 'l') > > ### > # Plot the pdf of the random variable T = ( Z + t_1 + t_10 )/3, > # where Z is a random variable with standard normal distribution > # and t_1 and t_10 are random variables with Student's t distribution > # with 1 and 10 degrees of freedom. The random variables are assumed > # to be stochastically independent. > > funtype = 2 > funx = numeric(0) > dff = c(Inf, 1, 10) > lambda = c(1, 1, 1) / 3 > pts = 6 > res = tdist(funx, dff, lambda, funtype, pts) > plot(res$xfun, res$yfun, type = 'l') > > ### > # Calculate the quantiles (for given probabilities 0.9, 0.95, 0.99) > # of the random variable T = ( t_1 + Z )/2, where Z is a random variable > # with standard normal distribution and t_1 is a random variable > # with Student's t distribution with 1 degree of freedom. The random variables > # are assumed to be stochastically independent. > > prob = c(0.9, 0.95, 0.99) > quantiles = tdist(prob, c(1, Inf), 1/2, 3)$yfun > cbind(prob, quantiles) prob quantiles [1,] 0.90 1.696137 [2,] 0.95 3.236059 [3,] 0.99 15.925971 > > ### > # Plot the characteristic function of the random variable T = t_1 + t_2 + t_3, > # where t_1, t_2, and t_3 are random variables with Student's t distribution > # with 1, 2, and 3 degrees of freedom. The random variables are assumed to be > # stochastically independent. > > chftt = tdist(numeric(0), c(1, 2, 3), c(1, 1, 1), 4) > chf = chftt[[1]] > tt = chftt[[2]] > plot(tt, chf, type = 'l') > > > > cleanEx(); ..nameEx <- "tmixdist" > > ### * tmixdist > > flush(stderr()); flush(stdout()) > > ### Name: TmixDist > ### Title: Distribution of a linear combination of independent Student's > ### t-variables > ### Aliases: ctdist dtdist ptdist qtdist rtdist > ### Keywords: distribution > > ### ** Examples > > # Plot the characteristic function of the random variable T = t_1 + t_2 + t_3, > # where t_1, t_2, and t_3 are random variables with Student's t distribution > # with 1, 2, and 3 degrees of freedom. The random variables are assumed to be > # stochastically independent. > > tt = c(seq(0,.1,,100), seq(.1,7,,200)) > chf = ctdist(tt, c(1, 2, 3), c(1, 1, 1)) > plot(tt, chf, type = 'l') > > ### > # Plot the pdf of the random variable T = ( Z + t_1 + t_10 )/3, > # where Z is a random variable with standard normal distribution > # and t_1 and t_10 are random variables with Student's t distribution > # with 1 and 10 degrees of freedom. The random variables are assumed > # to be stochastically independent. > > dff = c(Inf, 1, 10) > lambda = c(1, 1, 1) / 3 > x = seq(-4,4,,100) > pdf = dtdist(x, dff, lambda, 6) > plot(x, pdf, type = 'l') > > ### > # Plot the cdf of the random variable T = t_1 + 2*t_2 + 3*t_3 + 4*t_4 + Z, > # where Z is a random variable with standard normal distribution > # and t_1, t_2, t_3 and t_4 are random variables with Student's t distribution > # with 1, 2, 3 and 4 degrees of freedom. The random variables are assumed > # to be stochastically independent. > > dff = c(1, 2, 3, 4, Inf) > lambda = c(1, 2, 3, 4, 1) > x = seq(-36,36,,200) > cdf = ptdist(x, dff, lambda) > plot(x, cdf, type = 'l') > > ### > # Calculate the quantiles (for given probabilities 0.9, 0.95, 0.99) > # of the random variable T = ( t_1 + Z )/2, where Z is a random variable > # with standard normal distribution and t_1 is a random variable > # with Student's t distribution with 1 degree of freedom. The random variables > # are assumed to be stochastically independent. > > prob = c(0.9, 0.95, 0.99) > quantiles = qtdist(prob, c(1, Inf), 1/2, 3) > cbind(prob, quantiles) prob quantiles [1,] 0.90 1.696137 [2,] 0.95 3.236061 [3,] 0.99 15.403598 > > ### > # Generate 1000 realization of the random variable T = ( t_3 + t_5 + Z )/3, > # where Z is a random variable with standard normal distribution and t_3 and t_5 > # are a random variable with Student's t distribution with 3 and 5 degrees > # of freedom. The random variables are assumed to be stochastically independent. > > r = rtdist(1000, c(3, 5, Inf), 1/3) > > # Plot the binned kernel density estimate of the probability density of the > # generated data by 'bdke' procedure and compare with true density (red line). > > library(KernSmooth) KernSmooth 2.22 installed Copyright M. P. Wand 1997 > est <- bkde(r, bandwidth=0.25) > plot(est, ylim = c(-0, .6), type="l") > lines(est$x,dtdist(est$x, c(3, 5, Inf), 1/3), col = 'red') > > > > ### *