## Review the Student's-t Distribution

On your USB drive, create a new directory, copy model.R to that directory, rename the file in the new directory, double click on the file to open Rstudio. Then copy all of the text below the line and paste it into your Rstudio editor pane.
```# A quick review of using pt and qt
#
#   For the Student's t distribution
#
# find P(X<1.34) for 6 degrees of freedom
pt( 1.34, 6 )
# find P( X > 1.34) for 9 degrees of freedom
1 - pt( 1.34, 9 )  # first way to do this
pt( 1.34, 9, lower.tail=FALSE)
#
# find P( -0.43 < X < 1.73 ) for 7 degrees of freedom
pt( 1.73, 7 ) - pt( -0.43, 7 )
#
# find P( X < -0.75 or X > 1.14 ) for 19 degrees of freedom
#            first way
pt( -0.75, 19 ) + ( 1 - pt(1.14, 19))
#            second way
pt( -0.75, 19 ) + pt(1.14, 19, lower.tail=FALSE)
#            third way
1 - ( pt(1.14, 19)-pt( -0.75, 19) )
#
# find the value of x such that
#    P( X < x ) = 0.23 for 6 degrees of freedom
qt( 0.23, 6 )
# find the value of x such that
#    P( X > x ) = 0.23 for 10 degrees of freedom
qt( 1 - 0.23, 10 )  # first way
qt( 0.23, 10, lower.tail=FALSE)  # second way

# there is another type of problem that we can
# do because the t distribution is symmetric.
# find x such that
#  P( -x < X < x ) = 0.58 for 13 degrees of freedom
#  we can find -x by
qt( (1 - 0.58)/2, 13)
# we can find x by
qt( 0.21, 13, lower.tail= FALSE)
# or
qt( 0.79, 13 )

#
#  or find x such that
#  P( X <-x or X>x ) = 0.075 for 4 degrees of freedom
# Because we have a symmetric distribution we
# also know that this means
# that P( X < -x ) = 0.075/2 = 0.0375
qt( 0.0375, 4 )  # which gives the value for -x
# or we could do
qt(0.0375, 4, lower.tail=FALSE) # to get x

######################################################
#  Now, let us try some for non-standard             #
#  t distributions.                                  #
######################################################
#
#  For a t population with mean=54.3 and
#  standard deviation 12.7,
#  find P( X < 63 ) for 3 degrees of freedom?
#  Convert to a t-score
t <- (63 - 54.3)/ 12.7
t
#  then use pt
pt( t, 3 )
#

#######################
# for a mean=74.2 and standard deviation=6.54)find
# P( X > 68) for 8 degrees of freedom
#
# convert to t-score
t <- (68 - 74.2)/12.7
t
#  use pt
1 - pt( t, 8 )  # looking to the left,
# or
pt( t, 8, lower.tail = FALSE)  # looking to the right

#
########################
# for a mean=3.2 and standard deviation= 0.73 distribution
#  find P( 2.8 < X < 3.6 ) for 7 degrees of freedom
#
# convert both values to t-scores
t_left <- (2.8 - 3.2)/ 0.73
t_left
t_right <- ( 3.6 - 3.2 )/ 0.73
t_right
# use the pt functions
pt( t_right, 7 ) - pt( t_left, 7 )
#

#########################
# For a mean=153, standard deviation=14  distribution find
# P( X < 140 or X > 170) for 8 degrees of freedom
#
# convert both values to t-scores
t_left <- ( 140 - 153 ) / 14
t_left
t_right <- ( 170 - 153 ) / 14
t_right
# use pt
pt( t_left, 8) + (1-pt( t_right,8) )
#    or we coud do that as
pt( t_left, 8 ) + pt( t_right, 8, lower.tail=FALSE)
#
#
################### qt
#
# for a mean = 13.2 and standard deviation=8.4 distribution
# find the value of x such that
# P( X < x ) is 0.315 for 23 degrees of freedom
# for a standard t value would be
t <- qt( 0.315, 23 )
t
# so we convert that to our mean=13.2 and standard deviation=8.4
# via
x <- t*8.4 + 13.2
x

################################
#  for a mean=4.3 and standard deviation= 8.2 distribution
# find the value of x such  that
#  P( X > x ) = 0.29 for 12 degrees of freedom
# for a standard t value would be
t1 <- qt( 1 - 0.29, 12 )
t1
#  which we could have found by
t2 <- qt( 0.29, 12, lower.tail=FALSE)
t2
# and then we need to move that value back
# to our mean=4.3 and standard deviation=8.2
x <- t1*8.2 + 4.3
x
#
```