# Build a t-Table

The “paper t table” referred to below is Devore’s Table A-5. Submit any combination of thequestions below.
1) Use R to build the paper t table. Call it myttable. It will have 7 columns and 39rows. Buildeach column separately. That means 7 assignments: col1 <- whatever,col2<- whatever, …, col7 <-whatever. Then use data.frame() or cbind() toput the columns together as we’ve done before. Makea vector alphavec to hold the αvalues .10, .05, . . . , .0005, and let nuvec hold the values in the νcolumn. Use characterversions of nuvec and alphavec to name the rows and columns. (Remember νis thesame thing as df = degrees of freedom.)
2) Build myttable again using a for(){} loop; let each iteration of the loop build a column andattach it to the table using cbind(). That means your loop will be indexed by alphavec. Initialize theloop like this: myttable<- NULL. Name the rows and columns.
3) Build myttable again, but this time let each iteration of your for(){} loop build a row and attach itto the table using . This time the loop will be indexed by . Initialize the loop as in 2 above. Namethe rows and columns.
4) We’ve built myttable by column and by row. Now build it element-by-element using nestedfor(){} loops. Index the outer loop by (ν) and the inner loop by (α). Initialize myttable as a 7x39matrix of zeros or NA’s. Name the rows and columns.
5) Same thing, but reverse the inner and outer loops.
6) No loops this time. Build myttable with a single line of code. Functions within functions. It’s notnecessary to include row and column names.
7) Use a loop or not for this part. We know that as df increases, the t dist converges to N(0, 1).
Use R to find the smallest value of df for which the .975 quantile of the t dist equals the .975quantile of the standard normal dist when both are rounded to two decimal places.
8) Write an R function to answer question 7 for any quantile and any number of decimal places.

quiz.t.table

• Use R to build the paper t table. Call it myttable. It will have 7 columns and 39
Build each column separately. That means 7 assignments: col1 <- whatever,
col2 <- whatever, …, col7 <- whatever. Then use data.frame() or cbind() to
put the columns together as we’ve done before. Make a vector alphavecto hold the α
values .10, .05, … , .0005, and let nuvechold the values in the ν column. Use character
versions of nuvecand alphavecto name the rows and columns. (Remember ν is the
same thing as df= degrees of freedom.)

• Build myttableagain using a for()fgloop; let each iteration of the loop build
a column and attach it to the table using cbind(). That means your loop will be
indexed by alphavec. Initialize the loop like this: myttable<- NULL. Name the rows
and columns.
• Build myttableagain, but this time let each iteration of your for()fgloop
build a row and attach it to the table using . This time the loop will be
indexed by . Initialize the loop as in 2 above. Name the rows and columns.
• We’ve built myttableby column and by row. Now build it element-by-element
using nested for()fg Index the outer loop by (ν) and the inner loop by (α).
Initialize myttableas a 7×39 matrix of zeros or NA’s. Name the rows and columns.
• Same thing, but reverse the inner and outer loops.
• No loops this time. Build myttablewith a single line of code. Functions within
It’s not necessary to include row and column names.
• Use a loop or not for this part. We know that as dfincreases, the t dist converges
to N(0; 1). Use R to find the smallest value of dffor which the .975 quantile of the
t dist equals the .975 quantile of the standard normal dist when both are rounded to
two decimal places.
• Write an R function to answer question 7 for any quantile and any number of
decimal places.

quiz.z.table

