7 t-tests and t CIs
8 The \(t\) distribution in R
8.1 Critical Values
In order to find the critical value of a CI: For a \((1-\alpha)\) CI, there should \((1-\alpha)\) in the middle of the sampling distribution. This means that there is \(\alpha/2\) is to the left of the lower value (\(-t^*\)) and \(\alpha/2\) to the right.
If we have a sample of, say, 20 people, then the critical value of a 95% CI is:
Verify these values with the t-table!
8.2 \(P\)-values
Just like with the normal distribution and the binomial distribution, we use the p*() functions to calculate probabilities.
If we have a t-statistic of, say, 2.093024 with a sample size of 20, then the \(P\)-value still depends on what the alternative hypothesis:
Notice that the p-value for 2.093024 is exactly 0.05 (within rounding errors) for a two-sided test, and that the critical value for a 95% CI is 2.093024. This is not a coincidence!
10 Built-in R function: t.test
R is for stats, and this is basic stats. This is how many, many, many scientific papers actually calculate their statistics, and is a great way for you to check your answer to textbook problems that provide the data.
Our values match exactly!
muis the value from the null hypothesis.alternativecan be either"two.sided","less", or"greater", as appropriate.levelis the confidence level, \(1 - \alpha\).
Important Note: The WeBWorK assignments expect you to use the \(t\)-table! Use R to check your answers, but make sure you can use the tables for WeBWorK and the exam.
11 Addendum: \(t_{n-1}\rightarrow Z\) as \(n\rightarrow\infty\)
#| standalone: true
#| viewerHeight: 700
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("n",
"Sample size (n)",
min = 2,
max = 100,
value = 5,
step = 1,
animate = TRUE),
sliderInput("obs",
"Test statistic",
min = -4,
max = 4,
value = 1.96,
step = 0.01,
animate = TRUE)
),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) {
output$distPlot <- renderPlot({
x <- seq(-4, 4, length.out = 200)
z <- dnorm(x)
t <- dt(x, df = input$n - 1)
pz <- round(2 * pnorm(-abs(input$obs)), 3)
pt <- round(2 * pt(-abs(input$obs), df = input$n - 1), 3)
dz <- dnorm(input$obs)
dt <- dt(input$obs, df = input$n - 1)
plot(z ~ x,
type = "l",
main = "Z vs t",
ylim = c(0, max(z)),
xlab = "Test statistic", ylab = "Density"
)
lines(c(input$obs, input$obs), c(0, dz))
lines(t ~ x, col = 2, lwd = 2)
lines(c(input$obs, input$obs), c(0, dt), col = 2)
# dt and dz are swapped so the higher label is the larger p-value
text(input$obs, dt, label = paste0("sigma known\np=", pz), pos = 2)
text(input$obs, dz, label = paste0("sigma unknown\np=", pt), pos = 4)
})
}
# Run the application
shinyApp(ui = ui, server = server)
Intuition check: Is the p-value shown a one-sided or two-sided p-value?