One-Sample t-Procedures in R, STAT 3011 – Study Notes
offline

Difficulty: Intermediate | Prerequisites: Confidence interval basics, hypothesis testing logic, basic R syntax.

TL;DR

This topic covers how to use R to construct t-confidence intervals for a population mean, find t-critical values from the t-distribution, and run one-sample t-tests. The key R functions are t.test() for intervals and tests, and qt() for critical values. You need to know which arguments to pass (conf.level, mu, alternative) and how to read the output.

Key Terms

t-distribution

A family of symmetric, bell-shaped distributions indexed by degrees of freedom (df). It is wider and has heavier tails than the standard normal, especially at small sample sizes. As df increases, the t-distribution approaches the standard normal.

In simple terms, it is the distribution you use instead of the Z when you are estimating a population mean and the population standard deviation is unknown.

Degrees of freedom (df)

For a one-sample t-procedure, df = n - 1, where n is the sample size. The degrees of freedom determine which t-distribution to use.

Think of it as a measure of how much information the sample provides for estimating variability.

t-critical value (t)*

The cutoff from the t-distribution that marks the boundary of the middle C% of the distribution, where C is the confidence level. For a two-sided confidence interval with confidence level C, you need the value that leaves (1 - C)/2 in each tail.

In simple terms, t* tells you how many standard errors wide the confidence interval needs to be.

t.test() in R

The primary R function for one-sample t-procedures. It can construct a confidence interval and run a hypothesis test in a single call. Key arguments: the data vector, mu (the null value for testing), conf.level (the confidence level), and alternative ("two.sided", "less", or "greater").

qt() in R

The quantile function for the t-distribution. qt(p, df) returns the value below which a fraction p of the t-distribution's area falls. Used to find t-critical values directly.

Think of it as the inverse lookup: you give it a probability and degrees of freedom, and it gives you the t-value.

conf.level

The argument in t.test() that sets the confidence level for the interval. For a 92% confidence interval, you would write conf.level = 0.92. Default is 0.95.

alternative

The argument in t.test() that specifies the direction of the alternative hypothesis. Options are "two.sided" (default), "less", or "greater".

Core Content

Constructing a t-confidence interval with t.test()

  • Pass the data vector and the desired confidence level.

  • Example: for a 92% confidence interval on a variable called Electricity, use t.test(Electricity, conf.level = 0.92).

  • R returns a confidence interval as part of the output. For the quiz examples, intervals like (772.77, 1096.03) or (769.16, 1102.04) were produced this way.

  • The default confidence level is 0.95. If the question asks for anything other than 95%, you must specify conf.level.

Finding t-critical values with qt()

  • qt(p, df) gives the t-value below which a fraction p of the area lies.

  • For a two-sided confidence interval at level C with df degrees of freedom, the positive t* is qt((1 + C)/2, df).

    • Example: for a 92% CI with df = 4, you need qt(0.96, 4), which gives 2.333. The logic: (1 + 0.92)/2 = 0.96, and you want the value that leaves 4% in the upper tail.

  • Equivalent alternatives: qt(alpha/2, df) gives the negative t*, or qt(alpha/2, df, lower.tail = FALSE) gives the positive one.

    • Example: qt(0.04, 4) gives -2.333. qt(0.04, 4, lower.tail = FALSE) gives 2.333.

  • For a 93% CI with df = 5: qt(0.965, 5) = 2.297.

Running a one-sample t-test with t.test()

  • To test H0: mu = mu0, pass mu = mu0 to t.test().

  • To specify the direction, use the alternative argument.

    • alternative = "less" tests Ha: mu < mu0

    • alternative = "greater" tests Ha: mu > mu0

    • alternative = "two.sided" (default) tests Ha: mu is not equal to mu0

  • Example: t.test(Electricity, mu = 1000, alternative = "less") tests whether the population mean is less than 1000.

  • The P-value from the output is what you report. For the quiz, this gave values like 0.1987, 0.3167, 0.2089, and 0.2472 depending on the version.

Reading R output from t.test()

  • The output includes the t-statistic, degrees of freedom, P-value, the confidence interval, and the sample mean.

  • For exam purposes, you typically need to extract the confidence interval endpoints or the P-value and report them with the correct number of decimal places.

Formulas

t-confidence interval for a population mean

x-bar +/- t* x (s / sqrt(n))

where x-bar is the sample mean, s is the sample standard deviation, n is the sample size, and t* = qt((1 + C)/2, n - 1) for confidence level C.

Finding the positive t-critical value

t* = qt( (1 + C) / 2, df )

where C is the confidence level as a decimal (e.g. 0.92) and df = n - 1.

