R: t Distribution
Code Only
|
Code with Rweb Output
|
### Let's calculate the cumulative probability
pt(-1,df=10) ### First Example pt(0, df=10) ### Second Example pt(1, df=10) ### Third Example ### Calculating percentiles ### Find the 25th percentile with a degree of freedom=4 qt(.25, df=4) ### Find the 10th and 90th percentile with a degree of freedom=7 qt(c(0.10, 0.90), df=7) ### Create 3 random variables drawn from the t distribution ### with a degrees of freedom = 6 rt(3, df=6) ### The following code will display the Student's t distributions ### with various degrees of freedom and compare to the ### normal distribution x <- seq(-4.5, 4.5, .01) normal.curve <- dnorm(x) df <- c(1, 5, 20) colors <- c("purple", "red", "green") labels <- c("df=1", "df=10", "df=20", "normal") plot(x, normal.curve, , lty=2, xlab="x", ylab="Density", main=" t Distributions - Comparison of Different Degrees of Freedom") for (i in 1:3){ lines(x, dt(x,df[i]), lwd=2, col=colors[i]) } legend("topleft", inset=.1, title="Distributions", labels, lwd=2, lty=c(1, 1, 1, 2), col=colors) |
> ### Let's calculate the cumulative probability
> pt(-1,df=10) ### First Example [1] 0.1704466 > > pt(0, df=10) ### Second Example [1] 0.5 > > pt(1, df=10) ### Third Example [1] 0.8295534 > > ### Calculating percentiles > ### Find the 25th percentile with a degree of freedom=4 > qt(.25, df=4) [1] -0.7406971 > > ### Find the 10th and 90th percentile with a degree of freedom=7 > qt(c(0.10, 0.90), df=7) [1] -1.414924 1.414924 > > ### Create 3 random variables drawn from the t distribution > ### with a degrees of freedom = 6 > rt(3, df=6) [1] 0.3354537 1.4502319 0.4642840 > > ### The following code will display the Student's t distributions > ### with various degrees of freedom and compare to the > ### normal distribution > > x <- seq(-4.5, 4.5, .01) > normal.curve <- dnorm(x) > > df <- c(1, 5, 20) > colors <- c("purple", "red", "green") > labels <- c("df=1", "df=10", "df=20", "normal") > > plot(x, normal.curve, , lty=2, xlab="x", + ylab="Density", main=" t Distributions - Comparison of Different Degrees of Freedom") > > for (i in 1:3){ + lines(x, dt(x,df[i]), lwd=2, col=colors[i]) + } > > legend("topleft", inset=.1, title="Distributions", + labels, lwd=2, lty=c(1, 1, 1, 2), col=colors) |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.