1. Use R to build the positive half of the paper z table (from 0 to 3.99). Build
each column separately. Then use cbind() or frame() to combine the columns
into a single matrix or data.frame. Finally, name the rows and columns like in the
paper table. If you want, use the code below as a starting point. The code appears
twice, once with comments and once without.
# b u i l d i n g the top 5×5 corner of z t a b l e ( the p o s i t i v e h a l f )
# in t h i s version we b u i l d the 5 columns s e p a r a t e l y
# use cbind () to combine them , then l a b e l the t a b l e to look nice
# f i r s t b u i l d row l a b e l s 0.0 , 0.1 , 0.2 , 0.3 , 0.4 , column l a b e l s 0.01 , . . . , 0.04
# simple way :
myrows<c ( . 1 , . 2 , . 3 , . 4 , . 5 )
# or
myrows<. 1 ( 0 : 4 )
# or
myrows<seq(0 , . 4 , by = . 1 )
# b u i l d mycols the same way ; or use myrows to make mycols
mycols<. 1 myrows
# now b u i l d input v e c t o r s f o r each column , c a l l them vec1 , vec2 , . . .
# vec1 i s 0.0 , 0.1 , 0.2 , 0.3 , 0.4 , vec2 i s 0.01 , 0.11 , 0.21 , 0.31 , 0.41 , e t c .
# here ’ s one way to b u i l d vec1 , vec2 , . . .
vec1 <myrows# t h a t was convenient !
vec2 <vec1 + .01
vec3 <vec2 + .01
vec4 <vec3 + .01
vec5 <vec4 + .01
# other ways a l s o good . . .
# now b u i l d each column by passing i t s input vec to pnorm ()
c o l 1 <pnorm( vec1 )
c o l 2 <pnorm( vec2 )
c o l 3 <pnorm( vec3 )
c o l 4 <pnorm( vec4 )
c o l 5 <pnorm( vec5 )
mymat<cbind( c o l 1 , c o l 2 , c o l 3 , c o l 4 , c o l 5 ) # combine columns i n t o a matrix
# check i t a g a i n s t paper t a b l e ; maybe l i k e t h i s :
mymat [ 3 , ] # p u l l out a row :
mymat [ , 5 ] # p u l l out a column :
mymat [ 2 , 3 ] # p u l l out a s p e c i f i c element
# n o t i c e t h a t mymat columns have i n h e r i t e d names col1 , col2 , . . .
# and n o t i c e t h a t mymat rows have no names
# we want to name both l i k e the paper t a b l e , so . . .
rownames(mymat) <myrows
colnames(mymat) <mycols
# here i s a f a n c i e r way , u s e f u l i f matrix has many dimensions
dimnames(mymat) <l i s t ( myrows , mycols )
The same code appears below without comments and extra steps.
myrows<c ( . 1 , . 2 , . 3 , . 4 , . 5 )
mycols<. 1 myrows
vec1 <myrows
vec2 <vec1 + . 0 1
vec3 <vec2 + . 0 1
vec4 <vec3 + . 0 1
vec5 <vec4 + . 0 1
c o l 1 <pnorm( vec1 )
c o l 2 <pnorm( vec2 )
c o l 3 <pnorm( vec3 )
c o l 4 <pnorm( vec4 )
c o l 5 <pnorm( vec5 )
mymat<cbind( c o l 1 , c o l 2 , c o l 3 , c o l 4 , c o l 5 )
rownames(mymat) <myrows
colnames(mymat) <mycols
2. Construct the positive half of the paper z table again, but this time build each
row separately and use rbind() to append it to the table. There are MANY rows, so
use a forfg The code below provides a starting point. You’ll have to make some
adjustments to make it work by row instead of by column. For example, cbind()
needs to become rbind(). And the step that creates veckwill have to change.
# i n i t i a l i z e some t h i n g s
myrows<seq(0 , . 4 , by = . 1 )
mycols<. 1 myrows
mymat<NULL # i t ’ s empty f o r now ; waiting to g e t b u i l t rowbyrow below
# now write a loop to b u i l d and bind each column
# k w i l l index our loop ; k w i l l take v a l u e s 1 ,2 ,3 ,4 ,5
# veck w i l l take v a l u e s vec1 , vec2 , . . . , vec5
# c o l k w i l l take v a l u e s col1 , col2 , . . . , col5
for ( k in 1 : 5 ) f
# b u i l d veck ; ( k1) w i l l take v a l u e s 0 ,1 ,2 ,3 ,4
veck<myrows + (k1).01
# pass veck to pnorm () and compute a t a b l e column
colk<pnorm( veck )
# bind the r e s u l t to our matrix ;
# we created mymat ahead of time
# so i t would be there when R looked f o r i t
mymat<cbind(mymat, colk )
g # loops are always enclosed by b r a c k e t s fg
rownames(mymat) <myrows
colnames(mymat) <mycols
3. One more time, but now fill the table cell-by-cell. This approach will require
two nested forfg Code below provides an example. Note the indentation and
placement of opening and closing brackets.
# i n i t i a l i z e some t h i n g s
myrows<seq(0 , . 4 , by = . 1 )
mycols<. 1 myrows
mymat<matrix (0 ,nrow=5, ncol=5) # s p e c i f y dimension of mymat b e f o r e loop
rownames(mymat) <myrows# t h a t means we can l a b e l mymat now
colnames(mymat) <mycols
# k w i l l index rows , j w i l l index columns
for ( k in 1 : 5 ) {
for ( j in 1 : 5 ) {
mymat [ k , j ] <pnorm( myrows [ k]+mycols [ j ] )
}
}

