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
- 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 - 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 row–by–row 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 ; ( k–1) w i l l take v a l u e s 0 ,1 ,2 ,3 ,4
veck<– myrows + (k–1)∗.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 - 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[1], nuvec), qt(1-alphavec[2], nuvec), qt(1-alphavec[3], nuvec),
qt(1-alphavec[4], nuvec), qt(1-alphavec[5], nuvec), qt(1-alphavec[6], nuvec),
qt(1-alphavec[7], 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
}
}
# Answer is 473
#Q8
answer<- function(quantile, roundoff) # function
{
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)
# Answer is 473
OUTPUT
>myttable0.1 0.05 0.025 0.01 0.005 0.001 5e-041 3.078 6.314 12.706 31.821 63.657 318.309 636.6192 1.886 2.920 4.303 6.965 9.925 22.327 31.5993 1.638 2.353 3.182 4.541 5.841 10.215 12.9244 1.533 2.132 2.776 3.747 4.604 7.173 8.6105 1.476 2.015 2.571 3.365 4.032 5.893 6.8696 1.440 1.943 2.447 3.143 3.707 5.208 5.9597 1.415 1.895 2.365 2.998 3.499 4.785 5.4088 1.397 1.860 2.306 2.896 3.355 4.501 5.0419 1.383 1.833 2.262 2.821 3.250 4.297 4.78110 1.372 1.812 2.228 2.764 3.169 4.144 4.58711 1.363 1.796 2.201 2.718 3.106 4.025 4.43712 1.356 1.782 2.179 2.681 3.055 3.930 4.31813 1.350 1.771 2.160 2.650 3.012 3.852 4.22114 1.345 1.761 2.145 2.624 2.977 3.787 4.14015 1.341 1.753 2.131 2.602 2.947 3.733 4.07316 1.337 1.746 2.120 2.583 2.921 3.686 4.01517 1.333 1.740 2.110 2.567 2.898 3.646 3.96518 1.330 1.734 2.101 2.552 2.878 3.610 3.92219 1.328 1.729 2.093 2.539 2.861 3.579 3.88320 1.325 1.725 2.086 2.528 2.845 3.552 3.85021 1.323 1.721 2.080 2.518 2.831 3.527 3.81922 1.321 1.717 2.074 2.508 2.819 3.505 3.79223 1.319 1.714 2.069 2.500 2.807 3.485 3.76824 1.318 1.711 2.064 2.492 2.797 3.467 3.74525 1.316 1.708 2.060 2.485 2.787 3.450 3.72526 1.315 1.706 2.056 2.479 2.779 3.435 3.70727 1.314 1.703 2.052 2.473 2.771 3.421 3.69028 1.313 1.701 2.048 2.467 2.763 3.408 3.67429 1.311 1.699 2.045 2.462 2.756 3.396 3.65930 1.310 1.697 2.042 2.457 2.750 3.385 3.64632 1.309 1.694 2.037 2.449 2.738 3.365 3.62234 1.307 1.691 2.032 2.441 2.728 3.348 3.60136 1.306 1.688 2.028 2.434 2.719 3.333 3.58238 1.304 1.686 2.024 2.429 2.712 3.319 3.56640 1.303 1.684 2.021 2.423 2.704 3.307 3.55150 1.299 1.676 2.009 2.403 2.678 3.261 3.49660 1.296 1.671 2.000 2.390 2.660 3.232 3.460120 1.289 1.658 1.980 2.358 2.617 3.160 3.373Inf 1.282 1.645 1.960 2.326 2.576 3.090 3.291
>answer(0.975, 2)[1] 473