Home » A Guide to dt, qt, pt, & rt in R

A Guide to dt, qt, pt, & rt in R

by Tutor Aspire

The Student t distribution is one of the most commonly used distribution in statistics. This tutorial explains how to work with the Student t distribution in R using the functions dt(), qt(), pt(), and rt().

dt

The function dt returns the value of the probability density function (pdf) of the Student t distribution given a certain random variable x and degrees of freedom df. The syntax for using dt is as follows:

dt(x, df) 

The following code illustrates a few examples of dt in action:

#find the value of the Student t distribution pdf at x = 0 with 20 degrees of freedom
dt(x = 0, df = 20)

#[1] 0.3939886

#by default, R assumes the first argument is x and the second argument is df
dt(0, 20)

#[1] 0.3939886
#find the value of the Student t distribution pdf at x = 1 with 30 degrees of freedom
dt(1, 30)

#[1] 0.2379933

Typically when you’re trying to solve questions about probability using the Student t distribution, you’ll often use pt instead of dt. One useful application of dt, however, is in creating a Student t distribution plot in R. The following code illustrates how to do so:

#Create a sequence of 100 equally spaced numbers between -4 and 4
x #create a vector of values that shows the height of the probability distribution
#for each value in x, using 20 degrees of freedom
y #plot x and y as a scatterplot with connected lines (type = "l") and add
#an x-axis with custom labels
plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "")
axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))

This generates the following plot:

Student t Distribution plot in R

pt

The function pt returns the value of the cumulative density function (cdf) of the Student t distribution given a certain random variable x and degrees of freedom df. The syntax for using pnorm is as follows:

pt(x, df) 

Put simply, pt returns the area to the left of a given value x in the Student t distribution. If you’re interested in the area to the right of a given value x, you can simply add the argument lower.tail = FALSE

pt(x, df, lower.tail = FALSE) 

The following examples illustrates how to solve some probability questions using pt.

Example 1: Find the area to the left of a t-statistic with value of -0.785 and 14 degrees of freedom.

pt(-0.785, 14)

#[1] 0.2227675

Example 2:  Find the area to the right of a t-statistic with value of -0.785 and 14 degrees of freedom.

#the following approaches produce equivalent results

#1 - area to the left
1 - pt(-0.785, 14)

#[1] 0.7772325

#area to the right
pt(-0.785, 14, lower.tail = FALSE)

#[1] 0.7772325 

Example 3:  Find the total area in a Student t distribution with 14 degrees of freedom that lies to the left of -0.785 or to the right of 0.785.

pt(-0.785, 14) + pt(0.785, 14, lower.tail = FALSE)

#[1] 0.4455351

qt

The function qt returns the value of the inverse cumulative density function (cdf) of the Student t distribution given a certain random variable x and degrees of freedom df. The syntax for using qt is as follows:

qt(x, df) 

Put simply, you can use qt to find out what the t-score is of the pth quantile of the Student t distribution.

The following code illustrates a few examples of qt in action:

#find the t-score of the 99th quantile of the Student t distribution with df = 20
qt(.99, df = 20)

# [1] [1] 2.527977

#find the t-score of the 95th quantile of the Student t distribution with df = 20
qt(.95, df = 20)

# [1] 1.724718

#find the t-score of the 90th quantile of the Student t distribution with df = 20
qt(.9, df = 20)

# [1] 1.325341

Note that the critical values found by qt will match the critical values found in the t-Distribution table as well as the critical values that can be found by the Inverse t-Distribution Calculator.

rt

The function rt generates a vector of random variables that follow a Student t distribution given a vector length n and degrees of freedom df. The syntax for using rt is as follows:

rt(n, df) 

The following code illustrates a few examples of rt in action:

#generate a vector of 5 random variables that follow a Student t distribution
#with df = 20
rt(n = 5, df = 20)

#[1] -1.7422445  0.9560782  0.6635823  1.2122289 -0.7052825

#generate a vector of 1000 random variables that follow a Student t distribution
#with df = 40
narrowDistribution #generate a vector of 1000 random variables that follow a Student t distribution
#with df = 5
wideDistribution #generate two histograms to view these two distributions side by side, and specify
#50 bars in histogram,
par(mfrow=c(1, 2)) #one row, two columns
hist(narrowDistribution, breaks=50, xlim = c(-6, 4)) 
hist(wideDistribution, breaks=50, xlim = c(-6, 4))

This generates the following histograms:

Histogram of t distributions

Notice how the wide distribution is more spread out compared to the narrow distribution. This is because we specified the degrees of freedom in the wide distribution to be 5 compared to 40  in the narrow distribution. The fewer degrees of freedom, the wider the Student t distribution will be.

Further Reading:
A Guide to dnorm, pnorm, qnorm, and rnorm in R
A Guide to dbinom, pbinom, qbinom, and rbinom in R

You may also like