• No loops this time. And we will build the entire table,not just the positive half. The first two steps are like the tall skinny table we built inexcel.
(a) Let xvecbe the vector with elements
3.99,3.98,3.97,…,-.02,.01,0,0,.01,.02,…,98,3.99
It needs to have the value 0 repeated so that its length will be 800. There are
several ways to build xvec.
(b) Pass xvecto pnorm() to calculate the 800 prob’s that go in the table. Call this
result pvec.
(c) Pass pvecto the matrix() function to create mymat.
(d) The matrix mymatis almost but not quite identical to the paper z table. Identify
what’s wrong and fix it.

Solution

alphavec<- c(0.1,0.05,0.025,0.01,0.005,0.001,0.0005) #alpha values

nuvec<- c(c(1:30,32,34,36,38,40,50,60,120,Inf)) #degrees of freedom

#Q1

col1 <- round(qt(1-0.1,nuvec),3)

col2 <- round(qt(1-0.05,nuvec),3)

col3 <- round(qt(1-0.025,nuvec),3)

col4 <- round(qt(1-0.01,nuvec),3)

col5 <- round(qt(1-0.005,nuvec),3)

col6 <- round(qt(1-0.001,nuvec),3)

col7 <- round(qt(1-0.0005,nuvec),3)

myttable<- cbind(col1,col2,col3,col4,col5,col6,col7) #creating paper t table

rownames(myttable) <- as.character(nuvec) # naming rows

colnames(myttable) <- as.character(alphavec) # naming columns

#Q2

myttable<- NULL # initializing

for(i in 1:length(alphavec)) # building a column in a loop

{

col<- round(qt(1-alphavec[i], nuvec),3)

myttable<- cbind(myttable, col)

}

rownames(myttable) <- as.character(nuvec) # naming rows

colnames(myttable) <- as.character(alphavec) # naming columns

#Q3

myttable<- NULL # initializing

for(i in 1:length(nuvec)) # building a row in a loop

{

row<- round(qt(1-alphavec, nuvec[i]),3)

myttable<- rbind(myttable, row)

}

rownames(myttable) <- as.character(nuvec) # naming rows

colnames(myttable) <- as.character(alphavec) # naming columns

#Q4

myttable<- matrix(nrow = 39, ncol = 7) # initializing

for(i in 1:length(nuvec)) # outer loop indexed by degrees of freedom

{

for(j in 1:length(alphavec)) # inner loop indexed by alpha values

{

myttable[i,j] <- round(qt(1-alphavec[j],nuvec[i]),3) # building table element by element

}

}

rownames(myttable) <- as.character(nuvec) # naming rows

colnames(myttable) <- as.character(alphavec) # naming columns

#Q5

myttable<- matrix(nrow = 39, ncol = 7) # initializing

for(i in 1:length(alphavec)) # outer loop indexed by alpha values

{

for(j in 1:length(nuvec)) # inner loop indexed by degrees of freedom

{

myttable[j,i] <- round(qt(1-alphavec[i],nuvec[j]),3) # building table element by element

}

}

rownames(myttable) <- as.character(nuvec) # naming rows

colnames(myttable) <- as.character(alphavec) # naming columns

#Q6

# building table with a single line of code

myttable<- matrix(round(c(qt(1-alphavec, nuvec), qt(1-alphavec, nuvec), qt(1-alphavec, nuvec),

qt(1-alphavec, nuvec), qt(1-alphavec, nuvec), qt(1-alphavec, nuvec),

qt(1-alphavec, nuvec)), 3), nrow = 39, ncol = 7)

#Q7

for(v in 1:1000000) # iterating over the degrees of freedom

{

a <- round(qt(0.975, v), 2) #rounding the 0.975 quantile of t with d.f. v to 2 decimal places

b <- round(qnorm(0.975, 0, 1), 2) #rounding the 0.975 quantile of standard normal to 2 decimal places

if(a == b) # checking if the two quantiles are equal

{

print(v) # printing the smallest degrees of freedom for which the 2 quantiles are equal

break

}

}

#Q8

{

for(v in 1:100000000) # iterating over the degrees of freedom

{

a <- round(qt(quantile, v), roundoff) #rounding the given quantile of t with d.f. v to given decimal places

b <- round(qnorm(quantile, 0, 1), roundoff) #rounding the given quantile of standard normal to given decimal places

if(a == b) # checking if the two quantiles are equal

{

print(v) # printing the smallest degrees of freedom for which the 2 quantiles are equal

break

}

}

}

print(answer(0.975, 2)) # running the functile for quantile = 0.975 and roundoff = 2 (Q7)