s-news
[Top] [All Lists]

[S] way to output t-distribution and normal distribution tables

To: s-news@wubios.wustl.edu
Subject: [S] way to output t-distribution and normal distribution tables
From: vlmcguir@usgs.gov
Date: Thu, 24 Feb 2000 18:43:56 -0600
Sender: owner-s-news@wubios.wustl.edu
Thanks for your help with my question on a "way to output
       t-distribution and normal distribution tables" from S-Plus for
       import to Excel. I have more options than I knew.

       First. =======================================================
       It was pointed out that the t and normal distributions are already
       available through the Excel functions NORMDIST(), NORMINV(),
       TDIST(), TINV(), ...
           Marcel

       Second. =========================================================
       If you still want to export these tables from S-Plus, one way to do
       it is to create the necessary "matrix" using variants of pnorm() in
       S-PLUS, and then use write.table() to output into text files, which
       you can easily import into Excel.
          Anne

       Third. =========================================================
       Sure. This is a good time to use the outer() function
       ("for a good time, call outer()," that's my motto).

       Suppose you want t-tables for 1-10, 15, 20, 25, 50, and 100 df, for
       quantiles (.001, .01, .05, .1). Of course you can use any numbers
       you like here.

       Do this:

       my.df <- c(1:10, 15, 20, 25, 50, 100); my.quantiles <- c(.001,
       .01, .05, .1)
       my.frame <- round (outer (my.quantiles, my.df, qt), 4)
                 # produce table of rounded values of qt().
       dimnames(my.frame) <- list (my.quantiles, my.df)
                 # add row and column names
       write.table (my.frame, "clipboard", sep="\t")

       then go into Excel and paste with Control-V or Edit | Paste. Voila!

       The usual Normal table is only slightly trickier. Here, of course,
       we
       traditionally have part of the z-value down the rows and the other
       part across the columns; each entry is then Phi (z). You might
       do something like this:

       big.part <- format (round (c(1.1, seq (-3.4, 0, by = .1)), 3))[-1] #
       use
       characters for nice formatting of row names
       small.part <- seq (.00, .09, len=10) # column headers
       my.frame <- format (round (outer (big.part, small.part, function
       (x,y) pnorm
       (as.numeric (x) - y)), 4))# make into characters
       # The minus is needed when the "big.parts" are negative
       dimnames(my.frame) <- list (big.part, small.part)
       write.table (my.frame, "clipboard", sep="\t")

       It's more difficult to keep Excel from truncating trailing zeros,
       even
       if you prevent that in S-Plus. But I hope this helps,

       Sam Buttrey
       buttrey@nps.navy.mil

       Fourth. ============================================================
       > Table <- expand.grid(list(df = c(1, 5, 30, 1000),
          alpha = c(0.9, 0.5, 0.05, 0.001)))
       > Table <- cbind(Table, Qt = abs(qt(p = Table$alpha/2,
          df = Table$df)))
       > Table <- matrix(Table$Qt, ncol = 4, byrow = F,
           dimnames = list(as.character(c(1, 5, 30, 1000)),
           as.character(c(0.9, 0.5, 0.05, 0.001))))
       > Table
                  0.9       0.5      0.05      0.001
          1 0.1583844 1.0000000 12.706205 636.619249
          5 0.1321752 0.7266868  2.570582   6.868827
         30 0.1267296 0.6827557  2.042272   3.645959
       1000 0.1256933 0.6747352  1.962339   3.300283

       Then, you can export Table using S+ export menus or write.table()
       functions.

       Hope this helps,

       Renaud

       Fifth =======================================================

       I guess I don't understand your question, since you
       don't need S-Plus to generate these tables for Excel.

       A)  In Excel

       Normal:
       You can generate a column of values in Excel with the
       menu command Edit | Fill | Series, and then use the
       worksheet function NORMSDIST(x) to get the normal
       probabilities.  If you need densities, you could use the
       more general function NORMDIST(x, 0, 1, FALSE).

       "t" distribution:

       For the "t", you would set up your desired tail
       probabilities across the top of a grid, and run your
       degrees of freedom down the side.  Then use TINV(p,df)
       to compute the values.

       B) In S-Plus:

       Normal:

       For the normal table, you could set up a vector with the
       values of x ~ N(0,1) that are of interest.  Since the
       normal is symmetric, it is sufficient to start from zero.

       To create the values, use

       > x <- c(0,600)/100  # 0.0 to 6.0 by 0.01

       Now, make the table using pnorm()...

       > df.norm.table <- as.data.frame(cbind("X"=x,
       +"Pr(X)"=pnorm(x)))

       Pop open a data window.  Format the probabilities to
       show the desired precision.  Copy to the clipboard and
       paste into Excel.


       "t" Distribution.

       > # Select the tail probabilities, but enter them as
       > # cumulative probabilities
       > w <- 1 - c(0.10,0.05,0.025,0.01,0.005)
       > # Select your desired degrees of freedom values
       > y <- c(1:35,(8:20)*5)
       > y  # Print them to see what this did.
       > # Build the table
       > mx.t.table <- rbind(c(0,1-w),cbind(y,apply(
       +as.matrix(w),MARGIN=1,FUN=qt,df=y)))
       >
       > mx.t.table  # Print table to see what we did.
       >

       Now, open a data window to display the matrix mx.t.table
       so you can format your data columns to display the
       desired precision.  Whatever you set the displayed
       precision to here is what you'll get in Excel.

       That ought to give you a guide for working with these
       tables in either Excel or S-Plus.

       Cheers!

       Charles Thayer

Virginia (Ginny) L. McGuire
U.S. Geological Survey
100 Centennial Mall North, Room 406
Lincoln, NE 68508

vlmcguir@usgs.gov
(402)437-5124


-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>