Equivalently: t* = qt( alpha/2, df, lower.tail = FALSE ), where alpha = 1 - C.

Relationship between confidence level and qt() arguments

For a 92% CI: alpha = 0.08, alpha/2 = 0.04, so qt(0.96, df) or qt(0.04, df, lower.tail = FALSE).

For a 93% CI: alpha = 0.07, alpha/2 = 0.035, so qt(0.965, df) or qt(0.035, df, lower.tail = FALSE).

Common Misconceptions

  • Passing the confidence level directly into qt(). Students sometimes write qt(0.92, df) for a 92% CI. This is wrong. You need qt(0.96, df), because (1 + 0.92)/2 = 0.96. The qt() function takes a cumulative probability, not a confidence level.

  • Forgetting to adjust degrees of freedom. For a one-sample t-procedure, df = n - 1, not n. If the sample has 5 observations, df = 4. Getting this wrong shifts the critical value and the interval.

  • Mixing up "less" and "greater" in the alternative argument. alternative = "less" tests Ha: mu < mu0. If you are testing whether the mean is greater than some value, you need alternative = "greater". Choosing the wrong direction gives the complement of the correct P-value.

  • Confusing the t-test P-value with the confidence interval. The P-value from t.test() with a specified mu answers the hypothesis-testing question. The confidence interval in the same output answers a different question (what range of values for mu is consistent with the data). They are related but not interchangeable.

Why It Matters / Exam Flags

  • Expect to be asked for the exact R command to produce a confidence interval at a non-default confidence level. Writing t.test(Electricity, conf.level = 0.92) correctly, including the argument name and value, is worth marks.

  • The qt() question is a common exam item. You will be given a confidence level and degrees of freedom and asked for the critical value. Know the formula: qt((1 + C)/2, df).

  • One-sample t-tests in R are tested by asking you to provide the R code and report the P-value. Double-check that you have the correct mu and the correct alternative direction.

  • Numerical answers should be reported to the precision shown in the R output (typically four decimal places for P-values, two for critical values and interval endpoints).

Quick Self-Test

Fill in the blank: To get a 93% confidence interval in R, the command is t.test(data, conf.level = ______).

0.93.

True or False: qt(0.92, 4) gives the correct positive t for a 92% confidence interval with 4 degrees of freedom.*

False. The correct call is qt(0.96, 4). You need (1 + 0.92)/2 = 0.96 as the first argument.

Fill in the blank: For a one-sample t-test of H0: mu = 950 against Ha: mu < 950, the R argument is alternative = "______".

"less".

True or False: If a sample has 6 observations, the degrees of freedom for a one-sample t-test is 6.

False. df = n - 1 = 5.

Fill in the blank: qt(0.035, 5) gives the ______ (positive/negative) t-critical value for a 93% CI with 5 df.

Negative. To get the positive value, use qt(0.965, 5) or qt(0.035, 5, lower.tail = FALSE).

Practice Q&A

Q: Write the R code to produce a 92% confidence interval for a variable called Electricity.

A: t.test(Electricity, conf.level = 0.92)

Q: What R command gives the positive t-critical value for a 93% confidence interval with 5 degrees of freedom?

A: qt(0.965, 5), which returns 2.297. The logic: (1 + 0.93)/2 = 0.965.

Q: You want to test whether the mean electricity usage is less than 1000 kWh. Write the R code, assuming the data are stored in a variable called Electricity.

A: t.test(Electricity, mu = 1000, alternative = "less")

Q: The R output from the command above shows a P-value of 0.1987. What is your conclusion at the 5% significance level?

A: Since 0.1987 > 0.05, we fail to reject H0. At the 5% level, there is insufficient evidence that the true mean electricity usage is less than 1000 kWh.

Q: A student writes qt(0.93, 5) to find t for a 93% CI. What did they do wrong, and what is the correct call?*

A: They passed the confidence level directly as the probability argument. The correct call is qt(0.965, 5), because you need the cumulative probability (1 + C)/2 = (1 + 0.93)/2 = 0.965, not C itself.

Connections to Other Topics

The one-sample t-test is the mean-based counterpart to the one-proportion z-test. Both follow the same hypothesis-testing framework (hypotheses, test statistic, P-value, decision), but the t-test uses the t-distribution because the population standard deviation is unknown and estimated from the sample. These t-procedures extend naturally to two-sample t-tests and paired t-tests later in the course. The qt() function also comes back when constructing prediction intervals and tolerance intervals in regression.


Related Terms / Search Tags

one-sample t-test, t-confidence interval, t-critical value, t.test R, qt R, degrees of freedom, conf.level, alternative hypothesis R, one-sided t-test, t-distribution, STAT 3011, intro to probability and statistics, t.test function, qt function, lower.tail