## Plot the t-distribution with varying sd and scale
require(graphics)
xx <- seq(-5, 15, length=201)
density <- dt3(xx, mean=5, sd=1, 4)
plot(xx, density, type='l', lwd=2, main="t-distribution with df = 4")
lines(xx, dt3(xx, 5, 2, 4), lwd=2, col=2)
lines(xx, dt3(xx, 5, 3, 4), lwd=2, col=3)
lines(xx, dt3(xx, 5, 4, 4), lwd=2, col=4)
legend('topleft', paste0("sd = ", 1:4), lwd=2, lty=1, col=1:4,
bty='n')
lines(xx, dt2(xx, 5, 1, 4), lwd=2, lty=2, col=1)
lines(xx, dt2(xx, 5, 2, 4), lwd=2, lty=2, col=2)
lines(xx, dt2(xx, 5, 3, 4), lwd=2, lty=2, col=3)
lines(xx, dt2(xx, 5, 4, 4), lwd=2, lty=2, col=4)
legend('topright', paste0("scale = ", 1:4), lwd=2, lty=2, col=1:4,
bty='n')
# Generate random numbers
rand2 <- rt2(1e6, location=5, scale=2, df=4)
mean(rand2)
sd(rand2) # about 2.83
rand3 <- rt3(1e6, mean=5, sd=2, df=4)
mean(rand3)
sd(rand3) # close to 2
# check pt* and qt*
prob <- pt2(x=7, location=5, scale=3, df=4)
qt2(p=prob, location=5, scale=3, df=4)
Run the code above in your browser using DataLab